How to perform batch operation using mysql and php

别来无恙 提交于 2019-12-12 03:32:15

问题


I am trying to insert multiple records using single MySQL query, but i don't want to insert a huge number of records at once.

The following code fetches, buildes and inserts the records

if(is_array($id_rec)){
        $values = "";
        foreach($id_rec as $key=>$value){
            $values .= "(".(int)$value.",".(int)$id_group."), ";
        }
        $values = rtrim($values,", ");
        $sql = "INSERT IGNORE INTO fu_rec_group_link (id_rec, id_group) VALUES ".$values;
        $GLOBALS['db']->execute($sql);

I have two questions here.

  1. Frist Question: How many records should i insert at once? What is right amount?
  2. Second Question: How can i pause/break the loop after reaching the max limit of records and insert it and then continue from i left?

Any help would be greatly appreciated.


回答1:


You should insert as many records as possible in a single INSERT as opposed to breaking it down into many INSERTS.

For example, doing

INSERT INTO mytable (column1, column2, column3)
VALUES ('text', 'text', 'text'),
       ('text', 'text', 'text'),
       ('text', 'text', 'text'),
       ('text', 'text', 'text');

is faster than doing

INSERT INTO mytable (column1, column2, column3)
VALUES ('text', 'text', 'text'),
       ('text', 'text', 'text');
INSERT INTO mytable (column1, column2, column3)
VALUES ('text', 'text', 'text'),
       ('text', 'text', 'text');

The aggregate difference in performance is more pronounced as the number of rows go up.



来源:https://stackoverflow.com/questions/35943395/how-to-perform-batch-operation-using-mysql-and-php

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