Home >Database >Mysql Tutorial >How to implement MySQL MVVC multi-version concurrency control
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.
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.
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..
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 ...
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.
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:
Transaction 10 | 事20 |
---|---|
BEGIN; | |
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!