Mysql
 sql >> Database >  >> RDS >> Mysql

Quali sono i modi per spostare i dati più vecchi di "Y" giorni in una tabella archivio/cronologia in MySQL?

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