Currently I have a MySQL query I run in PHP and when going through the results I update the original table but a simple table with 500 rows takes 30 seconds to complete:
$sqlquery = mysql_query('SELECT id, special_data FROM stats_visits WHERE processed = 0');
while($row = mysql_fetch_assoc($sqlquery)){
$stat_id = $row['id'];
// Make use of special_data field for some operations
mysql_query('UPDATE stats_visits SET processed = 1 WHERE id = ' . $stat_id);
}
Is it because I am updating the table from which I am selecting? I’ve solved this by doing the following but because the table might have thousands of records in future I’m unsure how well the IN will hold up:
$statids = array();
$sqlquery = mysql_query('SELECT id, special_data FROM stats_visits WHERE processed = 0');
while($row = mysql_fetch_assoc($sqlquery)){
$statids[] = $row['id'];
// Make use of special_data field for some operations
}
mysql_query('UPDATE stats_visits SET processed = 1 WHERE id IN(' . implode(',', $statids) . ')');
In the first version you’re hitting the database once for every single update. This is normally a bad idea. The 2nd version is hitting the database once.
If you are concerned about how this will work out in the future, perhaps create a hybrid. Batch the updates up into groups of 100 and update them in blocks.