问题
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.
- Frist Question: How many records should i insert at once? What is right amount?
- 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