当前位置:网站首页>MySQL gap lock
MySQL gap lock
2022-07-01 12:52:00 【Tang Bohu points mosquito repellent incense DW】
Study Mysql, There will always be a mountain that can't go around , That's the lock . Theoretically , There are more patterns in the lock , It can't go beyond the categories mentioned in the operating system class , however Mysql The lock tipped me over .
stay Mysql The granularity of middle locks can be divided into : Table lock , Row-level locks , Clearance lock Three . Table level lock and row level lock are not too difficult to understand . Only clearance lock, I can't understand its design intention accurately , And the phenomenon I've tested makes me feel weird .
So why is there a gap lock , According to most of the information available , Clearance lock is introduced to solve the problem of RR The problem of unreal reading at isolation level .
Here's an example , First create a Table:
Create Table: CREATE TABLE `foo` (
`uid` int(11) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`uid`),
KEY `age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into foo values(1,1),(4,4),(7,7),(9,9);
then , Open two mysql client (M1,M2), The order of execution is as follows :
M1: begin;
M1: select * from foo were uid > 1 and uid < 5 for update;
M2: begin;insert into foo values(2,2);commit;
M1: select * from foo were uid > 1 and uid < 5 for update;
M1: commit;
If in M1 First execution select Statement, only add line lock , Then all that's locked is uid=4 This business . stay M1 Second execution select When the sentence is , because M2 Insert a (2,2), So we will find one more (2,2) The record of . This will produce unreal reading .
mysql The solution is : Use clearance lock , take uid In the gap interval (1,4),(4,7) All locks , So when M2 stay insert Row data (2,2) even to the extent that (6,6) Will be blocked by lock to prevent M1 Phantom reading .
If it comes to a perfect end , Then I won't roll over , Look at the other three sql sentence :
M1: begin;
M1: select * from foo were age = 4 for update;
M2: begin;insert into foo values(6,6);commit;
M1: select * from foo were age = 4 for update;
M1: commit;
If you do it manually, you'll find M2 It will be blocked by a lock , It's because he's interested in age With a clearance lock ( Locks are added to the index ).
because Locks are added to the index , According to my first reaction , Direct pair age=4 This index lock solves the problem , Why do you need a clearance lock ?
I checked for a long time , Just found a very important point that few people mentioned Primary key stored in secondary index , Will participate in secondary index sorting , such as age When the index is sorted , Actually, it's (age,uid) To sort . And the reason why I use uid I think most of the reasons for participating in the ranking should be B+ The same value is not allowed to be stored in the tree . Use age,uid After splicing, all secondary indexes can be guaranteed , stay B+ The value in the tree must be unique .
let me put it another way , We can't just lock age=4 This condition , Because there might be (age,uid)= (4,1)/(4,2)/(4,5) Wait for any index .
Secondary index in stitching , because age before uid After , therefore age To some extent, the value of represents the entire index value . That's why the clearance lock can be locked age=4 This condition .
In order to verify the correctness of the above statement , Let's look at this sql:
M1: begin;
M1: select * from foo were age = 4 for update;
M2: begin;update foo set age = 2 where uid = 1;commit;
M1: select * from foo were age = 4 for update;
M1: commit;
Let's start with a brief analysis : 1. age Is not unique secondary index 2. The secondary index is implemented internally by age,uid After splicing, they participate in sorting 3. The gap is locked (age,uid) = (1,1) ~ (4,4) The open range of 4. M2 The statement executed is to insert a secondary index value (2,1)
According to the clearance lock principle , We can push out M2 It will be blocked by the clearance lock , And that's exactly what happened .
ps. The primary key stored in the secondary index will participate in the secondary index sorting , I think this is very important . I don't know why many reference books have passed by intentionally or unintentionally .
------------------------------------------------------------------------------------------------------------------------------
summary :
Mysql MVCC It's solved RR The readability of transactions , Using clearance lock solves RR Level unreal reading problem
Mysql The gap lock is for RR Level is introduced to solve the problem of unreal reading , The clearance lock is gap lock , and mysql It uses clearance lock and gap Lock combination , That is to say next-key lock, On different indexes ,mysql The way of locking is also different :
On the unique index : If the condition is =5 , Gap lock degenerates into row lock , That is, only the row of objects in the condition will be locked , If it is >5, A... Will be added [5, ∞) One of the next-key lock ,5 This row lock and (5,∞) This clearance lock
On the general index : If the condition is 5, that mysql Will pass 5 Query a gap on the left and right , That is to say, bi 5 Small first value and ratio 5 The big first value , Then add a clearance lock , For example, the index value of two pieces of data in the database is 3 and 7, that mysql One will be added (3-5)[5](5-7] Such a gap lock , Why do ordinary indexes need to be added like this , That's because ordinary indexes can be repeated , Here is a previous sentence
Primary key stored in secondary index , Will participate in secondary index sorting , such as age When the index is sorted , Actually, it's (age,uid) To sort . And the reason why I use uid I think most of the reasons for participating in the ranking should be B+ The same value is not allowed to be stored in the tree . Use age,uid After splicing, all secondary indexes can be guaranteed , stay B+ The value in the tree must be unique .
let me put it another way , We can't just lock age=4 This condition , Because there might be (age,uid)= (4,1)/(4,2)/(4,5) Wait for any index .
Secondary index in stitching , because age before uid After , therefore age To some extent, the value of represents the entire index value . That's why the clearance lock can be locked age=4 This condition .
The clearance lock is an interval that opens on the left and closes on the right , Take the example above , Equivalent query where c = 5, Then one will be added (3,7] A left open right close clearance lock , If we insert c=3 A record of is not blocked , But if we insert one c=7 The record of , It will block
No index : because mysql All locks are on the index , If there is no index, the table lock is used
边栏推荐
- Like the three foot platform
- What are the solutions for session sharing of highly paid programmers & interview questions series 118?
- 数字化转型再下一城,数字孪生厂商优锘科技宣布完成超3亿元融资
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- 题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
- 数据库之MHA高可用集群部署及故障切换
- Operations related to sequence table
- Wechat simulated geographical location_ Camouflage wechat location
- Flinkcdc should extract Oracle in real time. What should be configured for oracle?
- Idea of [developing killer]
猜你喜欢

Manage nodejs with NVM (downgrade the high version to the low version)
![Idea of [developing killer]](/img/74/f4a18afd2b86373996e4ca62b50f88.png)
Idea of [developing killer]

Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures

Circular linked list--
![[encounter Django] - (II) database configuration](/img/13/9512c1e03349092874055771c3433d.png)
[encounter Django] - (II) database configuration
![[brain opening] west tide and going to the world series](/img/b2/444af296e170d19629800b3d4c50fa.jpg)
[brain opening] west tide and going to the world series

软件测试中功能测试流程

mysql统计账单信息(下):数据导入及查询

我选的热门专业,四年后成了“天坑”

华为HMS Core携手超图为三维GIS注入新动能
随机推荐
阿霍的三个阶段
VM虚拟机配置动态ip和静态ip访问
手机便签应用
logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
Project deployment is not difficult at all!
Vs code setting Click to open a new file window without overwriting the previous window
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
类的初始化与实例化
Stack-------
华为HMS Core携手超图为三维GIS注入新动能
Chained storage of queues
Perl 5.10.0 installation package download
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
路由基础之OSPF LSA详细讲解
nexus搭建npm依赖私库
声明一个抽象类Vehicle,它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。子类Mot
How to play with the reading and writing operations of blocking sockets?
Interpretation of hard threshold function [easy to understand]
PG基础篇--逻辑结构管理(触发器)