简体   繁体   中英

How do I clear old entries from a MySQL database table?

I have a MySQL database with one big table in it. After a while, it becomes too full and performance degrades. Every Sunday, I want to delete rows whose last update is older than a certain number of days ago.

How do I do that?

Self Answer

Make a web server that sends the following SQL to the database every weekend:

DELETE FROM table WHERE timestamp < DATE_SUB(NOW(), INTERVAL 7 DAY);

or

DELETE FROM table

WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))


I might need locking to prevent accumulation of jobs, like so:

DELIMITER //
CREATE EVENT testlock_event ON SCHEDULE EVERY 2 SECOND DO
BEGIN
 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
 BEGIN
   DO RELEASE_LOCK('testlock_event');
 END;
 IF GET_LOCK('testlock_event', 0) THEN
   -- add some business logic here, for example:
   -- insert into test.testlock_event values(NULL, NOW());
  END IF;
  DO RELEASE_LOCK('testlock_event');
END;
//
DELIMITER ;

Final answer:

CREATE EVENT `purge_table` ON SCHEDULE
        EVERY 1 DAY
    ON COMPLETION NOT PRESERVE
    ENABLE
    COMMENT ''
    DO BEGIN
IF GET_LOCK('purge_table', 0) THEN
DELETE FROM table WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY));
END;

What is the table design? Do you have a column with a timestamp?

Assuming you do, you could use that timestamp value with a datediff(your_date,CURDATE()) in a delete command.

Delete from table where datediff(date_col, CURDATE ()) > your_num_days.

Make a Scheduled Event to run your query every night. Check out Event Scheduler as well.

CREATE EVENT `purge_table` ON SCHEDULE
        EVERY 1 DAY
    ON COMPLETION NOT PRESERVE
    ENABLE
    COMMENT ''
    DO BEGIN
DELETE FROM my_table WHERE my_timestamp_field <= now() - INTERVAL 5 DAY
END

Maybe you can provide more information on how you are pushing the data to the DB and how you are working on the DB in general? Therefore we can help you and don't have to struggle with the dark...

I'll provide an easy solution: It's kind of workaround, but works:

Everytime you touch the data you update a time stamp in the updated rows.

Therefore you could easily filter them out every sunday.


UPDATE

The answer, the author provided by himself, was discussed at Stackoverflow and seems not to work in exactly that way, compare the discussion .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM