MySQL Forums :: InnoDB :: Should ORDER BY SELECTs to to avoid deadlocks?


Advanced Search

Re: Should ORDER BY SELECTs to to avoid deadlocks?
Posted by: Rick James ()
Date: October 02, 2016 12:33PM

If those SELECTs are fetching the rows you are about to UPDATE, the be sure to include FOR UPDATE.

If the SELECTs are over multiple rows in one table (such as with IN), try to sort the IN clause.

If the SELECTs cover multiple tables, consider ordering them consistently.

The above two items sometimes turn a "deadlock" into a less-invasive "lock_wait_timeout".

Remove things that can be safely done outside of the transaction. A common example of this is "normalization".

Avoid lengthy computations inside the transaction.

The only way to have a deadlock is to have two threads running at the same time. Some of my tips above will help make transactions faster, thereby less likely to trip over some other connection.

Regardless of what measures you take to prevent deadlocks, be ready to handle deadlocks:
* Check for errors, even after COMMIT.
* Write code to go back to the BEGIN and repeat the entire transaction.

Options: ReplyQuote


Subject Views Written By Posted
Should ORDER BY SELECTs to to avoid deadlocks? 493 Victor Porton 09/30/2016 12:52PM
Re: Should ORDER BY SELECTs to to avoid deadlocks? 323 Rick James 10/02/2016 12:33PM
Re: Should ORDER BY SELECTs to to avoid deadlocks? 271 Victor Porton 10/02/2016 12:46PM
Re: Should ORDER BY SELECTs to to avoid deadlocks? 273 Rick James 10/03/2016 10:59PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.