I have got a question about the fastest way to copy a large number of records (around 10 million) from 1 table to another in MySQL.
The target table has 1 primary key, 4 indexes and 3 foreign key constraints. The source table has the same primary key, 3 equal indexes, no foreign keys.
Before copying I can truncate the target table, so it's not necessary to any duplicate key check (source table has the same primary key after all).
On my local development machine I have experimented with insert into, insert ignore into, insert into … on duplicate key update, replace into. The fastest result so far has been around 75 minutes (equal results for replace into and insert ignore into). Is there a faster way to do this?
Best Answer
(Seems like I'm able to post detailed answer here today... let me know if this has helped you) Here is a test output for you...
With larger records and data-size you might see increased margin of difference.
If those are MyISAM tables then you might want to review following notes from documentation:
Also loading into/from memory tables is much faster but based on size you've mentioned I suspect we have sufficient memory (you might see table-full errors and issues due to that...)!
Anyways, try following commands and share the speed results:
select * from t into outfile '/tmp/t.txt';
load data infile '/tmp/t.txt' into table t1;
(I guess <60 mins but no magic...)