Quick revision
The short answer
Without an index on status or created_at, InnoDB has to scan the whole clustered index to find the matching rows, and under REPEATABLE READ it puts a next-key lock on every row it examines, not only on the few it changes. Next-key locks also cover the gaps between rows, so inserts anywhere in the table wait, and the UPDATE behaves almost like a table lock. The fix is an index that matches the predicate, such as (status, created_at), so only the matching range is locked; READ COMMITTED and batching reduce the blocking further.
What to remember
In this answer, we saw that InnoDB locks index entries it examines while searching, and that under REPEATABLE READ those next-key locks cover rows and gaps until commit. Without an index, the UPDATE examines every row, so it locks every gap and blocks all inserts. An index on (status, created_at) limits the locks to the matching range, READ COMMITTED releases non-matching rows and drops gap locks at the cost of weaker guarantees, and batching by primary key keeps each lock short. performance_schema.data_locks shows the evidence before and after.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Without an index on status or created_at, InnoDB has to scan the whole clustered index to find the matching rows, and under REPEATABLE READ it puts a next-key lock on every row it examines, not only on the few it changes. Next-key locks also cover the gaps between rows, so inserts anywhere in the table wait, and the UPDATE behaves almost like a table lock. The fix is an index that matches the predicate, such as (status, created_at), so only the matching range is locked; READ COMMITTED and batching reduce the blocking further.
Why this question is asked
This is a classic production surprise: a maintenance UPDATE that touches twenty rows freezes the checkout flow for a minute. Interviewers ask it because the answer shows whether we know that InnoDB locks what it reads while searching, not just what it writes, and that an index is a locking decision as much as a speed decision. In this answer, we will recap how InnoDB row locks and gap locks work, follow the UPDATE through a full scan, see why new orders cannot be inserted, look at how to prove it with performance_schema, and then go through the fixes: an index, READ COMMITTED, and batching by primary key.
1A Quick Recap of InnoDB Row Locks
InnoDB, the default MySQL storage engine, does not lock rows as separate objects. It locks index records. Every InnoDB table is stored as a clustered index, a B-tree ordered by the primary key whose leaf pages hold the full rows, so locking a row really means locking its entry in that index.
There are three kinds of record lock worth knowing here. A record lock locks one index entry. A gap lock locks the space between two entries, so nobody can insert a new entry there. A next-key lock is the two together: the record plus the gap before it. Under REPEATABLE READ, the default isolation level in MySQL 8.4, locking reads and the search part of UPDATE and DELETE use next-key locks. The reason is phantom protection: if our transaction runs the same search again, no new row may appear in the range it has already read.
One more detail matters. InnoDB locks the entries it examines while evaluating the WHERE clause, because at the storage level it has to lock a row before it can be sure the row is the latest version. Rows that turn out not to match are still locked under REPEATABLE READ until the transaction ends.
2Following the UPDATE Through a Full Scan
Our statement filters on created_at and status, and neither column is indexed. The optimizer has no way to jump to the matching rows, so it reads the clustered index from the first entry to the last. For each entry it takes an exclusive next-key lock, checks the condition, and changes the row only if it matches.
Suppose orders has 2 million rows and 30 of them are OPEN orders from 2025. The statement changes 30 rows but locks 2 million entries and every gap between them, including the gap after the last row, which InnoDB represents with a special supremum record. New orders get higher primary keys, so their insert lands in that final gap. The insert asks for an insert intention lock on the gap, sees our next-key lock, and waits.
- No usable indexthe optimizer chooses a full scan of the clustered index
- Lock the entryexclusive next-key lock: the record plus the gap before it
- Row matches the WHERE?Yeschange it, keep the lockNokeep the lock anyway under REPEATABLE READ
- Move to the next entryrepeat until the supremum record
- Inserts blockedevery gap is locked, so insert intention locks wait
5 steps in order. Play them, or step through them one at a time.
Under REPEATABLE READ the locks taken during the search stay until commit.
Notice that session 2 is inserting a row that has nothing to do with the update. It waits only because the scan locked the whole index, including the space where its new row belongs. So in practice the statement acts like a table lock for writers, even though it is technically a set of row and gap locks.
3Proving It With performance_schema
Before we change anything we should look at the locks. MySQL 8 exposes them in performance_schema.data_locks, one row per lock, and performance_schema.data_lock_waits, one row per blocked request together with the request that blocks it. The sys.innodb_lock_waits view joins these into a readable form.
A lock count close to the row count on PRIMARY is the fingerprint of a scan. The lock_mode column shows X for next-key locks, X,REC_NOT_GAP for record-only locks and X,GAP for gap-only locks, so we can also see which kind each lock is.
4Fix 1: An Index That Matches the Predicate
The real fix is to let InnoDB find the matching rows without touching the others. An index on (status, created_at) puts all OPEN rows together, ordered by date, so the search becomes a range scan over the entries ('OPEN', anything before 2026-01-01). InnoDB locks only those entries, the gaps inside that range and the one entry just after it, plus the clustered index records of the 30 matching rows.
Now a new OPEN order with today's date belongs at ('OPEN', 2026-11-05), far after the locked range, so its insert goes through at once. The column order matters: status first because it is compared with equality, created_at second because it is a range. With (created_at, status), the range on the first column would scan every row before 2026-01-01, whatever its status, and lock all of them.
5Fix 2: READ COMMITTED, and What It Costs
Under READ COMMITTED, InnoDB behaves differently in two useful ways. It does not take gap locks for ordinary searches, only record locks (gap locks remain for foreign key and duplicate-key checks). And for an UPDATE it uses a so-called semi-consistent read: when it finds that a row does not match the WHERE clause, it releases that row's lock immediately instead of keeping it until commit. So even the full scan would end up holding locks only on the 30 changed rows.
The trade-offs are real, though. READ COMMITTED allows non-repeatable reads and phantoms inside a transaction, so code that reads the same range twice may see different results. It also requires row-based binary logging for replication, which is the default since MySQL 8.0 but worth confirming. We can set it for one session or transaction (SET TRANSACTION ISOLATION LEVEL READ COMMITTED) for this maintenance job instead of changing the whole server, and we should still add the index, because a full scan is slow regardless of locks.
6Fix 3: Batching by Primary Key Range
For large maintenance updates, the last improvement is to work in small slices of the primary key, each in its own short transaction. Each batch locks only its own slice and commits within milliseconds, so other sessions wait at most for one batch, and a failure rolls back only one slice.
Because the range is on the primary key, InnoDB locks only that part of the clustered index, and inserts of new orders, which go to the end of the key range, never meet a lock until the last batch. Combined with the index, the job becomes invisible to the online system.
7Follow-up questions
Why can batching the update by primary key ranges help further?
Batching limits both how many rows are locked at once and how long they stay locked. Each batch is a short transaction over a slice of the clustered index, so other sessions wait for one small slice for a few milliseconds instead of the whole table for the whole job. It also keeps undo log growth small, makes progress resumable after a failure, and lets us pause between batches if replication lag grows.
Does the index have to include both columns?
An index on status alone already narrows the search to the OPEN rows, which may be enough if there are few of them. Including created_at as the second column narrows it to exactly the rows we change, so fewer entries are locked and fewer rows are read. If most orders are OPEN, an index on status alone would still lock almost the whole table.
Would SELECT ... FOR UPDATE with the same WHERE clause behave the same way?
Yes. Locking reads, UPDATE and DELETE all lock the entries they examine during the search. A plain SELECT is different: it reads a consistent snapshot through MVCC and takes no row locks, so it would not block inserts at all.
8Conclusion
In this answer, we saw that InnoDB locks index entries it examines while searching, and that under REPEATABLE READ those next-key locks cover rows and gaps until commit. Without an index, the UPDATE examines every row, so it locks every gap and blocks all inserts. An index on (status, created_at) limits the locks to the matching range, READ COMMITTED releases non-matching rows and drops gap locks at the cost of weaker guarantees, and batching by primary key keeps each lock short. performance_schema.data_locks shows the evidence before and after.
Open with
Because there is no index on status or created_at, InnoDB has to scan the whole clustered index, and under REPEATABLE READ it takes a next-key lock on every row it examines, not just the thirty it changes.
Then, if they want more
Next-key locks include the gap before each record, and the scan also locks the gap after the last row, so any new order insert asks for an insert intention lock on a locked gap and waits. In effect it's a table lock.
I'd confirm it in performance_schema.data_locks, where I'd see roughly one X lock per row on PRIMARY, and sys.innodb_lock_waits would show the insert waiting on the update.
The main fix is an index on status then created_at, equality column first, so the update does a range scan and locks only the matching entries and the next one; new orders land far outside that range.
For this job I could also run it at READ COMMITTED, where InnoDB drops gap locks for searches and releases locks on rows that don't match, but that weakens repeatable reads and needs row-based binlogging, so I'd use it per session rather than globally.
Finally I'd run the update in primary key batches of a few thousand rows with a commit each, so locks are held for milliseconds.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8, MySQL 8.0, MySQL 8.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In MySQL 8.4 with REPEATABLE READ, this statement blocks inserts of new orders by other...": https://scrivio-interview-library.netlify.app/databases/database-locking/603169c44c/