文档章节

InnoDB Record, Gap, and Next-Key Locks

秋风醉了
 秋风醉了
发布于 2015/11/13 19:52
字数 890
阅读 120
收藏 0

InnoDB Record, Gap, and Next-Key Locks

http://dev.mysql.com/doc/refman/5.7/en/innodb-record-level-locks.html

InnoDB has several types of record-level locks including record locks, gap locks, and next-key locks. 

For information about shared locks, exclusive locks, and intention locks, see Section 14.2.2.1, “InnoDB Lock Modes”.

Record lock: This is a lock on an index record.

Gap lock: This is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.

Next-key lock: This is a combination of a record lock on the index record and a gap lock on the gap before the index record.


Record Locks

Record locks always lock index records, even if a table is defined with no indexes. For such cases, InnoDB creates a hidden clustered index and uses this index for record locking. See Section 14.2.7.2, “Clustered and Secondary Indexes”.


Next-key Locks

By default, InnoDB operates in REPEATABLE READ transaction isolation level. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows (see Section 14.2.2.5, “Avoiding the Phantom Problem Using Next-Key Locking”).


Next-key locking combines index-row locking with gap locking. InnoDB performs row-level locking in such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index records it encounters. Thus, the row-level locks are actually index-record locks. In addition, 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.


Suppose that an index contains the values 10, 11, 13, and 20. The possible next-key locks for this index cover the following intervals, where ( or ) denote exclusion of the interval endpoint and [ or ] denote inclusion of the endpoint:

(negative infinity, 10]

(10, 11]

(11, 13]

(13, 20]

(20, positive infinity)

For the last interval, the next-key lock locks the gap above the largest value in the index and the “supremum” pseudo-record having a value higher than any value actually in the index. The supremum is not a real index record, so, in effect, this next-key lock locks only the gap following the largest index value.


Gap Locks

The next-key locking example in the previous section shows that a gap might span a single index value, multiple index values, or even be empty.


Gap locking is not needed for statements that lock rows using a unique index to search for a unique row. (This does not include the case that the search condition includes only some columns of a multiple-column unique index; in that case, gap locking does occur.) For example, if the id column has a unique index, the following statement uses only an index-record lock for the row having id value 100 and it does not matter whether other sessions insert rows in the preceding gap:

SELECT * FROM child WHERE id = 100;

If id is not indexed or has a nonunique index, the statement does lock the preceding gap.


A type of gap lock called an insert intention gap lock is set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap. Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6 each lock the gap between 4 and 7 with insert intention locks prior to obtaining the exclusive lock on the inserted row, but do not block each other because the rows are nonconflicting.


It is also worth noting here that conflicting locks can be held on a gap by different transactions. For example, transaction A can hold a shared gap lock (gap S-lock) on a gap while transaction B holds an exclusive gap lock (gap X-lock) on the same gap. The reason conflicting gap locks are allowed is that if a record is purged from an index, the gap locks held on the record by different transactions must be merged.


Gap locks in InnoDB are “purely inhibitive”, which means they only stop other transactions from inserting to the gap. Thus, a gap X-lock has the same effect as a gap S-lock.


Disabling Gap Locking

Gap locking can be disabled explicitly. This occurs if you change the transaction isolation level to READ COMMITTED or enable the innodb_locks_unsafe_for_binlog system variable (which is now deprecated). Under these circumstances, gap locking is disabled for searches and index scans and is used only for foreign-key constraint checking and duplicate-key checking.


There are also other effects of using the READ COMMITTED isolation level or enabling innodb_locks_unsafe_for_binlog: Record locks for nonmatching rows are released after MySQL has evaluated the WHERE condition. For UPDATE statements, InnoDB does a “semi-consistent” read, such that it returns the latest committed version to MySQL so that MySQL can determine whether the row matches the WHERE condition of the UPDATE.

============END============

© 著作权归作者所有

上一篇: MySQL锁的调试
下一篇: InnoDB Lock Modes
秋风醉了
粉丝 253
博文 532
码字总数 405755
作品 0
朝阳
程序员
私信 提问
加载中

评论(0)

MySQL InnoDB锁介绍及不同SQL语句分别加什么样的锁

(给ImportNew加星标,提高Java技能) 作者:iceman1952(本文来自作者投稿) 本文中,我们详细介绍MySQL InnoDB存储引擎各种不同类型的锁,以及不同SQL语句分别会加什么样的锁。 阅读提示 ...

ImportNew
2019/01/28
0
0
MySQL 5.5 InnoDB 锁状态解析

目录 目录 一 引子 二 表结构说明 三 INNODB 锁等待模拟 3.1 创建测试表,录入测试数据 3.2 模拟锁等待 3.3 再次模拟锁等待 3.4 查询锁等待 3.4.1 直接查看 innodblockwaits 表 3.4.2 innodb...

java_龙
2018/11/30
41
0
【 58沈剑 架构师之路】InnoDB七种锁——记录锁、间隙锁、临键锁

MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一。 但是,如《InnoDB,5项最佳实践》所述,如果查询没有命中索引,也将退化为表锁。 InnoDB的细粒度锁,是实现在索引记录上的。 一,Inn...

张锦飞
2018/12/07
2.7K
0
InnoDB Lock Modes

InnoDB Lock Modes http://dev.mysql.com/doc/refman/5.7/en/innodb-lock-modes.html InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks......

秋风醉了
2015/11/13
53
0
【58沈剑 架构师之路】InnoDB,select为啥会阻塞insert?

MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一。 但是,如《InnoDB,5项最佳实践》所述,如果查询没有命中索引,也将退化为表锁。 InnoDB的细粒度锁,是实现在索引记录上的。 一,Inn...

张锦飞
2018/12/11
182
0

没有更多内容

加载失败,请刷新页面

加载更多

UGUI图片层级和渲染顺序的奇怪关系

之前见别人的文章总是说,在Hierachy下,相同图集的图片要连续排列,这样Unity会对相同图集的图片进行合批,从而减少draw call。今天做了简单的试验发现情况并不是这么简单的。 第一种情况:...

myctrd
45分钟前
48
0
jQuery中$.each()方法的使用-Json对象-dom元素

对于循环我们首先会想到for循环,但是在前端对数组我们可以使用,但是对于json对象,想把对象中的属性的key-value循环去取出,那么for循环提供不了的。而each方法则给我们提供了便利,下面介...

imzchloe
55分钟前
59
0
Linuxprobe第六天

待补充

nt狮子男人
56分钟前
58
0
gem install:无法构建gem native扩展(找不到头文件)

我正在使用Fedora 14,我安装并运行了MySQL和MySQL服务器5.1.42。 现在我尝试以root用户身份执行此操作: gem install mysql 但我得到这个错误: Building native extensions. This could ...

技术盛宴
今天
51
0
就8张图片带你搞清楚JS的原型链

JS(JavaScript)是目前互联网开发中十分重要的一门编程语言,他承载着网页、手机应用程序、硬件程序、微信、微信小程序中的各种特效及处理逻辑功能。

涂老师
今天
52
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部