Reputation: 9073
I have a table with 10 million records, what is the fastest way to delete & retain last 30 days.
I know this can be done in event scheduler, but my worry is if takes too much time, it might lock the table for much time.
It will be great if you can suggest some optimum way.
Thanks.
Upvotes: 3
Views: 2224
Reputation: 51
To expand on Michael Todd's answer.
If you have the space,
Assuming: table is the table name of the table you want to purge a large amount of data from newtable is the staging table name no other tables are called temptable
rename table table to temptable, newtable to table;
drop temptable;
This will be done in a single transaction, which will require an instantaneous schema lock. Most high concurrency applications won't notice the change.
Alternatively, if you don't have the space, and you have a long window to purge this data, you can use dynamic sql to insert the primary keys into a temp table, and join the temp table in a delete statement. When you insert into the temp table, be aware of what max_packet_size is. Most installations of MySQL use 16MB (16777216 bytes). Your insert command for the temp table should be under max_packet_size. This will not lock the table. You'll want to run optimize table to reclaim space for the rest of the engine to use. You probably won't be able to reclaim disk space, unless you were to shutdown the engine and move the data files.
Upvotes: 1
Reputation: 8809
You could try partition tables.
PARTITION BY LIST (TO_DAYS( date_field ))
This would give you 1 partition per day, and when you need to prune data you just:
ALTER TABLE tbl_name DROP PARTITION p#
http://dev.mysql.com/doc/refman/5.1/en/partitioning.html
Upvotes: 4
Reputation: 70490
Not that it helps you with your current problem, but if this is a regular occurance, you might want to look into a merge table: just add tables for different periods in time, and remove them from the merge
table definition when no longer needed. Another option is partitioning, in which it is equally trivial to drop a (oldest) partition.
Upvotes: 1
Reputation: 17051
Offhand, I would:
This will enable you to keep the table live through (almost) the entire process and get the past 30 days worth of data at your leisure.
Upvotes: 5
Reputation: 3082
Shutdown your resource,
SELECT .. INTO OUTFILE
, parse output, delete table, LOAD DATA LOCAL INFILE optimized_db.txt
- more cheaper to re-create, than to UPDATE.
Upvotes: 0