当前位置:网站首页>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
边栏推荐
- Redis exploration: cache breakdown, cache avalanche, cache penetration
- 下半年还有很多事要做
- Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
- 网络socket的状态要怎么统计?
- Need your own cognition
- 【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
- 高薪程序员&面试题精讲系列118之Session共享有哪些方案?
- PG基础篇--逻辑结构管理(触发器)
- Question d'entrevue de Huawei: recrutement
- Idea of [developing killer]
猜你喜欢

数论基础及其代码实现

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现

逆向调试入门-PE结构-输入表输出表05/07

codeforces -- 4B. Before an Exam

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable

【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告

工具箱之 IKVM.NET 项目新进展

Mobile note application
![leetcode:226. Flip binary tree [DFS flip]](/img/b8/6c5596ac30de59f0f347bb0bddf574.png)
leetcode:226. Flip binary tree [DFS flip]

数据库之MHA高可用集群部署及故障切换
随机推荐
软件测试中功能测试流程
腾讯总考epoll, 很烦
ROS2 Foxy depthai_ros教程
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
微信小程序 – 80个实用的微信小程序项目实例
Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
Topic 1004: the story of cows (recursion)
Redis exploration: cache breakdown, cache avalanche, cache penetration
Localtime can't re-enter. It's a pit
There are still many things to be done in the second half of the year
[today in history] July 1: the father of time sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
快速整明白Redis中的压缩列表到底是个啥
路由基础之OSPF LSA详细讲解
shell脚本导入存储过程到数据库
简单斐波那契(递推)
79. Word search [DFS + backtracking visit + traversal starting point]
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
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)
有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
Zero copy technology of MySQL