Reputation: 1637
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.
Any help would be greatly appreciated.
Upvotes: 0
Views: 712
Reputation: 36659
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.
Upvotes: 1