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.3.5.3, “InnoDB Lock Modes”.

InnoDB有几种行级锁类型, 包括 record 锁, gap 锁, next-key锁.

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

  • Record lock: 这是一个索引上的锁
  • 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.

  • Gap lock: 这是一个索引记录区间上的锁, 或者一个在第一条索引记录之前或最后一条索引记录之后的间隙上的锁
  • 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.

  • Next-key lock: 这是一个index的record lock和在这个index之前的间隙的gap lock的组合
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.3.5.13.2, “Clustered and Secondary Indexes”.

record locks 总是锁定索引记录, 即使一个表并没有定义索引.这种情况下, InnoDB创建了一个隐藏的clustered索引, 并用这个索引来进行record locking.

Next-key Locks

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

默认的, InnoDB工作于REPEATABLE READ事务隔离级别并且innodb_locks_unsafe_for_binlog系统变量是disabled的.在这种情况下, InnoDB使用next-key locks来查找和索引扫描, 这阻止了幻读的产生.

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.

Next-key locking 组合了 index-row locking 和 gap locking. InnoDB在搜索或者扫描表索引时使用了行级锁定, 他在遇到的索引记录上设置共享或排他锁. 因此, 行级锁实际上是索引记录锁.另外, 一个索引上的next-key锁也会影响index记录之前的"gap", 那就是, 一个 next-key锁是一个index-record lock 加上一个 gap lock, 它位于指定索引及索引之前的间隙, 形如 (a, b]. 如果一次护花拥有一个记录行R的索引的共享或排他锁, 另外一个会话将不能插入新索引行到间隙之间.

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:

假设索引包含10,11,13,20. 可能的next-key locks覆盖如下间隔, ()表示排除, []表示包含

