当前位置:网站首页>Mvcc multi version control

Mvcc multi version control

2022-06-09 05:55:00 I am a man in the middle of the mountain

MVCC MySQL Official documents

brief introduction

MVCC Version control is a kind of MySQL Mechanisms for implementing isolation levels , Leverage version chain 、undo journal 、readView Can solve dirty reading 、 Unrepeatable read problem ,MySQL The default isolation level is repeatable (REPEARABLE READ), It solves the problem that dirty reading cannot be re read , Unreal reading requires a gap lock + The solution of row lock , This article does not discuss .

MySQL Isolation level

MySQL The default isolation level is repeatable read (Repeatable Read)
 Insert picture description here

MVCC

undo Log

undo log Official documents
undo log Is a log used to undo fallback , Before the transaction is committed ,MySQL The data before the update will be limited to undo log In the log file , When a transaction is rolled back, you can use undo log Back off . The right side of the figure below is a undo log Version chain , We'll talk about the specific explanation later .
 chart 1-1

Explain one of them roll_ptr and trx_id, Let's take a look at the official explanation first :

 Insert picture description here
namely :

trx_id :  Transaction fields , When a transaction operates on a row of data , Will put their own affairs Id Assign a value to trx_id Field 
roll_ptr :  rollback pointer , When a transaction updates a field , The previous fields will not be deleted directly , Instead, store the pointer to the previous field in undo blog

ReadView

MVCC among , Version number comparison is done by comparing transactions id, adopt ReadView Such a data structure , The data structure is as follows

m_ids: Currently active transactions ID list , Of all transactions in the transaction linked list id aggregate 
min_trx_id: The first thing to start , The SQL Startup time , The smallest transaction in the current transaction list id Number , That is, the oldest transaction created in the current system but not yet committed 
max_trx_id: The last transaction to start , The SQL Startup time , The largest transaction in the current transaction linked list id Number , That is, the maximum transaction number created recently except for itself 
creator_trx_id: Means to generate the ReadView The business of id.

1. If the accessed version of trx_id Property value and ReadView Medium creator_trx_id Same value , It means that the current transaction is accessing its own modified records , So this version can be accessed by the current transaction .
2. If the accessed version of trx_id Attribute value less than ReadView Medium min_trx_id value , Indicates that the transaction generating this version generates ReadView Submitted before , So this version can be accessed by the current transaction .
3. If the accessed version of trx_id Property value is greater than or equal to ReadView Medium max_trx_id value , Indicates that the transaction generating this version generates ReadView It's only opened after , So this version cannot be accessed by the current transaction .
4. If the accessed version of trx_id The attribute value is ReadView Of min_trx_id and max_trx_id Between , Then we need to judge trx_id Is the attribute value in m_ids In the list , If in , Description creation ReadView The transaction that generated this version is still active , This version is not accessible ; If not , Description creation ReadView The transaction that generated this version has been committed , This version can be accessed .

If a version of the data is not visible to the current transaction , Then follow the version chain to find the data of the next version , Continue to follow the steps above to determine visibility , And so on , Until the last version in the version chain . If the last version is not visible , This means that the record is completely invisible to the transaction , The query result does not contain the record .

for instance :
 Insert picture description here
There is a select sentence , Let's not mention its previous operation , Suppose it's trx_id by 110,select Time generation ReadView, here m_ids Among them are 100 and 120,min_trx_id by 100,max_trx_id by 120,min<110<max, So the fourth rule is true ,trx_id 100 and trx_id 120 Are invisible to them , You can only follow the version chain , Finally find the previous trx_id Not active id(m_ids) In the middle of “ initial ” The data of .

stay MySQL in ,READ COMMITTED and REPEATABLE READ A very big difference in isolation levels is that they generate ReadView The timing is different .

READ COMMITTED ——  Generate a... Before each read ReadView

Because one is generated every time readview, So it may lead to the first select m_ids Wait for the data to be different from the following , Can not solve the problem of repeatable reading .

REPEATABLE READ —— Generate a... When reading data for the first time ReadView

原网站

版权声明
本文为[I am a man in the middle of the mountain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090539312465.html