`

mysql innodb next-key locking(小翻译)

阅读更多

InnoDB has several types of record-level locks:

    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 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 13.2.10.1, “Clustered and Secondary Indexes”.

By default, InnoDB operates in REPEATABLE READ transaction isolation level and with the innodb_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 13.2.8.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 (也就是说,next-key是一个索引记录锁和加在gap上的gap锁的组合) . 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
(如果一个session 在R记录的索引上 获得一个共享或互斥锁,那么另一个session不能以索引顺序立即在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.

The preceding example 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:
(如果一个语句用了唯一索引那么间隙锁是不需要的(当然对几列组成的唯一索引是不适用的)例如,如果 id这一列有一个唯一索引,那么下面的查询只用 index-record lock 锁住id为100的行,而是否有另一个session要往该行前的间隙插入一条新纪录是没有关系的,是可以的)
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 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.
(一种叫做insertion gap lock 的锁是用做插入操作前 插入行(这句怎么翻译 )????这种锁信号以这种方式插入:当很多事务同时插入到同一个索引的间隙中时,只要这些插入不是要插入到同一位置就不用互相等待。假设有值为4和7的索引纪录,当几个事务要在这之间插入5和6时分别用insertion gap lock锁住4和7之间的间隙,取得插入的行的排斥锁,但是不会互相阻塞,因为这些插入行之间是没有冲突的。)
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.

There is also another effect 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.

英文部分摘自:http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html

分享到:
评论

相关推荐

    MySQL innodb 技术内幕

    InnoDB 存储引擎通过使用 MVCC 来获取高并发性,并且实现 SQL 标准的 4 种隔离级别,同时使用一种被称为 next-key locking 的策略来避免幻读现象。 ### 1.3.2 MyISAM 存储引擎 MyISAM 存储引擎是不支持事务的存储...

    03-MySQL逻辑架构和Innodb存储引擎1

    5. **Next-Key Locking**:InnoDB为了解决幻读问题,使用Next-Key Locking策略,这涉及到记录锁和间隙锁。 6. **插入缓冲(Insert Buffer)**:用于优化非唯一索引的插入操作。 7. **二次写(Double Write)**:...

    MySQL存储引擎InnoDB的配置与使用的讲解

    同时,使用一种被称为next-key locking的策略来避免幻读(phantom)现象的产生。除此之外,InnoDB存储引擎还提供了插入缓冲(insert buffer)、二次读写(double write)、自适应哈希索引(adaptive hash

    MySQL锁的详细介绍

    * select … from lock in share mode 语句:追加了共享锁,InnoDB 会使用 Next-Key Lock 锁进行处理,如果扫描发现唯一索引,可以降级为 RecordLock 锁。 * select … from for update 语句:追加了排他锁,InnoDB ...

    面试宝典MySql.txt

    MyISAM 存储引擎 MyISAM 是 MySQL 官方提供默认的存储引擎,其特点是不支持事务、表锁和全文索引,对于一些 OLAP(联机 分析处理)系统,操作速度快。...称为 next-key locking 的策略避免幻读。

    MySQL新特性 For innodb 8.0

    4. **Enhanced Locking**: 在InnoDB 8.0中,行级锁定机制得到了增强,引入了“Next-Key Locks”的优化,减少死锁的发生。此外,也提供了更精细的加锁选项,比如Gap Locks和Record Locks,以便更好地控制并发访问。 ...

    innodb锁

    **innodb锁**是MySQL数据库InnoDB存储引擎中的一种关键机制,用于管理并发事务并确保数据的一致性和完整性。在多用户环境下,InnoDB通过锁来实现事务的隔离,防止脏读、不可重复读和幻读等并发问题。这篇博客文章...

    2021面试题总结MySQL篇.pdf

    - **解决方案**:InnoDB 存储引擎采用 Next-Key Locking 机制来避免幻读。 #### 4. 串行化(Serializable) - **定义**:最高级别的隔离级别,确保所有 SQL 操作按顺序执行,彻底解决脏读、不可重复读及幻读等问题...

    阿里大牛何sir 深入MySQL加锁处理分析

    - 对于可重复读(RR)隔离级别,InnoDB使用一种称为“next-key lock”的锁定方式,它是一种结合了记录锁(Record Lock)和间隙锁(Gap Lock)的锁,用于防止幻读。 复杂的SQL语句,尤其是涉及多个表的JOIN操作,其...

    MySQL数据库面试宝典1.pdf

    InnoDB使用了多种机制来实现行锁,包括自旋锁、自适应哈希索引、Next-Key Locking等。 **6.4 什么是死锁?怎么解决?** - **死锁**:两个或多个事务互相等待对方释放资源的情况。 - **解决方法**:设置超时机制、...

    11 并发访问控制_theory_MYSQL_

    - 清理过期记录的流程称为自增ID清理(Next-Key Locking)和Change Buffer。 6. **并发控制的优化**: - 适当调整事务隔离级别,平衡并发性和一致性需求。 - 使用更细粒度的锁,如行级锁,而非表级锁。 - 优化...

    MySQL 开发规范

    - **Next-Key Lock(下一个键锁)** - **特点**:同时锁定记录本身及其后的空白区域。 - **示例**:锁定索引10及其后的空白区域。 **4.3 共享锁(S-Lock)与排他锁(X-Lock)** - **共享锁**:允许多个事务读取同一行...

    MySQL中InnoDB存储引擎的锁的基本使用教程

    MySQL中的InnoDB存储引擎在处理并发事务时采用了行级锁(row-level locking)机制,这是它相较于其他如MyISAM(表级锁)和MEMORY(表级锁或页面锁)的一大优势,因为它提供了更高的并发性能。行级锁分为共享锁(S ...

    浅谈选择mysql存储引擎的标准

    - 默认的可重复读(REPEATABLE READ)隔离级别,通过间隙锁(next-key locking)防止幻读。 - 数据和索引存储在单独的文件中,便于备份、恢复和跨平台迁移。 - 支持热备份,这是其他引擎不具备的特性。 - 主键...

    mysql锁解决并发问题共7页.pdf.zip

    9. **Next-Key Locks**:是InnoDB存储引擎中的行锁与间隙锁的组合,锁定指定行以及行之间的间隙。 10. **非阻塞锁(Lock-Free)**:通过无锁编程技术,避免锁的使用,提高并发性能,但实现复杂,适用于特定场景。 ...

Global site tag (gtag.js) - Google Analytics