Why MySQL Inserts Deadlock on Unique Indexes and How to Prevent Them
This article analyzes a MySQL InnoDB deadlock caused by concurrent batch inserts into a table with a composite unique index, explains the lock types involved, and offers practical strategies such as shortening transactions and lowering isolation levels to avoid similar deadlocks.
1. Business Background
Our data product aggregates real‑time and crawled data into a big‑data warehouse (ODPS). For fast reads the data are copied into a MySQL table with a composite unique index (cinema_id, show_id, now_date). During high‑TPS batch inserts the table suffered frequent deadlocks, which we investigated with DBA assistance.
2. Deadlock Situation
Table definition
CREATE TABLE tkn_tb_cinema_show_data (
...,
cinema_id BIGINT(20) DEFAULT NULL COMMENT 'cinema ID',
show_id BIGINT(20) DEFAULT NULL COMMENT 'movie ID',
now_date VARCHAR(32) DEFAULT NULL COMMENT 'date',
...,
PRIMARY KEY (id),
UNIQUE KEY uid_cinema_show_date (cinema_id,show_id,now_date)
) ENGINE=InnoDB AUTO_INCREMENT=2162973490 DEFAULT CHARSET=utf8 COMMENT='Taobao movie order statistics';Deadlock log excerpt
TRANSACTION 73278630816 ACTIVE 1 sec inserting
...
HOLDS THE LOCK(S): index uid_cinema_show_date ... lock_mode X
WAITING FOR THIS LOCK TO BE GRANTED: index uid_cinema_show_date ... lock_mode X locks gap before rec insert intention waiting
...
TRANSACTION 73278630826 ACTIVE 1 sec inserting
...
HOLDS THE LOCK(S): index uid_cinema_show_date ... lock_mode X
WAITING FOR THIS LOCK TO BE GRANTED: index uid_cinema_show_date ... lock_mode X locks gap before rec insert intention waiting3. Analysis
Both transactions hold X locks on the unique index and wait for an insert‑intention lock (IK) on the same gap, creating a classic deadlock cycle. InnoDB’s lock compatibility matrix shows that an X‑IK request is blocked by a preceding S‑NK lock, and after the insert the X‑RK lock remains.
The insert statements for the two transactions illustrate that the key ranges overlap, causing each transaction’s S‑NK to be blocked by the other’s X‑RK.
4. Prevention
To avoid such deadlocks:
Keep transactions short and batch them.
Use a lower isolation level (e.g., READ‑COMMITTED) to reduce the duration of S‑NK locks.
Ensure that insert operations acquire gap locks in a consistent order or redesign the unique index usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
