I have the following query, and I wanted to use the diagram to CONFIRM IF I UNDERSTAND IT RIGHT:
SELECT * FROM table WHERE pk > 99;
"pk" is the primary key
I am having trouble understanding the next key lock, I found this diagram to know which gap lock will be applied and which "next key locks".
If the diagram is WRONG, let me know.
The diagram look right.
I'll assume
pk
in your example is the primary key of an InnoDB table, and is therefore the clustered index.The gap is locked starting one value greater than 97, and extending to infinity.
It seems strange, because the values 98 and 99 may seem like they should be free of locks, because the condition is on
WHERE pk > 99
, and therefore does not match the values 98 or 99. But the next-key lock locks the whole gap before the index record, down to but not including the preceding index record.https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html#innodb-next-key-locks says:
Demo: In a first window, I start a transaction that acquires a next-key lock:
Now the index record for pk 101 is locked, as well as the gap following pk 97.
In a second window I test this: