MySQL Forums
Forum List  »  InnoDB

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
Re: Should ORDER BY SELECTs to to avoid deadlocks?
960
October 02, 2016 12:33PM


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.