principlesqlMinor
MySQL INSERT INTO SELECT vs BULK INSERTs performance
Viewed 0 times
insertinsertsintobulkmysqlperformanceselect
Problem
Currently, I am trying to copy data from
In terms of insertions performance, would it be the same or faster if I would to do
TABLE1 to TABLE2.In terms of insertions performance, would it be the same or faster if I would to do
BULK INSERTmanually (i.e BULK insert every 10K records into TABLE2 viaINSERT INTO TABLE2 VALUES (1,2), (5,5), ...), versus
INSERT INTO TABLE2 SELECT * FROM TABLE1
Solution
You have to go with the BULK INSERT.
WHY NOT
Running
Imaging how populated an undo log will be to perform a single rollback.
If that transaction fails and rolls back, you create lots of table fragmentation.
Why
This takes a lot of pressure off the InnoDB Storage Engine for holding large undo information.
EXAMPLE : mysqldump
Have you ever noticed when reloading a mysqldump, hundreds or thousands of rows at a time are being inserted ? If you grep a mysqldump like this:
You will see many lines with INSERTs. Each INSERT is an extended insert by default. That allows 100's of rows to be inserted per INSERT command. So, the principle you already suggested of BULK INSERT 10K rows at a time is perfectly acceptable.
WHY NOT
INSERT INTO TABLE2 SELECT * FROM TABLE1 ???Running
INSERT INTO TABLE2 SELECT * FROM TABLE1 requires a single transaction.Imaging how populated an undo log will be to perform a single rollback.
If that transaction fails and rolls back, you create lots of table fragmentation.
Why
BULK INSERT manually ???This takes a lot of pressure off the InnoDB Storage Engine for holding large undo information.
EXAMPLE : mysqldump
Have you ever noticed when reloading a mysqldump, hundreds or thousands of rows at a time are being inserted ? If you grep a mysqldump like this:
grep "^INSERT" dump.sqlYou will see many lines with INSERTs. Each INSERT is an extended insert by default. That allows 100's of rows to be inserted per INSERT command. So, the principle you already suggested of BULK INSERT 10K rows at a time is perfectly acceptable.
Code Snippets
grep "^INSERT" dump.sqlContext
StackExchange Database Administrators Q#270906, answer score: 4
Revisions (0)
No revisions yet.