Archive
Posts Tagged ‘commit’
funny git commits
February 19, 2017
Leave a comment
Multiple inserts with MySQL/PHP
January 13, 2011
Leave a comment
Problem
You want to insert multiple records in a MySQL table (in a loop, for instance). How to improve the performance?
Solution #1
One way is to regroup all the inserts in one SQL command:
INSERT INTO x (a,b)
VALUES
('1', 'one'),
('2', 'two'),
('3', 'three')
Credits go here.
Solution #2
Another way is to regroup the inserts in a transaction:
mysql_query("START TRANSACTION");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("COMMIT"); // or "ROLLBACK" if you changed your mind
Credits go here.
Categories: mysql, php
commit, insert, transaction