innodb solve phantom read

Conclusion 1.
First of all, said the conclusions in the RR isolation level, Innodb use MVVC and next-key locks to solve the phantom read, MVVC solve is magic in ordinary reading (read snapshots) of, next-key locks to solve the current situation read phantom read under.

 

2. The phantom read is what
matters A, the first implementation:

update table set name=“hh” where id>3;

The results are:

How many rows of data OK row xx table name the successful impact

 

Transaction B, after the execution, and submit:

insert into table values(11, uu);

commit;

 

Transaction A, then select it:

select * from table where id>3

The result set as follows:

11, the

A transaction ignorant, I might not what id> 3 all the updates yet

The impact of the transaction is submitted B on A generates affairs, this effect is called "phantom read."

Magic Reading non-repeatable read and the difference is that the former is a range, which is itself

3. how to solve?
3.1. The current reading
so-called current read, referring to the locked select (S or X), update, delete and other statements. In the RR transaction isolation level, the database will use next-key locks to lock this record and the index range.

Take the example above, in the case of RR, assuming that the current reading, the read lock

select * from table where id> 3 is locked this record id = 3 and id> 3 this interval range, a range between the index record locking, record insertion to avoid inter-range, to avoid phantom rows.

 

3.2 ordinary reading
because reading is not locked ordinary reading, it will not have the use of next-key locks, the reading means magic solution is MVVC

mvvc give each tuple row plus some auxiliary fields, create version record and delete the version number.

And each transaction at startup, has a unique incrementing the version number. Each open a new transaction, the transaction version number is incremented.

By default isolation level (REPEATABLE READ), additions and deletions to change search became this:

SELECT
read to create a version equal to or less than the version number of the current transaction, and deletes empty or greater than the current version is the version number of the transaction record. This ensures that recorded before reading exist
INSERT
save the version number of the current transaction to create the version number of rows
UPDATE
newly inserted row, and the version number of the current transaction to create a new version of the line, while the original recording line delete version number to the version number of the current transaction
dELETE
to save the current transaction version number to the version number of rows deleted
 

For example, I insert a record, assuming that the transaction id is 1, then the record is as follows: In other words, create version number is the version number of the transaction.

id

name

createversion

deleteversion

1 wxt 1  
 

If I update it, assuming that the transaction id is 2

id

name

createverison

deleteversion

WXT 1 2 1
1 2 taotao  
Here is the updated name taotao, the original version of the tuple deleteversion this transaction id number, and a new

 

If I delete it, assuming that the transaction is id = 3

id

name

createverison

deleteversion

1 taotao 2 3
became like this now

 

The key point here

Now I read, it must meet two conditions

Read to create a version equal to or less than the current version number of this transaction means that the data must be created before this transaction
records are deleted version is empty or greater than the version number of the current transaction. This means that the deletion occurs after the transaction
 

Take the example described above

The current state of the database

 

id

name

createverison

deleteversion

4
a

2  
5 b 5  

A is assumed transaction id = 10

Now update table set name = "hh" where id> 3; the statement is executed

 

id

name

createverison

deleteversion

4
a

2 10
5 b 5 10
4
hh

10  
5 hh 10  
 

the transaction id = 11 B

insert into table values(11, uu);

 

id

name

createverison

deleteversion

4
a

2 10
5 b 5 10
4
hh

10  
5 HH 10  
11 11  
 

 

Last Transaction A (id = 10) this reading

 

select * from table where id>3

According to the rules,

Create a good read version of the current transaction → less so (4, hh) (4, a) (5, b) (5, hh)

Input output rule as above, then the following rules

Remove empty or greater than the current version is the version number of the transaction record → (4, hh) (5, hh)

So there is no read to read the line transaction B newly inserted solve phantom read

 

 

If the transaction is to update the B id = tuple name = cc 4 it

Similarly, according to the update of the rules

id

name

createverison

deleteversion

4
a

2 10
5 b 5 10
4
hh

10 11
5 hh 10  
4 cc 11  
 


Then to read it according to the rules of the select, or to give (4, hh) (5, hh)

4. say one more thing
in RC mode, MVVC not solve the phantom reads and non-repeatable read, because each read will read its own version of the snapshot refresh, is simply another transaction commits, he would refresh, read the latest
----------------
Disclaimer: This article is the original article CSDN bloggers' Aaron_ Tao ", the follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_33330687/article/details/89004462

Guess you like

Origin www.cnblogs.com/minikobe/p/12298533.html