We have a loop in our PHP code which inserts rows into a table. e.g.:
while ($info = mysql_fetch_assoc($someQuery)) {
mysql_query("INSERT INTO X (i,j) VALUES ($info['ii'],$info['jj'])");
}
This was fine a few months ago because the loop would only iterate several times. However, due to our website getting more traffic this loop now sometimes iterates 1000 or more times. The table has some overhead (4,305 KiB) and SELECTs from this table are appearing in the MySQL slow-log, probably because they are having to wait for a long list of INSERTs to release the locks?
How should I optimise the code so it can scale better?
Some things I thought I might try:
- INSERT DELAYED - Need to look into it. Could it help?
- Try inserting multiple rows in the same query. But what limit should I set? 50, 500, 1000?