简体   繁体   中英

Why is this MySQL query slowing down my code

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) . ')');

如果只想将所有记录更新为要processed = 1为什么不通过单个查询来解决呢?:

UPDATE `stats_visits` SET `processed` = 1 WHERE `processed` = 0;

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.

The easiest way would be to store all of the ids which need updating as an array then implode these into the query with the IN operator.

eg

$processedArray= array();
while($row = mysql_fetch_assoc($sqlquery)){ 
    $stat_id = $row['id'];
    //Your processing goes here
    $processedArray[] = $stat_id; //Store an id if it needs updating as processed
}

mysql_query('UPDATE stats_visits SET processed = 1 WHERE id IN ('.implode(',', $processedArray).')');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM