[MVCC] MVCC uses readview to determine which version a transaction reads (easy to understand, detailed explanation in one article!!)

Implementation principle of MVCC

        mvcc's reality, basicsundolog, print version ,readview. (Concrete performance)

In the data stored in MySQL, MySQL will add some additional hidden fields (Hidden Field) by default, including fields such as trx_id and roll_pointer. Most of these fields are used to support functions such as transactions and data recovery.

  • trx_id: It is an incrementing integer automatically generated by the system and used to identify the transaction in which the current operation is performed. Each time a new transaction is started, the transaction ID will be incremented by 1. Because the transaction ID is automatically generated by the system, we generally do not need to modify it manually.
  • roll_pointer: It is a pointer used to locate the previous version of data. When you modify a record, MySQL will internally create a copy of the record and save it in the redo log. At the same time, it will record the position of this copy in the redo log. This is roll_pointer the content represented. If a rollback operation occurs, MySQL will use roll_pointer to find the previous version of the corresponding record and restore it.

After there are so many versions, when there is a select query, which version is specifically queried?

readview reads the view to help us solve this problem

        When we use select to read data, there will be many versions of the data at this moment (for example, there are four versions in the picture above), but we don’t know which version to read, so we rely on readview. Limiting the version we can read, only through readview do we know which version we can read.

        When transaction select queries data, a readview will be constructed, which records some statistical values ​​​​of the data version chain, so that there is no need to traverse all version chains during subsequent query processing.

 A readview snapshot specifically includes the following fields:

Give some explanations about the parameters in readview

m_ids:Active transactions refer to transactions that have not yet been committed (those active [uncommitted] transactions will be displayed like a collection).

max_trx_id:For example, the transaction id in m_ids is (1, 2, 3), then the next transaction id that should be allocated is 4, and max_trx_id is 4.

creator_trx_id: The ID of the transaction currently executing the select read operation.

readview specifically determines which version in the version chain is available (emphasis!)

Four-step search rules——

Step 1: Determine whether the version was created by the current transaction

Ifcreator_trx_id=[current version trx_id], it means reading the data you modified, of course you can access it directly. If it is not equal to the trx_id of the current version, jump to the second step

Step 2: Whether [current version trx_id] is less than min_trx_id

[Current version trx_id]<min_trx_id, indicating that this version has been submitted before generating readview and can be accessed directly. If not, proceed to step 3

Step 3: Is [current version trx_id] greater than max_trx_id

[Current version trx_id]>max_trx_id, indicating that this version is only opened after the readview is generated , definitely cannot be accessed by the current transaction, so there is no need to perform the fourth step at this time to traverse and determine the next version. If the transaction ID of the current version is less than the maximum transaction ID, you can continue to step 4

Step 4: min_trx_id<[current version trx_id]<max_trx_id

        If the current version is not in the active transaction list, it means that when the readview is created, the version has been submitted and can be accessed directly.

        If it is in the active transaction list, the next version will be judged according to the version chain traversal until the first version that meets the requirements is found.

They are (1) (2) (3) (4) from top to bottom. Explain them again in order to deepen your impression.

trx_id indicates the transaction id to be read

(1) If the transaction ID to be read is equal to the transaction ID of the read operation, it means that I am reading the record created by myself, so why not?

(2) If the transaction ID to be read is less than the smallest active transaction ID, it means that the transaction to be read has been submitted and can be read.

(3) max_trx_id indicates the id assigned to the next transaction when generating readview. If the transaction id to be read is greater than max_trx_id, it means that the id is no longer in the readview version chain, so it cannot be accessed.

(4) m_ids stores the ID of the active transaction. If the transaction ID to be read is not in the active list, it can be read, but not vice versa.

 

How mvcc implements the isolation levels of RC and RR

(1)RC isolation level, each snapshot read< a i=4>willgenerate and obtain the latest readview.

(2)RR isolation level, only in the same transaction 's first snapshot read will create readview, every subsequent snapshot read uses the same readview, so every query result of is Same.

 

Guess you like

Origin blog.csdn.net/miles067/article/details/133745445