One option would be to use the INSERT INTO SELECT statement. Taking from sentences using timestamps to pull out the last lines, you can do something like ...
INSERT INTO t2 ( SELECT * FROM t1 WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR) );
This will take all the rows inserted in the previous hour and paste them into table 2. You could run the script for this query and run it every hour (or any interval you need).
This will greatly simplify your PHP script for pulling lines, since you will not need to iterate over all lines. It also eliminates the need to keep track of the last insert identifier.
Fanis solution also sounds as if it might be interesting.
As a note, the selection request in the above insertion can only be adjusted to insert certain fields. If you need only certain fields, you will need to specify them in the insert so ...
INSERT INTO t2 (field1, field2) ( SELECT field1, field2 FROM t1 WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR) );
Chris gutierrez
source share