简体   繁体   中英

MySQL thousands of updates, slowing down

I need to run a PHP loop for a total of 100, 000 times (about 10, 000 each script-run), and each loop has about 5 MySQL UPDATES to it. When I run the loop 50 times, it takes 3 sec. When I run the loop 1000 times, it takes about 1300 sec. As you can see, MySQL is slowing down ALOT with more UPDATEs.

This is an example update:

mysql_query("UPDATE table SET `row1`=`row1` +1 WHERE `UniqueValue`='5'");

This is generated randomly from PHP, but I can store it in a variable and run it every n loops.

Is there any way to either make MySQL and PHP run at a consistent speed (is PHP storing hidden variables?), or split up the script so they do?

Note: I am running this for a development purposes, not for production, so there will only be 1 computer accessing the data.

Use prepared statements . One of their two main benefits is that repeated queries are more efficient.

Using something like this should be loads faster:

mysql_query("UPDATE table SET `row1`=`row1` + 1 WHERE `UniqueValue` IN (5, ... 10)");

Assuming you've got an index on uniqueValue ofcourse. You can create queries up to 8MB with the default settings but 1MB should be more than enough for now. It will be a huge query but much faster.

You could also temporarily disable the index updates entirely, that should make things a bit faster too:

ALTER TABLE table DISABLE KEYS;
ALTER TABLE table ENABLE KEYS;

关闭auto_commit并在N次之后显式提交事务(如果您使用的是InnoDB表)。

UniqueValue上添加索引,并从row1和其他未使用的索引中删除索引。

Apart from indexing the row1 and UniqueValue fields, also ensure that they are using an numeric datatype like INTEGER and remove those singlequotes from the UniqueValue in the WHERE clause.

mysql_query("UPDATE table SET `row1`=`row1` +1 WHERE `UniqueValue`=5");

Else MySQL would need to massage it forth and back to/from String which is a performance cost.

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