Principle
For locking reads (SELECT
with FOR UPDATE
or FOR SHARE
), UPDATE
, and DELETE
statements, the locks that are taken depend on whether the statement uses a unique index with a unique search condition or a range-type search condition.
- For a unique index with a unique search condition,
InnoDB
locks only the index record found, not the gap before it. - For other search conditions, and for non-unique indexes,
InnoDB
locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range.
In other words, DELETE FROM ... WHERE ...
sets an exclusive next-key lock on every record the search encounters. However, only an index record lock is required for statements that lock rows using a unique index to search for a unique row.