What are ways to move data older than 'Y' days to an archive/history table in MySQL?

非 Y 不嫁゛ 提交于 2019-12-11 07:19:50

问题


Looking to move data from a table A to history table B every X days for data that is Y days old and then remove the data from history table B that is older than Z days.

Just exploring different ways to accomplish this. So any suggestions would be appreciated.

Example for variables X - 7days Y - 60days z - 365days

Thank you


回答1:


CREATE PROCEDURE prc_clean_tables (Y INT, Z INT)
BEGIN
        BEGIN TRANSACTION;

        DECLARE _now DATETIME;
        SET _now := NOW();

        INSERT
        INTO    b
        SELECT  *
        FROM    a
        WHERE   timestamp < _now - INTERVAL Y DAY;
        FOR UPDATE;

        DELETE
        FROM    a
        WHERE   timestamp < _now - INTERVAL Y DAY;

        DELETE
        FROM    b
        WHERE   timestamp < _now - INTERVAL Z DAY;

        COMMIT;
END



回答2:


If you are using MySQL 5.1 you maybe able to use the event scheduler, instead of cron. I have not used it but I have used something similar in SQL Server.




回答3:


This seems straight forward.

You want a nightly cron job to run a script.

#crontab -e 

50 11 * * * $HOME/scripts/MyWeeklyArchive.sh

The script file itself is pretty simple as well. We'll just use mysqldump and the Now() function;

#! /bin/bash

/usr/bin/mysqldump -uUser -pPassword Current_DB Table --where='date < NOW() - INTERVAL 7 DAY' | /usr/bin/mysql -uUser -pPassword archive_DB

You could just include that line in the cron file, but for scalability and such I reccomend making it a script file.




回答4:


The simplest way of course would be to insert the rows from the online table to the history table then do your deletes. Wrapping that up in a SPROC as Quassnoi suggests would be perfect.

Another potentially better way would be to take advantage of partitioning. If you have your tables partitioned by date, you should be able to speed up at least the DELETE parts by simply dropping the relevant partition. The INSERT part may also be faster since all the inserted rows would come from one partition (if you create your partitions right).

If the schemas are the same, there may even be some kind of trick that would allow you to just move the partitions from one table to the other relatively instantaneously.



来源:https://stackoverflow.com/questions/808039/what-are-ways-to-move-data-older-than-y-days-to-an-archive-history-table-in-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!