(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.

对于最后一个间隔, next-key lock 锁定了大于最大值的所有间隙, 并且上界是一个假的大于任何已有值的值. 上界并不是一个真的索引记录, 所以, next-key lock只锁定了最大值以上的值.

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.

上面的next-key locking 例子展示了一个gap可能会跨越单个索引值, 多个索引值, 或者是空

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:

Gap locking在使用唯一索引来查找一个唯一行是不需要的.(这不包括查找中仅仅包含一些多列唯一索引的列的情况, 这种情况下, 间隙锁会发挥作用.) 例如, 如果id列有一个唯一索引, 下面的语句仅仅会使用index-record lock, 使用的范围是id值100, 并不会阻止其他会话在100之前插入记录

SELECT * FROM child WHERE id = 100;

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

如果id不是索引, 或者只有一个非唯一索引, 那么语句就会使用间隙锁.

A type of gap lock called an insertion 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. For more information about intention locks, seeSection 14.3.5.3, “InnoDB Lock Modes”.

有一种间隙锁叫插入意向间隙锁, 他是由INSERT语句对行的插入产生的. 这个锁表示了如果有多个事务打算插入记录到同一个索引间隙, 他们不需要彼此等待, 只要他们插入的位置不是同一个位置. 想象有索引记录4,7.有两个事务试图插入5,6, 他们都使用插入意向锁锁住了4,7之间的间隙来获取插入行的排他锁,但是他们并不会相互阻塞, 因为行之间并不会冲突.

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.

值得一提的是冲突的锁可以被不同的事务在同一个间隙中持有. 例如, 事务A可以在一个间隙上持有一个共享间隙锁, 同时事务B可以也可以在该间隙上持有一个排他间隙锁. 这种情形被允许的原因是如果一条记录从索引中清除了, 不同事务持有的这条记录上的gap锁必须被合并

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.

gap locks 在innodb中是"完全被抑制的", 这意味着他们仅仅阻止其他事务在间隙中执行插入操作.因此, 一个gap X-lock的效果和一个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. 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.

Gap locking是可以被显式禁用的. 你讲事务隔离级别设置为READ_COMMITTED或者启用innodb_locks_unsafe_for_binlog系统变量时就可以禁用它.在这种情况下, gap locking在查找和索引扫描的时候会被禁用, 仅仅在外键约束检查和重复键检查时被使用.

There are also other effects of using the READ COMMITTED isolation level or enablinginnodb_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 WHEREcondition of the UPDATE.

还有些别的副作用: 没有匹配行的Record locks在MYSQL评估完WHERE语句后会被释放.对于UPDATE, innodb做了一次"半一致"读, 它返回最后提交的版本给MYSQL, 这样MSYQL可以判断这一行是否匹配UPDATE的WHERE条件.

InnoDB Record, Gap, and Next-Key Locks的更多相关文章

  1. 转 MYSQL InnoDB Record, Gap, and Next-Key Locks

    http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html InnoDB has several types of re ...

  2. Mysql加锁过程详解(8)-理解innodb的锁(record,gap,Next-Key lock)

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  3. Locks Set by Different SQL Statements in InnoDB

    A locking read, an UPDATE, or a DELETE generally set record locks on every index record that is scan ...

  4. InnoDB锁问题

    InnoDB锁问题 InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我 ...

  5. MySQL 5.6 Reference Manual-14.3 InnoDB Transaction Model and Locking

    14.3 InnoDB Transaction Model and Locking 14.3.1 InnoDB Lock Modes 14.3.2 InnoDB Record, Gap, and Ne ...

  6. Innodb 锁的介绍

    如下博文是参考如下博文内容,再加整理. http://blog.chinaunix.net/uid-24111901-id-2627857.html http://blog.csdn.net/wang ...

  7. 实战演示疑惑 mysql insert到底加什么锁

    innodb的事务隔离级别是可重复读级别且innodb_locks_unsafe_for_binlog禁用,也就是说允许next-key lock 实验来自网上. ( 如果你没有演示出来,请check ...

  8. MySQL锁与MVCC

    --MySQL锁与MVCC --------------------2014/06/29 myisam表锁比较简单,这里主要讨论一下innodb的锁相关问题. innodb相比oracle锁机制简单许 ...

  9. 转 mysql Next-Key Locking

    原文:http://dev.mysql.com/doc/refman/5.5/en/innodb-next-key-locking.html 14.5.2.5 Avoiding the Phantom ...

随机推荐

  1. linux驱动之LCD(无framebuffer)

    <简介> a:什么是液晶 物质一般有三态,固态,气态,和液态.这只是一种比较大致的划分,但是有些物质介于液体和固体之间——液晶.一般固体的分子或原子都由固定的排列方式,但是液晶介于固体和液 ...

  2. SPFA算法 O(kE)

    主要思想是:     初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将其入队.直到队列为空时算法结束.     这个算法,简单的说就是队列优化 ...

  3. dubbox REST服务使用fastjson替换jackson

    上一节讲解了resteasy如何使用fastjson来替换默认的jackson,虽然dubbox内部采用的就是resteasy,但是大多数情况下,dubbox服务是一个独立的app,并不需要以war包 ...

  4. asp.net MVC 中 Session统一验证的方法

    验证登录状态的方法有:1  进程外Session   2 方法过滤器(建一个类继承ActionFilterAttribute)然后给需要验证的方法或控制器加特性标签 3 :新建一个BaseContro ...

  5. linux下授予某用户对某文件夹的读写权限

  6. python websocket-client connection

    参考:https://pypi.python.org/pypi/websocket-client/    https://www.cnblogs.com/saryli/p/6702260.html i ...

  7. .NET轻量级DBHelpers数据访问组件

    一.摘要 一说到ADO.NET大家可能立刻想到的就是增.删.改.查(CRUD)操作,然后再接就想到项目中的SQLHelper.没错本课分享课阿笨给大家带来的是来源于github上开源的DAO数据库访问 ...

  8. React和Vue特性和书写差异

    Vue均使用ES6语法,主要以单文件组件为例,写法上优先使用缩写. React使用TS语法. 生命周期 Vue React 入口&根实例 Vue const app = new Vue({ / ...

  9. 使用FTP发布和更新Windows Azure网站

    在Windows Azure中,FTP的用户名和密码与管理门户的用户名和密码不一样,需要另外设置. →依次点击左侧的"网站",网站名称,右侧的"设置部署凭据", ...

  10. CentOS 安装 Redis (高可用)

    原文:https://www.sunjianhua.cn/archives/centos-redis.html 下载地址: http://download.redis.io/releases/ 官方文 ...