当前位置:网站首页>Isolation level RR, gap lock, unreal reading
Isolation level RR, gap lock, unreal reading
2022-07-28 07:23:00 【georgesnoopy】
Table structure and data , Which field c There is a general index on .

Here's the scene :

In the transaction A in ,T1 The data read at any time is id=10,15,20 Three pieces of data , stay T4 The data read at any time is id=10,12,15,20, The number of data pieces read twice is different . This is the phenomenon of unreal reading .
innodb in RR Unreal reading at isolation level
It's just the scene , But it is in RR Execute at the isolation level :

In the transaction A in ,T1 The data read at any time is id=10,15,20 Three pieces of data , stay T4 The data read at any time is id=10,15,20, The content read twice is the same .
The reason is the same , Because RR There are... Under the isolation level MVCC Mechanism , In the use of begin When starting a transaction , Carry out article 1 sql When , Create a consistent read view (read-view), Subsequent transactions are read based on this consistency view , So I can't see the changes of other transactions .
Look at the following scene :

Will find , In the transaction A in ,T1 The data read at any time is id=10,15,20 Three pieces of data , stay T4 The data read at any time is id=10,12,15,20, The number of data pieces read twice is different .
This is because in the mysql in , All unlocked reads are consistent reads , All locked reads are current reads . The current read is not a read based on the consistency view of the current transaction , It is the latest submitted data read .
therefore , about innodb Come on , The isolation level is RR When , Current reading will produce unreal reading .
The problem of unreal reading
Here's the scene :

From business A The angle of , stay T2 Always query c=10, Only id=10 This line of data , So change this line of data , Set up d=0, But in the end id=10、12 Two lines of data d All are 0, Semantically , Easy to cause confusion .
Except for that , The bigger problem of unreal reading is the inconsistency of data .
Here's the scene ,binlog Format =statement:
After the execution , Master database data :

binlog
insert into t (id,c,d) values(12,12,12)
update t set d=0 where c>=10 and c<15Will find ,binlog Recorded in the sql Execute in sequence and actual main library sql The order of is different , When replaying from the Library binlog When ,

There will be inconsistency between master and slave data ,id=12 This data is different in master database and slave database . If mysql For some reason, it needs to be based on binlog Data recovery , The recovered data is also inconsistent with the beginning .
ps: So , If you use mysql The segregated sector is CR, that binlog The format must be ROW.
RR The isolation level solves the unreal reading problem -- Clearance lock
Phantom read problem refers to the fact that the same two reads in a transaction read different data ,innodb The solution is to lock and block .
The same scenario as above :

Business A stay T2 When it comes to execution , Would be right id=10 This line is locked up , And right c=(5,10),(10,15) This gap is shoulder locked , When a transaction B stay T3 When inserting , Due to clearance lock conflict , Will block waiting , Wait for business A Post commit transaction B In order to proceed .
This is the time ,binlog The recorded statements are in the same order as the actual execution of the main library :
update t set d=0 where c>=10 and c<15
insert into t (id,c,d) values(12,12,12)Does this seem to be at the isolation level RR Next ” perfect “ Solved the unreal reading problem ?
Look at the scene :

stay T2 Read the following at any time :

stay T5 Read the following at any time :

Two reads of the same condition , Read different data pieces , This situation can only be solved by the serial isolation level .
So , Under the segregated sector , It solves the unreal reading problem of some scenes , Ensure the correctness of the data , But because the read in the transaction needs to see the modification of this transaction , There are also some unreal scenes that cannot be solved . To sum up :
- stay RR Below grade , Because of MVCC, Read the consistent read view based on the current transaction , You can't see the changes of other transactions ( Other things have changed , Nothing has changed , Is visible ), To some extent, it solves the unreal reading problem of some scenes .ps: That's exactly what it is. mvcc, Realize the lock free reading of consistent reading .
- Introduce clearance lock , To ensure the binlog Format =statement Under the circumstances ,binlog It's also true . Due to the existence of clearance lock , Blocking the insertion of data in the gap , For the unreal reading problem of current reading , The gap lock can ensure that there is no unreal reading .
in addition , Why is gap lock not a mechanism to solve unreal reading , No, just right insert It works , Yes update It's the same :

This will also be blocked , Although for data , Business B Not inserting data , But for indexes c Come on , Is in the (10,15) A data is inserted into this gap , Because there is a gap lock in this gap , It will block .
Clearance lock
innodb The data of is organized by index , The data is placed on the primary key index tree :

If there are other secondary indexes , For example, in the field c An index is added : There will be another one B+ Trees , The leaf node stores the primary key .

Then we call it clearance lock , Whose gap is it ? For example, enter the following statement , What is the situation of locking :
update t set d=0 where c=10;Give a conclusion first :
- The gap is loaded on the index , The index used is added to the index .
- For modifying class operations , Row locks must be added to the primary key index tree .
therefore , Lock diagram :

You can verify :
The initial data are as follows :

The implementation is as follows sql:

T3 Business B It won't block , Successful implementation , The reason is that the gap lock is added to the index c On , There is no gap on the primary key .
If the transaction B Execution is :insert into t (id,c,d) values(14,10,10), Will be blocked . In fact, as long as here c Column Values in (10,15) Between , Will be blocked .
So , Gap refers to the index column used , Is the index on this column .
边栏推荐
- Redis sentinel mode and cluster
- Freemaker exports word with tables and multiple pictures to solve the repetition and deformation of pictures
- NoSQL之Redis配置与优化
- Layer 3 switching and VRRP
- Qucs preliminary use guide (not Multism)
- Basic knowledge of functions and special points
- Generate create table creation SQL statement according to excel
- Student duty problems
- js二级联动院系
- 牛客Mysql——SQL必知必会
猜你喜欢

How low-end computers learn secrets in depth - using the mistgpu computing platform

JS upload file method

easypoi一对多,合并单元格,并且根据内容自适应行高

OJ questions about fast and slow pointers in linked lists

Not used for the longest time recently

低端电脑如何深度学习秘籍-使用mistGPU计算平台

软考证书还能这样用!拿到证书=获得职称?

和为s的两个数字——每日两题

学生执勤问题

一个定时任务提醒工具
随机推荐
Shell--- circular statement practice
guava之Retryer
Implementation method of converting ast into word vector before converting word vector
MHA高可用配置及故障切换
Shell -- first day homework
Review of C language (variable parameters)
Branch and loop statements
Image segmentation method
Nodejs operation mongodb
Basic knowledge of functions and special points
2018-cvpr-Gesture Recognition: Focus on the Hands
Layer 3 switching and VRRP
Serial port configuration of raspberry pie
Standard C language summary 2
Easypoi one to many, merge cells, and adapt the row height according to the content
232 (female) to 422 (male)
Use of C3d
Static and floating routes
VCF file production
Earliest deadline first (EDF)