In the SHOW PROCESSLIST – it is showing about 30+ rows has been locked for a few seconds and then update.
Is there a way to speed it up faster to update?
Example:
+------+-----------------+-----------+----------------+---------+------+----------+-------------------------------------------------------------------------------
| Id | User | Host | db | Command | Time | State | Info
+------+-----------------+-----------+----------------+---------+------+----------+--------------------------------------------------------------------------------
| 265 | user | localhost | xxxxxxxxxxxxxx | Query | 15 | Updating | UPDATE data SET status = '1', error = 'Unknown error' WHERE f= 0xxxxx
| 269 | user | localhost | xxxxxxxxxxxxxx | Query | 17 | Updating | UPDATE data SET status = '1', error = 'Invalid ....' WHERE f= 0xxx
| 280 | user | localhost | xxxxxxxxxxxxxx | Query | 7 | Updating | UPDATE data SET status = 1 WHERE f = 0xxxx
| 300 | user | localhost | xxxxxxxxxxxxxx | Query | 1 | Updating | UPDATE data SET status = '1', error = 'Unknown ....' WHERE f= 0xx
| 314 | user | localhost | xxxxxxxxxxxxxx | Query | 13 | Updating | UPDATE data SET status = '1', error = 'Invalid....' WHERE f= 0xxxx
| 327 | user | localhost | xxxxxxxxxxxxxx | Query | 11 | Updating | UPDATE data SET status = '1', error = 'Unknown ....' WHERE f= 0xxxx
| 341 | user | localhost | xxxxxxxxxxxxxx | Sleep | 2 | | NULL
| 350 | user | localhost | xxxxxxxxxxxxxx | Query | 7 | Updating | UPDATE data SET status = '1', error = 'Unknown ....' WHERE f= 0xxx
| 360 | user | localhost | xxxxxxxxxxxxxx | Query | 5 | Updating | UPDATE data SET status = 1 WHERE f = 0xxxx
There are a lot of UPDATE – I am using InnoDB, some fields are index.
In the data table, there are over 500,000 rows need to be updated while it looping in PHP CLI script (running in background/process in Linux).
Make sure the
fcolumn is indexed so the WHERE clause is using it. Otherwise you’ll scan the whole table for every UPDATE.Try to group updates so you can set many rows with one UPDATE instead of one row at a time.
Make sure you’re not using autocommit. Try to execute multiple changes per transaction.
Make sure you have tuned InnoDB. Most people use the default values for
innodb_buffer_pool_size,innodb_log_file_size, andinnodb_io_capacity. The default values are not tuned for high performance.Set
innodb_flush_log_at_trx_commit=2to reduce fsyncs, as @Paulo H. suggests.