MVCC history of the most clear to explain --- easy to understand MYSQL MVCC implementation mechanism

1. MVCC Introduction
1.1 What is MVCC
MVCC is a multi-version concurrency control mechanism.

1.2 MVCC is to solve the problem?
Most MYSQL transactional storage engine, such as, InnoDB, Falcon and PBXT do not use a simple row lock mechanism. In fact, they are MVCC- and multi-version concurrency control for use with .
everyone should know that the lock mechanism can control the concurrent operation, but the system overhead is large, and MVCC can replace the row-level locking, in most cases, the use of MVCC, to reduce its overhead.
1.3 MVCC implement
MVCC is by saving data in the snapshot at a point in time to achieve. MVCC different storage engines. MVCC to achieve different storage engines it is different, there are typically optimistic concurrency and pessimistic concurrency control.

2.MVCC specific implementation analysis
below, to analyze how the MVCC concurrency control is achieved through our MVCC InnoDB's. 
InnoDB is MVCC, is achieved by saving two hidden columns in each row behind the record, these two columns, save time were the creation of this line, a saving of time is to delete the line. Here are not storing the actual time value, but the system version number (can be understood as the transaction ID), not the beginning of a new transaction, the system will automatically increment the version number, system version number will be the start time of the transaction as a transaction ID. the following look at REPEATABLE READ isolation level, MVCC specifically how to do.

2.1 Simple small example
Create Table Yang ( 
ID int Primary Key AUTO_INCREMENT, 
name VARCHAR (20 is));

Assume that the system version number starting at 1.

The INSERT
the InnoDB for each newly inserted row save the current system version as the version number. 
The first transaction ID is 1;

Start Transaction;
INSERT INTO Yang values (NULL, 'Yang');
INSERT INTO Yang values (NULL, 'Long');
INSERT INTO Yang values (NULL, 'FEI');
the commit;
. 1
2
. 3
. 4
. 5
corresponds to the data the table below (two columns are hidden behind, and we can not see through query)

id name creation time (transaction ID) deletion time (transaction ID)
1 Yang undefined 1
2 1 undefined Long
3 1 undefined FEI
the SELECT
InnoDB checks every two rows in accordance with the following conditions: 
a.InnoDB will find the version earlier than the current transaction version of the row (i.e., system version number is less than or equal line transaction system version number), which ensures that the read line transaction, either before the start of the transaction already exists, either inserted or modified transaction itself a. 
delete version b. line either undefined or greater than the version number of the current transaction, which ensures that the transaction to read a row, has not been deleted before the transaction began. 
only a, b meet record as a query to return result.

The DELETE
the InnoDB saved for each row to delete the current system version number (ID of the transaction) as a deletion indicator. 
See specific examples below Analysis: 
second transaction, ID 2;

Transaction Start;
SELECT * from Yang; // (1)
SELECT * from Yang; // (2)
the commit; 
1
2
. 3
. 4
assumed an
assumed implementation of this process a transaction ID is 2, just performed to (1), In this case, there is another 3 to the transaction ID is a data table inserted; 
the third transaction ID is 3;

Transaction Start;
INSERT INTO Yang values (NULL, 'Tian');
the commit;
. 1
2
. 3
in the data table at this time is as follows:

id name creation time (transaction ID) deletion time (transaction ID)
. 1 Yang. 1 undefined
2 Long. 1 undefined
. 3 FEI. 1 undefined
. 4 Tian. 3 undefined
and then proceed with the transaction (2), since the creation time data id = 4 in (transaction ID is 3), the execution of the current transaction ID is 2, and will find the transaction ID InnoDB less than or equal to the current data line transaction ID, the id = 4 rows is not performed in the transaction 2 (2) is retrieved, retrieved 2 in two transactions will only select statement data in the following table:

id name creation time (transaction ID) deletion time (transaction ID)
1 Yang 1 undefined
2 Long 1 undefined
3 FEI 1 undefined
Hypothesis 2
assumed that the implementation of this transaction ID to process 2, just performed to (1), assuming that the transaction execution after completion of the transaction 3, followed implementation of the transaction 4; 
fourth transaction:

Transaction Start;  
Delete from WHERE ID = Yang. 1;
the commit;  
. 1
2
. 3
at this time database table is as follows:

id name creation time (transaction ID) deletion time (transaction ID)
. 1 Yang. 1. 4
2 Long. 1 undefined
. 3 FEI. 1 undefined
. 4 Tian. 3 undefined
then performs the transaction ID for the transaction 2 (2), based on a SELECT search condition can be known, it retrieves creation time (creating a transaction ID) less than the current transaction ID row and delete time (deleted transaction ID) is greater than the current transaction line, and the line above id = 4 has already been said, but id = 1 line due to deletion time (ID delete transaction) is greater than the current transaction ID, and therefore transaction (2) select * from yang 2 is also the id = 1 data retrieved. Therefore, the two 2 select statement transaction data is retrieved as follows:

id name creation time (transaction ID) deletion time (transaction ID)
1 Yang 1 4
2 1 undefined Long
3 1 undefined FEI
UPDATE
InnoDB perform UPDATE, in fact, it is a new line inserted record, and save the time of its creation the current transaction ID delete time while preserving the current transaction ID to be UPDATE row.

3 is assumed
assumed executing the transaction 2 (1) after execution, the user performs other transaction 3,4, this time, and a user performs an operation on this table UPDATE: 
5th transaction:

Start Transaction;
update Yang SET name = 'Long' WHERE ID = 2;
the commit;
. 1
2
. 3
according to the update policy update to: generate a new line, and column add this transaction ID in the original to be modified deletion time column, get table is as follows:

id name creation time (transaction ID) deletion time (transaction ID)
1 Yang 1 4
2 Long 1 5
3 FEI 1 undefined
4 tian 3 undefined
2 Long 5 undefined
proceed (2), the search condition according to the select statement of affairs 2 get the following table:

id name creation time (transaction ID) deletion time (transaction ID)
. 1. 1. 4 Yang
2 Long. 1. 5
. 3 FEI undefined. 1
or 2 and a transaction (1) select the same result.
---------- ----------- 
https://blog.csdn.net/whoamiyang/article/details/51901888 
 

Guess you like

Origin blog.csdn.net/m0_37477061/article/details/91483580