I run multiple servers and each of them are running multi-update statements such as these
UPDATE user SET ... WHERE user_id = 2;
UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 3;
If there is a concurrenct update like:
UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 2;
Then I will run into error deadlock detected
Right now, my fix is to order the update statement on client-side and always guarantee that the ids are in the same order. i.e. I always sort the statement on the client-side ASC by user_id
This has seemed to fix the issue so far, but I still have questions:
- Is this (order the statements) a good solution for fixing deadlock errors?
- If I start doing multi-table updates, do I have to order the statements across tables as well?
SET ...
? Is it bind variables, or constants, or expressions (increments of a count column?), or subselects? – Liebermann