当前位置:网站首页>Deeply understand the mvcc mechanism of MySQL
Deeply understand the mvcc mechanism of MySQL
2022-07-03 12:50:00 【Mcc_ mingchao】
undo log
undo log It's a rollback log , It does two things : Provides rollback operations and Multiple line versioning (MVCC).
When the data is modified , Not only did it record redo, The corresponding undo, If the transaction fails or rolls back for some reason , With the help of this undo Roll back .
undo log and redo log Recording physical logs is not the same .undo log The main record is the logical change of data , It's a logical log .
Can be considered as delete When a record ,undo log A corresponding insert Record , vice versa , When update When a record , It records an opposite update Record .
When executed rollback when , You can start from undo log The logical record in reads the corresponding content and rolls back .
Multiline version control (MVCC) When , through undo log To achieve :
When a read row is locked by another transaction , It can come from undo log Analyze the previous data of this line record , To provide the version information of this line , Let the user realize non lock consistent reading .
MySQL Transaction isolation level
except redo log and undo log outside , The other you are studying MVCC What I have to know before , Isolation level . Isolation level is quite basic knowledge , So let's review quickly here .
notes :MySQL The default isolation level for is repeatable-read Level
And in MySQL in repeatable-read Level can also handle unreal reading , This is a MySQL Unique next-keylock Realized .
Different database vendors have different requirements for SQL standard The four isolation levels specified in support are different :
Oracle Just support read-committed and serializable Isolation level .
MVCC Only in read-committed and repeatable-read Working at two isolation levels , The other two isolation levels :
read-uncommitted, Always read the latest data lines , Instead of reading the data row of the current transaction version .
serializable, Will lock all read lines , and MVCC Are not compatible .
MVCC principle
Version chain
MySQL Each row of records of is actually a linked list in logic .
MySQL In addition to recording business data in line records , And hidden trx_id and roll_ptr
trx_id
: Represents the... Of the recently modified transaction id , Every time a transaction changes a clustered index record , Will take the matterBusiness id
Assign a value totrx_id
Hide columns . When adding a transaction ,trx_id It will increase , therefore trx_id Be able to indicate the order in which transactions start .roll_pointer
: Point to the address of the previous version of the line , Every time a cluster index record is changed , Will write the old version toundo journal
in , Then the hidden column is equivalent to a pointer , It can be used to find the information before the modification of the record .
update user set name = ' Yuanbao 2 ' where id = 1
Of course , This linked list exists in undo log in , Different from the latest version of data .
After each update , Will put the old value in one undo log in , Even an old version of the record , As the number of updates increases , All versions will be roll_ptr Attributes are connected into a linked list , We call this list Version chain , The header node of the version chain is the latest value of the current record .
in addition , Each version is also included in the generated version Business id(trx_id), This information is very important , We'll use... Later .
ReadView
That's it undo log, Say again ReadView. We said earlier :MVCC Only in read-committed and repeatable-read
Working at two isolation levels , and read-committed and repeatable-read The difference between them is that they generate ReadView The strategy is different .
For the use of read-committed and repeatable-read For isolation level transactions , You must ensure that you read the transaction modified records that have been committed , That is, if another transaction has modified the record but has not yet committed , You can't read the latest version of the record directly .
The core issue is : We need to judge , Which version in the version chain is visible to the current transaction .
So ,InnoDB Came up with a ReadView The concept of , This ReadView There was a id list trx_ids
To read and write transactions currently active in the storage system , That is to say begin Not yet commit or rollback The business of .
Continue to use the previous example to understand ReadView and trx_ids.
Submit trx_id yes 2 After recording , Then there's a trx_id by 3 The business of , modify name For Yuanbao 3, But the business has not yet been committed .
update user set name = ' Yuanbao 3 ' where id = 1
Then the version chain is ︰
obviously , At this time trx_ids by [ 3 ]
If another transaction queries id by 1 The record of , because trx_ids Currently, there are only transactions trx_id by 3 The business of , and trx_ids The meaning of is to record unfinished transactions . ad locum , Transaction not completed , So this record is invisible , Continue to query — strip , The result returned to Yuanbao 2.
At this time, I put trx_id by 3 The transaction of has been submitted , And a new trx_id by 4 Also modify id by 1 The record of name= Yuanbao 4, And don't commit the transaction .
update user set name = ' Yuanbao 4 ' where id = 1
At this point, the version chain is ︰
read-committed —— Before each query of data, a ReadView
trx_ids Update to [ 4 ], The version chain passes trx_id The result of comparison is Yuanbao 3.
repeatable-read —— Generate a ReadView, All subsequent reads reuse the previous ones .
There will be no reconstruction ReadView , trx_ids still [ 3 ],MySQL Think of affairs 3 Hang in the air , therefore select The result is Yuanbao 2. The first 2 Time select The result and the first 1 It's the same time , So it's called repeatable reading .
This article references from In depth understanding of MySQL Of MVCC Mechanism - cloud + Community - Tencent cloud (tencent.com)
边栏推荐
猜你喜欢
随机推荐
Exploration of sqoop1.4.4 native incremental import feature
剑指Offer10- I. 斐波那契数列
Public and private account sending prompt information (user microservice -- message microservice)
CNN MNIST handwriting recognition
Dix règles de travail
【习题五】【数据库原理】
Sword finger offer10- I. Fibonacci sequence
Lambda expression
Sword finger offer05 Replace spaces
LeetCode 0556. Next bigger element III - end of step 4
并网-低电压穿越与孤岛并存分析
【习题六】【数据库原理】
The best shortcut is no shortcut
Apache Mina Development Manual
I'm too lazy to write more than one character
Sword finger offer09 Implementing queues with two stacks
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Swift Error Handling
Grid connection - Analysis of low voltage ride through and island coexistence
[exercice 7] [principe de la base de données]