Today, my beautiful sister sent me a picture as follows !
This is a InnoDB The default isolation level under the engine . We know InnoDB The default level is rr That is, can ` Repeated reading `
Before we look at this value, let's first understand the concept of what MVCC
MVCC Multi version concurrency control refers to “ Maintaining multiple versions of one data , So that there is no conflict between read and write operations ”.
And snapshot reading is MySQL For us to achieve MVCC One of the specific non blocking read functions of the ideal model . And relatively speaking , The current read is the specific function of pessimistic lock .
snapshot ” stay MVCC How does Li work :
At repeatable read isolation level , When the transaction starts “ Took a snapshot ”. This snapshot is based on the entire library . Of course, it doesn't copy all the data .
in fact InnoDB Each transaction has a unique transaction ID, called transaction id. It is at the beginning of the transaction to InnoDB The transaction system of the application is strictly increasing according to the order of application.
There are multiple versions of each row of data . Every time a transaction updates data , Will generate a new data version , And the transaction id The transaction assigned to this data version ID, Write it down as row trx_id. meanwhile , Keep the old data version , And in the new data version , You can get information directly .
InnoBB It's also a feature that uses multiple versions of each data ( Actually, I remember row_trx_id) The second level snapshot reading ability is realized .
At the transaction isolation level, we talked about the repeatable isolation level
I'm blind to everything else in my business. Give me a million dollars.
therefore , A transaction only needs to declare at startup that ,“ At the moment I start , If a data version is generated before I start , Just admit it ; If it was generated after I started , I don 't know , I have to find the last version of it ”.
Of course , If “ Last version ” Nor visible , Then we have to move on . Of course, if the transaction updates its own data , It still has to recognize itself .
meanwhile InnoDB An array is constructed for each transaction , Used to save the transaction startup moment , Right now “ active ” All the business of ID.“ active ” Refers to , Launched but not yet submitted
So in the figure above, there are three transactions that generate their transactions id The arrays are as follows ：
At this point, the transaction A In the transaction B、 Business C Before that means Relative to transactions A On business B、 Business C It's invisible, which is also consistent with the definition of repeatable reading, so Q2 The value is 18.
that Q1 Well ？
In the transaction B Then the transaction is executed C Look at the transaction C UPDATE statement executed . So you don't know if you have any impression of the second stage submission
The process of an update statement is the second stage submission. If you forgetPlease click this article to deepen your memory Two stage submission
The main meaning of this is update Statement will commit by default
update The statement actually requests
lock ( Row lock )
What happens if you don't apply for a lock ？
Suppose we have a record in our database. The initial value is 1, Update the same data in a concurrent environment First addition 1, The second time also added 1. Then it is very likely that they will find the same value and then update it It turns out that 2, Then we can't accept the result .
Of course, the line lock is added when necessary , But it's not about releasing them immediately when they don't need to , It's about waiting until the end of the transaction . This is the two-stage lock protocol .
And update data is read before write , And this read , Can only read the current value , be called “ The current reading ”（current read）.
So we're looking at the business C Yes update That is, the current reading So at this point age It has been changed to 19 Of course, if we commit the transaction manually, if the transaction C Late submission The transaction B Of course update Statement will wait until the transaction C Release and execute .
So let's go back to business B
Business B In the first query age There is no doubt that 18 Of And then tell the new through the above update Statement is locked and is currently read age Turned into 20. In the execution of the transaction B When querying statements , Take a look at their own version number is 2, The version number of the latest data It's also 2, It's your own update , You can use it directly , So the query results in age The value of is 20.