Search code examples
mysqlcopy

Fastest way to copy a large MySQL table?


What's the best way to copy a large MySQL table in terms of speed and memory use?

Option 1. Using PHP, select X rows from old table and insert them into the new table. Proceed to next iteration of select/insert until all entries are copied over.

Option 2. Use MySQL INSERT INTO ... SELECT without row limits.

Option 3. Use MySQL INSERT INTO ... SELECT with a limited number of rows copied over per run.

EDIT: I am not going to use mysqldump. The purpose of my question is to find the best way to write a database conversion program. Some tables have changed, some have not. I need to automate the entire copy over / conversion procedure without worrying about manually dumping any tables. So it would be helpful if you could answer which of the above options is best.


Solution

  • Off the three options listed above.

    I would select the second option if you have a Unique constraint on at least one column, therefore not creating duplicate rows if the script has to be run multiple times to achieve its task in the event of server timeouts.

    Otherwise your third option would be the way to go, while manually taking into account any server timeouts to determine your insert select limits.