MySQL Advanced Road (13) - Research on the Principle and Significance of MVCC

MySQL Advanced Road (13) - Research on the Principle and Significance of MVCC

I. Overview

​ The previous article talked about the isolation level of the transaction of the InnoDB engine. This article will introduce the MVCC mechanism in detail. It involves a lot of content, but it is not difficult.

​ MVCC is multiversion concurrency controlthe abbreviation of , which means multi-version concurrency control in Chinese. This technology allows InnoDB transactions with a specific isolation level to perform consistent read operations; in addition to querying the data being updated by other things, you can also query the data before these updates occur. data. This technique greatly improves the concurrency of the database, so that query operations do not need to wait for locks held by other things.

​ This technology is mainly implemented through hidden fields , undo log version chain and read view mechanism

2. What kind of scenarios is MVCC applied in?

​ Multi-version concurrency control, its use scenario is when multiple transactions are executed concurrently, but there are three cases of concurrent transaction execution: 1. Read-read 2. Read-write 3. Write-write. We know that there is basically no problem in the first read-read scenario, but in the second and third scenarios, the problems of dirty writes, dirty reads and non-repeatable reads are prone to occur. For the third scenario, we have to control concurrency by locking. For the second scenario, if we use locking to solve the same problem as the third scenario, it will undoubtedly seriously affect the performance during concurrency. Therefore, MVCC is used to solve the problem. Good control over read-write operations for multiple transactions and conflict resolution

3. What are the mysterious hidden fields?

For each piece of data in the database, they actually hide several fields. The information recorded in these fields is only used by the technology inside the database, so it will not be displayed.

​ Three hidden fields:

​DB_ROW_ID : Hidden id field. Because behind the establishment of a table is to create a B+ tree, the creation of a B+ tree needs to be based on the primary key. If the primary key is not specified in the table, then this hidden field will be used as the primary key to build the B+ tree.

​DB_TRX_ID : The transaction ID that last updated this row of data

​DB_ROLL_PTR : The rollback pointer, which points to the undo log log, can immediately point to the historical data before modification

4. What is the length of the undo log version chain?

​ Because each transaction updates the data, the transaction ID will be recorded on the hidden field, and the transaction ID is self-increasing, so you can judge whether it is the latest data according to the size of the field, that is, the so-called 版本号version. The chain font of the chain species is now DB_ROLL_PTRon the hidden field, which points to the data of the previous version, so that the data of each version forms a chain structure. As shown below:

Please add image description

5. What is the Read View mechanism?

​ Read View, which means read view in Chinese, is actually an internal snapshot used by InnoDB's MVCC mechanism. Simply put, when a read transaction is opened, a Read View will be generated, which records the status of other transactions in the database when the transaction is opened. Why record these? Because, after knowing the progress of other transactions, you can judge whether the data can be read under the current isolation level according to the hidden field TRX_ID

Important information in Read View:

  • m_ids: IDs of uncommitted transactions
  • min_trx_id: the smallest value in m_ids
  • max_trx_id: the next newly generated transaction ID, which is the largest transaction ID
  • creator_trx_id: The transaction ID that created the read view, which actually means which transaction created the read view

The following will give an example to better understand

Please add image description

​ Assume that two transactions are executed concurrently under the RC isolation level at this time. One transaction 1 executes the query operation, and the other transaction 2 updates the value to be queried by transaction 1. Both transactions have not yet been committed. When querying, a Read View will be created, because it is the RC isolation level, so when transaction 1 queries the value A, it is found that the TRX_ID of the value A exists in the m_ids in the Read View, that is, the data is dirty data, not yet. Commit, so transaction 1 will not read this data, he will query historical data according to ROLL_PTR, and the result will be value B, because it is smaller than min_trx_id in Read View and not in m_ids, indicating that before transaction 1 is opened, update this data The transaction of the row data must have been committed, so it can be read, so transaction 1 finally reads the value B

​ Through the above example, I believe that everyone has a preliminary understanding of the role of Read View. With Read View, you can get which data can be read according to the hidden fields. If not found, it will follow the undo log version. Chain to find, and then judge according to Read View.

6. Summary

​ This article introduces three key elements for implementing MVCC—hidden fields, undo log version chain, and Read View mechanism. Together, they implement MVCC, which enables read operations in the scenario of concurrent read-write by multiple transactions. Need to wait because of the shackles of write operations, which greatly improves the concurrency of the database. This article only introduces the example under the RC isolation level when introducing the Read View at the end. In fact, MVCC can implement RU and RR in addition to RC. I will not introduce too much about the RR level here. I believe that as long as Understand the example of RC, this is also very easy

​ If this article is wrong, I hope it can be pointed out, everyone can learn and make progress together

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/120641176