首页 > 数据库 >[Mysql]next-key lock

[Mysql]next-key lock

时间:2024-07-17 20:08:50浏览次数:6  
标签:index lock next record key id

next-key lock

这一节我们通过实验验证next-key-lock在各种情况下的表现:

在唯一索引上的等值查询

我们利用主键展现这一特性

命中记录

image

下面我们来解释这张图发生了什么,

会话A 会话B
1 开启事务 开启事务
2 快照读整张表,查到所有数据
3 对id为10的数据进行加for update
4 尝试插入id为7,成功
5 尝试插入id为9,成功
6 尝试插入id为11,成功
7 尝试插入id为18,成功
8 尝试更新id为10,失败
9 快照读id为10,成功
10 当前读id为10,失败
  • 4-7成功说明这时候并没有加间隙锁
  • 8说明确实加了锁,不能更新
  • 9快照读成功,说明这个锁并不阻塞快照读
  • 10当前读失败,说明这锁阻塞当前读

没有命中记录

image

下面我们来解释这张图发生了什么,

会话A 会话B
1 开启事务 开启事务
2 快照读整张表,查到所有数据
3 对id为15(实际不存在)的数据进行加for update
4 插入id为12,失败
5 插入id为17,失败
6 更新id为11,成功
7 更细id为18,成功
  • 4-5说明确实加了间隙锁,不可以在(11,18这个范围内添加数据)
  • 6-7说明这个锁并没有锁住区间的两端,也就是说这个锁的范围是左开右开的区间

非唯一索引

官方文档

网上众说纷纭,每个人说的都不一样,还是直接看看官方文档吧

Next-Key Locks

A next-key lock is a combination of a record lock on the index record and a gap lock on the gap before the index record.

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. 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 a round bracket denotes exclusion of the interval endpoint and a square bracket denotes 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.

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 17.7.4, “Phantom Rows”).

Transaction data for a next-key lock appears similar to the following in SHOW ENGINE INNODB STATUS and InnoDB monitor output:

RECORD LOCKS space id 58 page no 3 n bits 72 index `PRIMARY` of table `test`.`t`
trx id 10080 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 8000000a; asc     ;;
 1: len 6; hex 00000000274f; asc     'O;;
 2: len 7; hex b60000019d0110; asc        ;;

幻影行


Phantom Rows
The so-called phantom problem occurs within a transaction when the same query produces different sets of rows at different times. For example, if a SELECT is executed twice, but returns a row the second time that was not returned the first time, the row is a “phantom” row.

Suppose that there is an index on the id column of the child table and that you want to read and lock all rows from the table having an identifier value larger than 100, with the intention of updating some column in the selected rows later:

SELECT * FROM child WHERE id > 100 FOR UPDATE;
The query scans the index starting from the first record where id is bigger than 100. Let the table contain rows having id values of 90 and 102. If the locks set on the index records in the scanned range do not lock out inserts made in the gaps (in this case, the gap between 90 and 102), another session can insert a new row into the table with an id of 101. If you were to execute the same SELECT within the same transaction, you would see a new row with an id of 101 (a “phantom”) in the result set returned by the query. If we regard a set of rows as a data item, the new phantom child would violate the isolation principle of transactions that a transaction should be able to run so that the data it has read does not change during the transaction.

To prevent phantoms, InnoDB uses an algorithm called next-key locking that 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 the 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.

When InnoDB scans an index, it can also lock the gap after the last record in the index. Just that happens in the preceding example: To prevent any insert into the table where id would be bigger than 100, the locks set by InnoDB include a lock on the gap following id value 102.

You can use next-key locking to implement a uniqueness check in your application: If you read your data in share mode and do not see a duplicate for a row you are going to insert, then you can safely insert your row and know that the next-key lock set on the successor of your row during the read prevents anyone meanwhile inserting a duplicate for your row. Thus, the next-key locking enables you to “lock” the nonexistence of something in your table.

Gap locking can be disabled as discussed in Section 17.7.1, “InnoDB Locking”. This may cause phantom problems because other sessions can insert new rows into the gaps when gap locking is disabled.

