当前位置:网站首页>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)
边栏推荐
- Export the entire Oracle Database
- 【判断题】【简答题】【数据库原理】
- Social community forum app ultra-high appearance UI interface
- alright alright alright
- Using swift language features, write a pseudo-random number generator casually
- [exercise 6] [Database Principle]
- The best shortcut is no shortcut
- Quickly learn member inner classes and local inner classes
- (最新版) Wifi分销多开版+安装框架
- elastic_ L04_ introduction. md
猜你喜欢
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
【数据库原理复习题】
2021 autumn Information Security Experiment 1 (password and hiding technology)
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Eureka self protection
电压环对 PFC 系统性能影响分析
TOGAF认证自学宝典V2.0
剑指Offer09. 用两个栈实现队列
The upward and downward transformation of polymorphism
社交社区论坛APP超高颜值UI界面
随机推荐
Apache Mina Development Manual
Applet wxss introduction
自抗扰控制器七-二阶 LADRC-PLL 结构设计
idea将web项目打包成war包并部署到服务器上运行
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
[embedded] - Introduction to four memory areas
Implement verification code verification
Openstack node address change
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
基于Linu开发的项目视频
Is it safe to open an account for online stock speculation? Who can answer
01_ Using the concurrent tool class library, is thread safety safe
基于同步坐标变换的谐波电流检测
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Oh my Zsh + TMUX installation
Analysis of a music player Login Protocol
如何在微信小程序中获取用户位置?
Use bloc to build a page instance of shutter
Kung Fu pays off, and learning is done
Xctf mobile--app2 problem solving