I have a main database that stores up to 5’000 new rows per day.
I want to have a second database that only contains the latest 30 days worth of data at any time.
Therefore I plan to set up a cron job that regularly dumps the rows older than 30 days and copies the new ones.
What would be the best design for the copying part?
- Copying on the fly with MySQL alone
- An MySql export to a txt file, then an MySql import, then deleting the temporary file
- A php script that iterates through the rows and copies them one by one
I want robustness and minimum amount of CPU/memory usage
The quickest and most robust way is to perform the transfer directly in MySQL. Here are the steps involved:
First, create the second table:
Next, insert the records 30 days old, or newer:
Lastly, delete the records older than 30 days:
It would be advisable to not run the
INSERTandDELETEstatements at the same time.