V
V
Vlad2016-05-28 15:00:07
MySQL
Vlad, 2016-05-28 15:00:07

How to speed up multiple INSERTs in Mysql?

I have a php script that executes a while loop that repeats 10,000 times and does 1 INSERT into the database each time.
As a result, the script execution time reaches almost a minute .. can I speed it up somehow?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
index0h, 2016-05-28
@index0h

You can shove the whole thing into a transaction, so the indexes are recalculated 1 time.
True, I would recommend a batch insert in a cycle. In the sense to fill a request for N inserts with one insert, execute, then fill it again and execute, etc.

A
Aleksey Ratnikov, 2016-05-31
@mahoho

Put the data into CSV (with fopen(), fputcsv()) and then use LOAD DATA INFILE (for MySQL) or BULK INSERT (for SQL Server). It will be ultra fast, since the instructions above were created specifically for bulk uploads.

A
Artyom Karetnikov, 2016-05-28
@art_karetnikov

Certainly. Don't do it in a loop, but do it in batches. By the way, it would be nice to show the code and explain why such a cycle is necessary.

Z
Zhainar, 2016-05-28
@zhainar

What prevents to collect one big request and make one request:

INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 ), ( ValueN, ValueN )

S
shagguboy, 2016-05-28
@shagguboy

LOAD DATA INFILE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question