The first Monday of July 2019, 7:52 a.m. The operations manager's message was short: "The monthly mileage report has been spinning for 20 minutes, and the client is waiting in a meeting." I looked at our position table: 1.9 billion rows, 410 GB of data, plus 130 GB of indexes on top. One table. One single InnoDB table. And every second, 90-100 new rows were being written to it, roughly 8 million position records a day. EXPLAIN said the report query would scan 240 million rows, and it did exactly what it said.
I was the one who had let that table grow. For two years, with a note to myself saying "we'll partition it later."
The Silent Decay of a Big Table
The insidious part was that the table didn't "break" one day; it slowly decayed over months. In the first year everything was great, because the active data (the last few weeks) fit in the buffer pool. The problems started once the data grew to several times the size of the buffer pool: the colder regions of the index fell to disk, and we started paying 8-10 ms per random read. The nightly summary jobs began spilling into the morning, and our attempts to purge old data with DELETE turned into disasters: the job deleting records older than 90 days ran for 6 hours, bloated the undo log, and pushed replication 40 minutes behind. We canceled the purge job, and the data kept growing. A textbook vicious circle.
Everyone knew the name of the solution: partitioning. What we didn't know was the chain of rules that partitioning brings with it in MySQL.
First Confrontation with the Primary Key
I still keep the file from our first attempt. We were going to split the table into daily RANGE partitions; we wrote the ALTER statement, ran it on the test server, and MySQL slapped us in the face with this error: "A PRIMARY KEY must include all columns in the table's partitioning function." Our primary key was (device_id, gps_time), but the column we wanted to use in the partitioning function was server_time; since we didn't trust the device clock, we kept the server arrival time separately. MySQL's rule is clear: the partitioning key has to be part of every unique index on the table. Otherwise, to guarantee uniqueness it would have to look at every partition, which defeats the whole purpose of partitioning.
We argued about it for two days. In the end, we made the primary key (device_id, server_time, gps_time) and accepted a fact: our per-device uniqueness guarantee was now the application layer's responsibility. We paid the price of that compromise once (when a firmware bug sent the same record twice, the table accepted both), but once we added duplicate filtering to the processing layer, the matter was closed.
The Finicky Nature of Pruning
All the magic of a partitioned table lies in partition pruning: if your query specifies a time range, MySQL only goes into the relevant partitions. On a 1.9-billion-row table, looking only at three days' worth of partitions when querying the last 3 days: that was the promise. In the first test, the promise didn't hold. In the EXPLAIN PARTITIONS output, we saw the query scanning every partition.
The cause was an old helper function I had written: queries were built in the form WHERE DATE(server_time) BETWEEN .... The moment you wrap the column in a function, pruning dies; MySQL can't work out which partition DATE(server_time) falls into. The same thing was actually killing index usage too, meaning we'd been carrying this bug for years but couldn't tell its cost apart on an unpartitioned table. We rewrote the entire query-generation layer to leave the column bare (server_time >= '2019-07-01' AND server_time < '2019-07-02'). And since we partitioned time using TO_DAYS, we learned to accept that an extra partition gets scanned when the BETWEEN boundaries fall in the middle of a day; pruning works at a coarse grain, not with surgical precision.
Moving 1.9 Billion Rows
The real problem was moving the existing giant into the partitioned structure. ALTER TABLE ... PARTITION BY works directly, but it copies the table; at our size, that meant more than 30 hours of locked copying, replication included. Unacceptable. Our plan was this: set up an empty, partitioned shadow table with the same schema, update the write layer to dual-write (every new record goes to both the old table and the new one), then copy the historical data in daily slices in the background. The copy script ran between 1:00 and 6:00 a.m., moved an average of 45 daily slices per night, and paused itself if replication lag exceeded 5 seconds. After 41 nights the two tables were in sync, and in a maintenance window we swapped them with RENAME TABLE. Total downtime: 11 seconds.

While I'm at it, let me note a fork in the road: instead of daily RANGE, HASH partitioning on device_id, or subpartitioning that combines the two, was also on the table. Tempting on paper; most queries do narrow down to a single device, after all. We passed, because our real open wound was on the time axis: purging old data cheaply and speeding up time-range reports. With HASH partitioning you can't say "drop everything older than 90 days" with DROP PARTITION; every partition holds data of every age. Subpartitioning, meanwhile, doubled the management complexity, and in 5.7 some combinations were restricted anyway. Choose your partitioning key based on the data lifecycle, not the query pattern; the indexes already take care of the query pattern.

Our partition layout became daily RANGE (TO_DAYS(server_time)), and every night a cron job opens the partition for 14 days ahead in advance. (We're also the ones who forgot this in the first week and got hit with a "future-dated record found no partition" error; we added a MAXVALUE partition later as a safety net.) The data retention policy is now a single line: ALTER TABLE ... DROP PARTITION. The 6-hour DELETE torture was replaced by a metadata operation that takes milliseconds. When we ran the first DROP PARTITION and saw the command return instantly, the office genuinely broke into applause.
The Numbers and the Remaining Scars
The balance sheet in numbers: that infamous monthly mileage report went from 19 minutes to 42 seconds (pruning and the query fix together). The last-24-hours vehicle track query dropped from an average of 1.8 seconds to 140 milliseconds. The nightly summary jobs started finishing at 2:15 a.m. instead of 5:40 a.m. Once we started regularly dropping partitions outside the 90-day window, disk usage fell from 540 GB to 210 GB, and our buffer pool hit rate rose from 87 percent to 99.2 percent, which is probably the unsung hero behind the real speedup.
After the migration I imposed a single habit on the team: every new query that touches the position table comes to code review together with its EXPLAIN PARTITIONS output. If the number of scanned partitions is higher than expected, the query gets sent back, and the discussion comes later. Thanks to this rule, we twice caught ORM-generated function wrapping that killed pruning, before it ever went live. A partitioned table won't protect you from undisciplined queries; you build the discipline through process.

Not everything is sunshine and roses, either. As the number of partitions grows, opening the table and some metadata operations slow down; we considered keeping around 400 daily partitions and folding the older ones into monthly partitions, but once the 90-day retention policy was settled, there was no need. And queries that don't include the partitioning key (say, an all-time search by license plate, independent of device) still walk through every partition. Partitioning doesn't make a bad query good; it clears the stones from a good query's path. Hang that sentence on the wall.
One more subtlety we learned later: on a partitioned table, "give me the latest record" queries like ORDER BY server_time DESC LIMIT 1 will, if you're not careful, touch each partition's index separately and merge the results. For a query like a device's last position, which runs hundreds of times per second, that was wasteful; the solution was to keep the query away from the partitioned table entirely. Each device's current state was already kept in a separate, tiny "latest state" table, so we read from there.
If you're wrestling with time-series data and your table is racing toward a billion rows, my advice is clear: plan partitioning when the table is at 100 million rows, not at 2 billion. If you'd like to discuss which key and what granularity you should partition your own schema by, get in touch here; the notes from our 41-night migration are still fresh.