You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求澄清MySQL官方文档中间隙锁的冲突持有及特性相关内容

Clarifying InnoDB Gap Lock Behavior in MySQL

Alright, let's break down this key (and often confusing) detail about InnoDB gap locks straight from MySQL's official documentation:

不同事务可在同一间隙持有冲突锁。例如,事务A可在某间隙持有间隙共享锁(gap S-lock),而事务B可在同一间隙持有间隙排他锁(gap X-lock)

The core reason this works is that InnoDB gap locks are "purely inhibitory"—their sole purpose is to block other transactions from inserting new rows into the targeted gap. They don't prevent other transactions from acquiring any gap lock (even conflicting-sounding ones like gap S-locks and gap X-locks) on the same gap.

Let's break this down with practical takeaways:

  • Unlike row-level locks (where an X-lock blocks S-locks and vice versa on the same row), gap locks don't compete with each other for access to the gap.
  • A gap X-lock doesn't block another transaction from grabbing a gap S-lock on the same space, and vice versa.
  • The only universal rule for gap locks: no transaction can insert a new row into the gap while any gap lock (S or X) is held on it.

For example: If Transaction A holds a gap S-lock on the gap between rows with IDs 5 and 10, Transaction B can still acquire a gap X-lock on that exact same gap. Neither transaction blocks the other, but both will stop any third transaction from inserting a row with an ID between 5 and 10 until their respective locks are released.

内容的提问来源于stack exchange,提问作者Alex Love

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:47:54