Mysql – AWS RDS Post Optimisation after a huge delete

amazon-rdsawsinnodbmaintenanceMySQL

I'm using AWS RDS MySQL. All the tables are in InnoDB and enable innoDB_file_per_table.

One of my logging tables is growing too fast. I'd like to remove older than 1-week data. Even 1 week of data is 120GB. Every weekend this job will run and remove the old data.

I want to reclaim the disk space and update the statistics. OPTIMIZE table is really painful for us.

What are the optimizations I should perform after deleting a huge number of records?

Best Answer

PARTITION BY RANGE(TO_DAYS(date)) and have daily partitions. Every night DROP PARTITION for the week-old partition and REORGANIZE the normally empty "future" partition into tomorrow and a new "future".

That makes the delete essentially free and instantaneous. And keeps the disk space down to not much more than a week's worth of data.

More details .

(No Triggers are needed, no table rotation. Queries can work transparently on PARTITIONed tables. There may need to be some changes to the indexes.)