The quickest and most robust way is to perform the transfer directly in MySQL. Here are the steps involved:
First, create the second table:
CREATE TABLE IF NOT EXISTS second.last30days LIKE main_table;
Next, insert the records 30 days old, or newer:
INSERT INTO
second.last30days
SELECT
*
FROM
main_table
WHERE
created >= CURDATE() - INTERVAL 30 DAYS
ORDER BY created;
Lastly, delete the records older than 30 days:
DELETE FROM
second.last30days
WHERE
created < CURDATE() - INTERVAL 30 DAYS
ORDER BY created;
It would be advisable to not run the INSERT
and DELETE
statements at the same time.