MySQL dirty reads, phantom reads, phantom reads

Transaction characteristics:

  • Atomic: refers in the same transaction multiple statements are inseparable.
  • Consistency: A transaction must transform the database from one consistent state to another consistent state. For example, transfers, two transfers and account balances prior to 2k, then transfer it should be 2K.
  • Isolation: refers to a multi-threaded environment, a thread of affairs can not be bothered by other threads in the transaction
  • Persistence: Once you have submitted the transaction, it should be saved permanently.

Transaction isolation problem: 
If you do not consider the transaction isolation, the following questions:

  • Dirty read: refers to a transaction thread to read the data in another thread uncommitted.
  • Non-repeatable read (dummy read): refers to a read transaction thread to another thread data update of a submission.
  • Magic Reading: refers to a transaction thread to read a thread insert additional data submitted.

Isolation level:

Isolation Levels Dirty read (Dirty Read) Non-repeatable read (NonRepeatable Read) Magic Reading (Phantom Read)
Uncommitted Read (Read uncommitted) may may may
Read Committed (Read committed) impossible may may
Repeatable Read (Repeatable read) impossible impossible may
Serialization (the Serializable) impossible impossible impossible

 

The higher the level, the more secure the data, but the lower the performance.

Non-repeatable read and phantom read is quite similar, is in a plurality of times to a different read transaction data. Summary on the network as follows: 
non-repeatable read: so-called dummy read, that is, we often say that the non-repeatable read, refers to the database access, the same query within a transaction scope but returned two different data. This is due to the system to submit a query to modify other matters arising. Such as a data read transaction T1, T2 transaction to read and modify the data, the value of T1 in order to test the read and read the data again, get a different result. 
One kind of statement is easier to understand: in a transaction, reading the same data multiple times. When this transaction is not over, another transaction also access the same data. Then, in two of the first transaction between the read data. Since the modified second transaction, then the first read transaction data may be different, so that a transaction occurs in both of the read data is not the same, so called non-repeatable read, i.e., read the original taken not repeated. 
The so-called phantom read means reads transaction A number of lines that match the search criteria. Transaction B to insert or delete rows, etc. to modify the results A transaction set, and then submitted. 
Magic Reading refers to a phenomenon that occurs when the transaction is not performed independently, for example, a first transaction data in the table has been modified, such modification involves this table, "all the data lines." At the same time, also the second transaction to modify the data in this table, this modification is to insert "new row" to the table. Then, the user operates the first transaction will occur after the discovery of the table there is no modification of the data lines, as if the same happened hallucinations general solution is to increase the range of phantom read lock RangeS, lock the lock detection range only reading, thus avoiding the phantom reads. In simple terms, phantom read is caused by the insertion or deletion. 
General difference is that non-repeatable read another transaction because of a change to the data caused, and phantom reads another transaction is due to the insertion or deletion caused.

Non-repeatable read (dummy read) and differential phantom read: 
From the overall result, it seems that both inconsistent performance as a result of two readings 
but if you're from a control point of view, the difference is more large: 
for the former, you only need to lock records meet the conditions 
for the latter to be locked to meet the conditions of its record close

Published 324 original articles · won praise 77 · views 930 000 +

Guess you like

Origin blog.csdn.net/permike/article/details/98479206