目前我有一个在 PHP 中运行的 MySQL 查询,在查看结果时我更新了原始表,但是一个包含 500 行的简单表需要 30 秒才能完成:
$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);
}
是因为我正在更新我从中选择的表吗?我已经通过执行以下操作解决了这个问题,但是因为该表将来可能有数千条记录,所以我不确定 IN 的支持情况如何:
$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) . ')');