Skip to content

MySQL and InnoDB in depth · Part 4 of 10

InnoDB locks: record, gap and next-key locks

InnoDB locks index entries, not abstract rows. Learn what record, gap, next-key and intention locks protect, and why a query without a usable index can lock far more than you expect.

· 4 min read

mysqlinnodblocking

When two sessions fight over the same data in MySQL, the fight happens in InnoDB's lock manager. Knowing which lock a statement takes explains slow updates, lock wait timeouts and deadlocks. The most important idea is simple: InnoDB locks index entries, not rows in the abstract.

Shared and exclusive modes

A lock is held in one of two modes. A shared (S) lock lets other sessions read the same data but not change it, and many sessions can hold it at once. An exclusive (X) lock is for writing and is held by one session at a time. SELECT ... FOR SHARE takes S locks. SELECT ... FOR UPDATE, UPDATE and DELETE take X locks. Plain consistent reads take no locks at all.

Record, gap and next-key locks

Because the lock sits on index entries, there are three shapes.

  • A record lock locks a single index entry, for example the row with id = 5 found through the primary key.
  • A gap lock locks the empty space between two index entries, but not the entries themselves. It stops other sessions from inserting into that gap.
  • A next-key lock is a record lock plus the gap before it. It is InnoDB's default when scanning a range at REPEATABLE READ.

Gap locks exist to prevent phantoms. If a transaction runs SELECT ... WHERE amount > 100 FOR UPDATE, locking only the matching rows would still let another session insert a new matching row. The gap locks close that door until the first transaction ends.

What a statement actually locks

The exact footprint depends on the index and the predicate.

  • An equality search on a unique index that finds the row takes only a record lock on that entry.
  • An equality search on a unique index that finds nothing takes a gap lock on the gap where the row would be.
  • A search on a non-unique index, or any range, takes next-key locks over the scanned entries, plus a gap lock after the last one.
  • A statement that cannot use an index scans the whole clustered index and, at REPEATABLE READ, can end up locking every row it touches along the way, in practice most of the table.
SQL
-- session A
START TRANSACTION;
SELECT * FROM orders WHERE customer_id = 42 FOR UPDATE;

-- session B: blocks if customer_id has no index, or if the new
-- row falls inside a gap that session A has locked
INSERT INTO orders (customer_id, total) VALUES (42, 10);

If customer_id has no index, session A locks the whole scan, and unrelated updates elsewhere in the table wait behind it. Adding the missing index narrows both the query plan and the lock footprint.

Intention locks. InnoDB also takes intention locks at table level. An intention shared (IS) or intention exclusive (IX) lock is a flag that says "some row in this table is locked in this mode". They let the engine decide quickly whether a table-level request, such as an ALTER TABLE, conflicts with row locks, without checking every row. Intention locks conflict with very little, so you rarely think about them, but you will see them when reading lock diagnostics.

How isolation level changes locking. At READ COMMITTED, InnoDB takes record locks only, and releases locks on rows that do not match the predicate once the statement has evaluated them. There are almost no gap locks, which reduces contention and deadlocks. The price is that phantoms can occur. At REPEATABLE READ you get the gap and next-key locks described above.

Seeing the locks

MySQL 8 exposes current locks in Performance Schema. Use it on a test system to confirm what a statement really locks instead of guessing.

Code
SELECT object_name, index_name, lock_type, lock_mode, lock_status, lock_data
FROM performance_schema.data_locks;

SELECT * FROM sys.innodb_lock_waits\G

Practical rules

  • Make sure every locking statement uses a selective index. Without one you lock more and wait longer.
  • Lock the smallest set of rows you need, by primary key where possible.
  • Keep transactions short so locks are released quickly.
  • If gap-lock contention is hurting, consider READ COMMITTED, after checking that the logic tolerates phantoms.

Takeaway

InnoDB locks index entries and the gaps between them. A unique-key equality lookup takes the smallest lock, ranges and non-unique lookups take next-key locks, and a query without an index may lock almost everything it scans. Check the real locks in Performance Schema and index accordingly.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.