I am trying to build a first-come first-get model sale page. We have n number of items of the same type. We want to assign these n items to the first n users who made the reques
The answer is: it will validate the WHERE
condition before updating the data.
Well, I have to say that this is a very interesting question. I've never thought about such a question before, and it enforces me to have a better understanding of how it works inside MySQL. Thank you!
I did my test for this situation at first. I know it should work like this even before I did my test, but I just didn't understand why.
Finally, I found something useful in the Index Condition Pushdown section.
This is how it works inside MySQL:
MySQL Server
↑ ↑
↓ ↓
Storage Engine(InnoDB here)
WHERE
conditions for rows.As you can see, locking occurs inside InnoDB, and MySQL Server evaluates the WHERE
condition after obtaining the rows. For your situation, the row(id = 5) is locked by the first UPDATE
, and the second UPDATE
gets stuck when fetching the same row. And the evaluation for the second UPDATE
's WHERE
condition occurs after obtaining the lock for the row.
What's more, if you have created an index on id
, Index Condition Pushdown will take place in your query.