Batch inserts with PHP

杀马特。学长 韩版系。学妹 提交于 2019-12-20 04:23:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!