问题
Java has the PreparedStatement addBatch + executeBatch to do multiple insertions efficiently.
What is the fasted way to do a batch of inserts using php's mysqli extension?
Thanks!
回答1:
MySQL natively supports multiple insertions with one query.
INSERT INTO [Table] ([Column List])
VALUES ([Value List 1])
, ([Value List 2])
[...]
, ([Value List N])
回答2:
I realize this question is old, but for anyone else that comes across this, I found some useful information on this topic here: insert multiple rows via a php array into mysql.
Depending on your needs, it may be fastest to pipe the data as csv directly into LOAD DATA INFILE. However, be very cautious with this approach as it offers no protection against injection attacks.
I have used this approach in the past, validating the data cleanliness during csv generation.
来源:https://stackoverflow.com/questions/936458/batch-inserts-with-php