先帖一段官方的解释: A next-key lock on an index record also affects the “ gap ” before that index record. That is, a next-key lock is an index-record lock plus a gap lock on the gap preceding the index record. If one session has a shared or exclusive lock on record R in an index, another session cannot insert a new index record in the gap immediately before R in the index order.
假设我有一张表有一些数据 1,3,5,8,11 且具有非唯一索引,并且在 repeatable read 级别下执行:
select * from table where col = 8 for update;
此时会锁住 8 这条记录,另外的 gap 会锁住哪个范围?实际测试其实会锁住(5,8) 和 (8,11)这两个 gap 。但是根据官方手册里,不是before, preceding the index record 吗?( 8 , 11 )这个 gap 哪来的啊?已经纠结很久这个问题了,希望哪位大神帮忙解答下,不胜感激啊 :D