[58 Shen Jian Architect's Road] InnoDB, snapshot reading, what is the difference between RR and RC?

Snapshot Read

MySQL database, InnoDB storage engine, in order to improve concurrency, MVCC mechanism is used to improve concurrency by reading the historical data version of the data row without locking during concurrent transactions ( Consistent Nonlocking Read). ).

 

Read Committed

  • Database field, a type of transaction isolation level, referred to as RC

  • It solves the "dirty read" problem and ensures that the data rows read are all written by committed transactions

  • It may have the problem of "reading phantom rows". In the same transaction, consecutive identical reads may read different result sets

 

Repeated Read

  • Database field, a type of transaction isolation level, referred to as RR

  • It not only solves the problem of "dirty reading", but also solves the problem of "reading phantom rows". In the same transaction, consecutive identical reads read the same result set

 

Under the isolation level of read commit ( RC ) and repeatable read ( R R ), what is the difference between snapshot read ?

 

Conclusion first :

  • Transactions can always read and write (update /insert /delete) row records by themselves

  • Under RC, snapshot reading can always read the latest row data snapshot, of course, it must be written by a committed transaction

  • Under RR, the time of the first read record of a transaction is T, and records written by committed transactions after time T will not be read in the future, so as to ensure that consecutive identical reads read the same result set

Voice-over: can see

(1) It has nothing to do with the start time of the concurrent transaction, and it is related to the time of the first read of the transaction;

(2) Since it is not locked, it has little relationship with mutual exclusion;

 

These can answer " InnoDB's snapshot reading, what is it related to? ", the InnoDB table:

t(id PK, name);
 
There are three records in the table:
1, shenjian
2, zhangsan
3, lisi

 

In case 1 , the time sequence of two concurrent transactions A and B is as follows (A starts before B, and B ends before A):

A1: start transaction;
         B1: start transaction;
A2: select * from t;
         B2: insert into t values (4, wangwu);
A3: select * from t;
         B3: commit;
A4: select * from t;

 

Question 1 : Assuming that the isolation level of the transaction is repeatable read RR , what result sets are read by A2, A3, and A4 for the three queries in transaction A?

 

Answer : under RR

(1) The result set read by A2 must be {1, 2, 3}, which is the first read of transaction A, assuming time T;

(2) The result set read by A3 is also {1, 2, 3}, because B has not yet submitted;

(3) The result set read by A4 is still {1, 2, 3}, because transaction B is submitted after time T, A4 has to read the same record as A2;


Question 2 : Assuming that the isolation level of the transaction is read and submit RC , what result sets are read by A2, A3, and A4?

 

Answer : under RC

(1) The result set read by A2 is {1, 2, 3};

(2) The result set read by A3 is also {1, 2, 3}, because B has not yet submitted;

(3) The result set read by A4 is still {1, 2, 3, 4}, because transaction B has been committed;

 

case 2 , still the two transactions above, just with slightly different start times for A and B (B starts before A, and B ends before A):

         B1: start transaction;

A1: start transaction;

A2: select * from t;
         B2: insert into t values (4, wangwu);
A3: select * from t;
         B3: commit;
A4: select * from t;

 

Question 3 : Assuming that the isolation level of the transaction is repeatable read RR , what result sets are read by A2, A3, and A4 for the three queries in transaction A?

Question 4 : Assuming that the isolation level of the transaction is read-commit RC , what are the result sets of A2, A3, and A4?
 

Answer : The start time of the transaction is different and will not affect the result of "snapshot read", so the result set is the same as case 1.

 

case 3 , still concurrent transactions A and B (A starts before B, and B ends before A):

A1: start transaction;
         B1: start transaction;
         B2: insert into t values (4, wangwu);
         B3: commit;
A2: select * from t;

 

Question 5 : Assuming the isolation level of the transaction is Repeatable Read RR , A2 query in transaction A, what is the result set?

Question 6 : Assuming that the isolation level of the transaction is read-commit RC , what is the result set of A2?

 

Answer : Under RR,

A2 is the first read of transaction A. Assuming time T, it can read the data rows written by the transaction submitted before T, so the result set is {1, 2, 3, 4}. Under RC, there is no doubt that it must be {1, 2, 3, 4}.

 

In case 4 , the transaction start time is changed again (B starts before A, and B ends before A):

         B1: start transaction;

A1: start transaction;

         B2: insert into t values (4, wangwu);

         B3: commit;
A2: select * from t;

 

Question 7 : Assuming the isolation level of the transaction is Repeatable Read RR , A2 query in transaction A, what is the result set?

Question 8 : Assuming that the isolation level of the transaction is read-commit RC , what is the result set of A2?

 

Answer : The start time of the transaction is different and will not affect the result of "snapshot read", so the result set is the same as case 3.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135048&siteId=291194637