当前位置:网站首页>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)
边栏推荐
- Glide question you cannot start a load for a destroyed activity
- Lambda expression
- Swift Error Handling
- Day 1 of kotlin learning: simple built-in types of kotlin
- Node. Js: use of express + MySQL
- 【数据库原理复习题】
- 【判断题】【简答题】【数据库原理】
- The upward and downward transformation of polymorphism
- 最新版抽奖盲盒运营版
- Alibaba is bigger than sending SMS (user microservice - message microservice)
猜你喜欢
Grid connection - Analysis of low voltage ride through and island coexistence
【数据挖掘复习题】
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
(latest version) WiFi distribution multi format + installation framework
Sword finger offer09 Implementing queues with two stacks
Ali & ant self developed IDE
Public and private account sending prompt information (user microservice -- message microservice)
Sword finger offer07 Rebuild binary tree
Low code platform international multilingual (I18N) technical solution
自抗扰控制器七-二阶 LADRC-PLL 结构设计
随机推荐
【习题五】【数据库原理】
双链笔记·思源笔记综合评测:优点、缺点、评价
Implement verification code verification
【ManageEngine】IP地址扫描的作用
Using swift language features, write a pseudo-random number generator casually
Seven second order ladrc-pll structure design of active disturbance rejection controller
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
Analysis of a music player Login Protocol
[exercice 7] [principe de la base de données]
Differences between initial, inherit, unset, revert and all
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
LeetCode 0556. Next bigger element III - end of step 4
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
Powerful avatar making artifact wechat applet
Nodejs+Express+MySQL实现登陆功能(含验证码)
阿里大于发送短信(用户微服务--消息微服务)
Write a simple nodejs script
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
Project video based on Linu development