当前位置:网站首页>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
- Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
- Application of stack -- bracket matching problem
- 【脑洞大开】《西潮》及《走向世界丛书》
- Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
- Huawei interview question: Recruitment
- Which securities company has a low, safe and reliable account opening commission
- Simple Fibonacci (recursive)
- Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
猜你喜欢
我花上万学带货:3天赚3元,成交靠刷单
【脑洞大开】《西潮》及《走向世界丛书》
基于开源流批一体数据同步引擎 ChunJun 数据还原 —DDL 解析模块的实战分享
Manage nodejs with NVM (downgrade the high version to the low version)
79. 单词搜索【dfs + 回溯visit + 遍历起点】
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
基因检测,如何帮助患者对抗疾病?
路由基础之OSPF LSA详细讲解
79. Word search [DFS + backtracking visit + traversal starting point]
Operator-1 first acquaintance with operator
随机推荐
leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
leetcode:241. Design priority for operation expression [DFS + Eval]
codeforces -- 4B. Before an Exam
题目 1004: 母牛的故事(递推)
There are risks in trading
Wechat applet - 80 practical examples of wechat applet projects
Three stages of aho
Flinkcdc should extract Oracle in real time. What should be configured for oracle?
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
Queue operation---
【邂逅Django】——(二)数据库配置
華為面試題: 招聘
逆向调试入门-PE结构-输入表输出表05/07
Wang Xing's infinite game ushers in the "ultimate" battle
Blocking sockets的读写操作该怎么玩?
Ikvm of toolbox Net project new progress
shell脚本导入存储过程到数据库
我选的热门专业,四年后成了“天坑”