[Database] MySQL MVCC (Multi-Version Concurrency Control) multi-version concurrency control

MySQL's MVCC (Multi-Version Concurrency Control) multi-version concurrency control

I. Introduction

1, MySQL in order to guarantee transaction isolation, to achieve isolation level of the database, the introduction of MVCC
2, need to understand the undo log entry .
3. Need to understand the snapshot ReadView entry .
4. First look at the following MySQL snapshot read and current read.

Snapshot read:
read the visible version of the record (maybe the historical version) without locking. Simple and pure query operation belongs to snapshot read.
Such as:

SELECT * FROM student WHERE id=1;

Current read:
The latest version of the record is read, and the record returned by the current read will be locked to ensure that other transactions will not concurrently modify this record. Special query operations, insert, update, and delete operations belong to the current read.
Such as:

SELECT * FROM student WHERE id=1 LOCK IN SHARE MODE; //共享锁(S)
SELECT * FROM student WHERE id=1 FOR UPDATE; //排他锁(X)
INSERT INTO student VALUES (1, '张三'); //排他锁(X)
UPDATE student SET name='李四' WHERE id=1; //排他锁(X)
DELETE FROM student WHERE id=1; //排他锁(X)

Insert, update, and delete all belong to the current read, because these three operations include a current read. (The insertion operation may trigger the conflict check of Unique Key)
such as the specific process of the update operation:

  1. Start to execute the update sql, MySQL Server will read the first record that meets the conditions according to the where condition, and then the InnoDB engine will return the first record and lock it.
  2. After waiting for MySQL Server to receive this locked record, it will initiate an update request to update this record.
  3. One record operation is completed, and then the next record is read until the where conditions are not met.

Second, the realization of MVCC in MySQL

Generally, MVCC has two implementation methods:

  1. When writing new data, the old data is not deleted, but new data is inserted. PostgreSQL is the implementation method used.
  2. When writing new data, move the old data to a separate place, such as the rollback segment. When others read the data, read the old data from the rollback segment, such as the innodb engine in the Oracle database and MySQL.

Simply put, MySQL's MVCC provides us with a way to read row data in the current database. If the read row is performing a DELETE or UPDATE operation, the read operation will not wait for the locked row to be released, but Go read a snapshot of the row.
As shown:
Insert picture description here

The above figure visually shows the mechanism of InnoDB consistent non-locking read. It is called a non-locking read because there is no need to wait for the release of the exclusive lock on the row. Snapshot data refers to the data of the previous version of the row. Each row record may have multiple versions. This technique is generally called the row multi-version technique. The resulting concurrency control is called Multi Version Concurrency Control (MVCC). InnoDB implements MVCC through undo log.

In transaction isolation levels RC and RR, InnoDB uses MVCC consistent non-locking read by default.

  • Under RC, consistent non-locking read always reads the latest snapshot data of the locked row.
  • Under RR, read the row data version at the beginning of the transaction.

Guess you like

Origin blog.csdn.net/thesprit/article/details/112984374