How to implement MySQL MVVC multi-version concurrency control
WBOY
Release: 2023-05-31 13:14:01
forward
1644 people have browsed it
1. Overview
Multi-version concurrency control (MVCC) is a concurrency control technology. MVVC, which implements database concurrency control, is inseparable from the version chain in the undo log, which is accomplished by maintaining multiple versions of data rows.
To put it simply, the current transaction queries the rows that are being changed by another transaction (dirty reading will occur if read at this time). Instead of locking and waiting, the historical version of the data is read, which reduces the response. time.
MVVC is implemented through two technologies: undo log and Read View.
2. Snapshot read and current read
The implementation of MVCC in MySQL InnoDB is mainly to improve the concurrent performance of the database and use a better way to handle read-write conflicts, so that even if there are When there is a read-write conflict, no locking and non-blocking concurrent reading can be achieved, and this reading refers to snapshot reading, not current reading. The current read is actually a locking operation.
1. Current reading
In order to ensure that the record read is the latest data and to prevent other concurrent transactions from modifying the record, the record needs to be locked when reading.
Locked reading is called current reading, and any addition, deletion or modification of data requires reading the data first. This reading process is also current reading.
SELECT * FROM t LOCK IN SHARE MODE; # 共享锁
SELECT * FROM t FOR UPDATE; # 排他锁
UPDATE SET t..
Copy after login
2. Snapshot read
Snapshot read is also called consistent read. It reads the snapshot version of the data row. In MySQL, ordinary select statements (select statements without for update or lock in share mode) use snapshot reading by default without locking.
SELECT * FROM table WHERE ...
Copy after login
The reason for this is that snapshot reading can avoid locking operations and reduce overhead.
When the isolation level of the transaction is serial, snapshot reading is useless and will degrade to current reading.
3. Isolation level and version chain review
Isolation level:
The default isolation level in MySQL is repeatable read RR, which can solve the non-repeatable read problem. In MySQL, special support is provided to solve the phantom read problem.
How does it solve the phantom reading problem? There are two ways:
Use gap locks and temporary key locks to solve the problem. In short, it is locking. During this period, other transactions cannot insert data
MVCC method does not require locking and has low consumption (the disadvantage is that it does not completely solve the phantom read problem).
undo log version chain:
Corresponding to InnoDB, each record in the clustered index contains two necessary hidden fields:
trx_id: Every time a transaction changes a clustered index record, the transaction id of the transaction will be assigned to the trx_id hidden column.
roll_pointer: rollback pointer. Every time the data is modified, the old data will be put into the undo log. The new data points to the old data to form a version chain. The pointer field is called the rollback pointer, through which the data before modification can be found.
Example:
A transaction with ID 8 creates a piece of data, then the schematic diagram of the record is as follows:
Assume that two transactions with IDs 10 and 20 will update this record. The process is as follows:
The above is the detailed content of How to implement MySQL MVVC multi-version concurrency control. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn