当前位置:网站首页>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 idAssign a value totrx_idHide 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 journalin , 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)
边栏推荐
- It feels great to know you learned something, isn‘t it?
- [exercise 6] [Database Principle]
- [exercice 7] [principe de la base de données]
- 【数据挖掘复习题】
- [embedded] - Introduction to four memory areas
- GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
- 双链笔记·思源笔记综合评测:优点、缺点、评价
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- 剑指Offer03. 数组中重复的数字【简单】
- The latest version of blind box mall thinkphp+uniapp
猜你喜欢

阿里 & 蚂蚁自研 IDE

Integer case study of packaging

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

2021 autumn Information Security Experiment 1 (password and hiding technology)

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Analysis of the influence of voltage loop on PFC system performance

记录自己vulnhub闯关记录

Record your vulnhub breakthrough record

剑指Offer04. 二维数组中的查找【中等】

Public and private account sending prompt information (user microservice -- message microservice)
随机推荐
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
【习题七】【数据库原理】
The latest version of lottery blind box operation version
Method overloading and rewriting
Ali & ant self developed IDE
[exercise 6] [Database Principle]
elastic_ L01_ summary
The best shortcut is no shortcut
Harmonic current detection based on synchronous coordinate transformation
【数据挖掘复习题】
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
剑指Offer09. 用两个栈实现队列
Low code platform international multilingual (I18N) technical solution
【ManageEngine】IP地址扫描的作用
Redhat5 installing socket5 proxy server
Attack and defense world mobile--ph0en1x-100
CNN MNIST handwriting recognition
【习题五】【数据库原理】
Quick learning 1.8 front and rear interfaces