标签:index,lock,next,record,key,id
From: https://www.cnblogs.com/DCFV/p/18308188

相关文章

  • live555 rtsp服务器实战之doGetNextFrame
    live555关于RTSP协议交互流程live555的核心数据结构值之闭环双向链表live555rtsp服务器实战之createNewStreamSourcelive555rtsp服务器实战之doGetNextFrame概要live555用于实际项目开发时,createNewStreamSource和doGetNextFrame是必须要实现的两个虚函数,一般会创建两......
  • 高通Perflock
    高通的Perflock是Qualcomm公司开发的一项技术,用于优化设备性能和功耗管理。Perflock是一种锁定机制,允许操作系统或应用程序在需要时对处理器的性能状态进行控制,从而确保在关键任务或高性能需求的情况下,处理器能够维持在高性能状态。主要功能和特点性能锁定:Perflock允许应用程......
  • Keysight 是德 DSAX92004A 高性能示波器
    Keysight是德DSAX92004A高性能示波器DSAX92004AInfiniium高性能示波器:20GHzzui高带宽实时示波器20GHz真正模拟带宽,可升级为最高带宽:32GHz80GSa/s采样率,2通道;40GSa/s采样率,4通道zui深的存储深度――高达2Gpts存储器(每个通道具有50Mpts的标准深度)zui......
  • Keysight 是德 DSOX92004A 高性能示波器
    Keysight是德DSOX92004A高性能示波器DSOX92004AInfiniium高性能示波器:20GHzzui高带宽实时示波器20GHz真正模拟带宽,可升级为zui高带宽:32GHz80GSa/s采样率,2通道;40GSa/s采样率,4通道zui深的存储深度――高达2Gpts存储器,每通道20Mpts标准深度(DSA型号具有......
  • Keysight 是德 DSAX92804A 高性能示波器
    Keysight是德DSAX92804A高性能示波器DSAX92804ADSAX92804AInfiniium高性能示波器:28GHz高带宽实时示波器28GHz真正模拟带宽,可升级为zui高带宽:32GHz80GSa/s采样率,2通道;40GSa/s采样率,4通道zui深的存储深度――高达2Gpts存储器(每通道50Mpts标准深度)zui......
  • Keysight 是德 DSAX92504A 高性能示波器
    Keysight是德DSAX92504A高性能示波器DSAX92504AInfiniium高性能示波器:25GHz高带宽实时示波器25GHz真正模拟带宽,可升级为zui高带宽:32GHz80GSa/s采样率,2通道;40GSa/s采样率,4通道zui深的存储深度――高达2Gpts存储器(每通道50Mpts标准深度)zui高的实时示波......
  • DedeCMS网站模板的title、description、keywords应该怎么写?
    首页​<title>{dede:global.cfg_webname/}-{dede:global.cfg_websubtitle/}</title><metaname="description"content="{dede:global.cfg_description/}"><metaname="keywords"content="{dede:global.cfg_k......
  • 深入理解 Vue.js 中的 nextTick:原理与应用
    深入理解Vue.js中的nextTick:原理与应用在使用Vue.js开发复杂的前端应用时,你可能会遇到这样一种情况:你希望在数据更新后立即执行某些操作,但发现DOM并没有如预期那样立即更新。这时,nextTick就派上用场了。本文将深入探讨nextTick的原理、应用场景以及一些实用的代码示例......
  • Monkey 01 lexer 词法分析器
    此处可以下载每章代码https://interpreterbook.com/waiig_code_1.7.zip 首先,词法分析器是`输入字符串,输出词法单元token`.要定义词法分析器,首先要定义tokentoken具有两个属性,一个是token的类型,另一个是token的字面量或者说能打印出来的值//token/token.gopackagetokentyp......
  • Linux 报错INFO: task blocked for more than 120 seconds
     一般情况下,Linux会把可用内存的40%的空间作为文件系统的缓存。 当缓存快满时,文件系统将缓存中的数据整体同步到磁盘中。但是系统对同步时间有最大120秒的限制。 如果文件系统不能在时间限制之内完成数据同步,则会发生上述的错误。 这通常发生在内存很大的系统上。系统......