Skip to content

MySQL table partitioning — when, how, and the maintenance routine

Range, list, and key partitioning in MySQL — when partitioning actually helps, the syntax we use, partition pruning verification, and the rolling-window pattern for time-series tables.

Table partitioning splits a single logical table into multiple physical pieces — partitions — that MySQL can prune at query time, drop wholesale at archive time, and store on separate filesystems. It is not a free performance win; applied to the wrong workload it just adds complexity. This article is the operator-grade view: when partitioning earns its keep, the syntax we use, how to verify pruning actually happens, and the rolling-window pattern that justifies most of the partitioning we deploy.

How to verify

After defining a partitioned table, confirm the partition layout and then prove the optimizer is pruning to the partitions you expect.

mysql -e "SHOW CREATE TABLE events\G"
mysql -e "SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, DATA_LENGTH \
  FROM information_schema.PARTITIONS WHERE TABLE_NAME='events';"
mysql -e "EXPLAIN PARTITIONS SELECT * FROM events WHERE created_at >= '2026-05-01';"
mysql -e "EXPLAIN FORMAT=JSON SELECT * FROM events WHERE id = 12345\G"

The partitions column in EXPLAIN PARTITIONS lists which partitions the optimizer plans to scan. If it shows every partition, your WHERE clause is not selecting on the partition key and partitioning is buying you nothing for this query.

What’s happening

MySQL stores each partition of an InnoDB table as a separate file (table#p#p202604.ibd). Each partition has its own indexes; an index defined on the table is replicated to each partition. There is no global index that crosses partitions — which is exactly why partition pruning works, and exactly why uniqueness constraints get strange.

The optimizer prunes at query time: if your WHERE clause filters on the partition key, the optimizer can prove some partitions cannot contain matching rows and skips them entirely. For a range-partitioned events table partitioned by month on created_at, a query WHERE created_at >= '2026-05-01' AND created_at < '2026-06-01' scans one partition; the same query without the upper bound scans the May partition and every later partition.

The headline use case is time-series. DROP PARTITION is a metadata operation — milliseconds — whereas DELETE FROM events WHERE created_at < ... reads, writes the undo log, writes the binlog, and replicates every row deleted. For a one-billion-row table archive, partitioning turns a multi-day delete into a single statement.

The trade-offs are real. Every unique index must include the partition key. Foreign keys do not work on partitioned tables in MySQL. Queries that do not filter on the partition key scan every partition, which is slower than the equivalent monolithic table because each partition has its own B-tree to traverse.

The procedure

  1. Decide whether partitioning helps. The workload qualifies if:

    • You have a date or numeric column you WHERE on almost every query.
    • You need to drop or archive old rows by that column.
    • The table is large enough that maintenance is a problem (typically tens of millions of rows or larger).
    • You can accept that foreign keys to this table will not be enforced.
  2. Define the partitioned table with a clear scheme. Range by date is the most common in our engagements.

    CREATE TABLE events (
      id BIGINT NOT NULL AUTO_INCREMENT,
      created_at DATETIME NOT NULL,
      user_id BIGINT NOT NULL,
      event_type VARCHAR(50) NOT NULL,
      payload JSON,
      PRIMARY KEY (id, created_at),
      INDEX idx_user (user_id, created_at)
    ) ENGINE=InnoDB
    PARTITION BY RANGE (TO_DAYS(created_at)) (
      PARTITION p202604 VALUES LESS THAN (TO_DAYS('2026-05-01')),
      PARTITION p202605 VALUES LESS THAN (TO_DAYS('2026-06-01')),
      PARTITION p202606 VALUES LESS THAN (TO_DAYS('2026-07-01')),
      PARTITION pmax    VALUES LESS THAN MAXVALUE
    );

    The primary key must contain created_at because every unique index has to include the partition key. The trailing MAXVALUE partition is the catch-all — any row that arrives outside the defined ranges lands here.

  3. Confirm partition pruning works for the typical query.

    EXPLAIN PARTITIONS SELECT COUNT(*) FROM events WHERE created_at BETWEEN '2026-05-15' AND '2026-05-20';

    The partitions column should show p202605. If it shows every partition, the WHERE clause needs work — WHERE MONTH(created_at) = 5 defeats pruning; WHERE created_at >= ... enables it.

  4. Roll the window forward by adding next month and dropping ancient months. This is the routine that makes partitioning earn its keep.

    -- add June if not already defined
    ALTER TABLE events
      REORGANIZE PARTITION pmax INTO (
        PARTITION p202606 VALUES LESS THAN (TO_DAYS('2026-07-01')),
        PARTITION pmax    VALUES LESS THAN MAXVALUE
      );
    
    -- drop everything from 2025 in one operation
    ALTER TABLE events DROP PARTITION p202501, p202502, p202503;

    DROP PARTITION deletes the data files and updates the metadata — typically milliseconds even on a 100 GB table.

  5. To archive instead of drop, exchange the partition out into a separate table first.

    CREATE TABLE events_202501 LIKE events;
    ALTER TABLE events_202501 REMOVE PARTITIONING;
    ALTER TABLE events EXCHANGE PARTITION p202501 WITH TABLE events_202501;
    -- now events_202501 holds the data; dump or move it as needed
  6. Schedule the rolling maintenance. We use a cron-driven Python script that runs nightly: ensure next month exists, drop partitions older than the retention window, alert if pmax has more rows than expected (indicating a missing month).

Common pitfalls

  • Partitioning without a partition-key filter — most queries scan every partition; you pay the per-partition overhead and gain nothing.
  • WHERE function(partition_col) = ... — MONTH(created_at) = 5 cannot prune; created_at >= '2026-05-01' AND created_at < '2026-06-01' can.
  • Forgetting MAXVALUE — a row that lands after the highest defined partition raises an error or silently fails to insert depending on the version. Always include the catch-all.
  • Foreign keys — InnoDB will refuse to add a foreign key to or from a partitioned table. Applications often discover this when a migration fails.
  • Too many partitions — every partition has its own file handle and metadata. A million partitions cripples server start time. Keep partition counts in the hundreds, not thousands.

Partitioning earns its keep on time-series and event tables where the dominant access pattern is recent data and the dominant maintenance task is dropping ancient data. In the engagements we run, partitioned tables come with a rolling-window job, an alert on missing partitions, and EXPLAIN PARTITIONS checks baked into the schema migration tests so a partition-defeating query never ships. For the broader pattern of operating large MySQL schemas like this, see /en/knowledge-base/.