Copy millions of rows to another table in batches mySQL
3
votes
2
answers
9429
views
Table A
which is always getting updated (records being inserted or updated).
Table A
contains millions of records.
I'd like to copy some of these records to a new table Table B
.
Table A
and Table B
has exact same schema.
How can I copy records from Table A
to Table B
? I don't want to consider the data which keeps getting updated in Table A
. I only want to copy the data which is there when I first queried Table A
. I'm trying to copy data in batches. So everytime I query for a batch of 500 records from Table A
and copy them to Table B
. The next time I query Table A
to get next 500 records using offset
. There is no guarentee that the new set of records are exactly next batch of 500 records since Table A
is always getting updated. The task is to be able to ensure we are fetching batches in sequential way and it guarentees we have exactly next 500 records.
INSERT INTO Table B
FROM SELECT * FROM Table A
WHERE ...
doesn't work. Because as I mentioned Table A
has a lot of data and running this query timesout. It needs to be carried out in batches. Creating a temporary table would also require to copy it in batches.
I tried to use mySQL views but they also have the same problem. The view fetches data from the underlying table. If the underlying table gets updated the view fetches the updated data.
Asked by BountyHunter
(33 rep)
Nov 10, 2021, 09:29 AM
Last activity: Nov 11, 2021, 12:20 AM
Last activity: Nov 11, 2021, 12:20 AM