MySQL InnoDB transaction isolation level has four levels, the default is "repeatable read" (REPEATABLE) READ).
· Read Uncommitted (READUNCOMMITTED). Another transaction has modified the data but has not yet submitted it, and the SELECT in this transaction will read the uncommitted data (dirty read) ( The lowest isolation level and high concurrency performance ).
· 2). Submit reading (READCOMMITTED). What this transaction reads is the latest data (after other transactions are committed). The problem is that in the same transaction, the same SELECT will read different results twice (without repeated reading). There will be non-repeatable reading and phantom reading problems (locking the row being read)
· 3). Repeatable read (REPEATABLEREAD). In the same transaction, the result of SELECT is the state at the time when the transaction starts. Therefore, the results read by the same SELECT operation will be consistent. However, there will be phantom reading (explained later). Phantom reads occur (all rows read are locked).
· 4).Serialization (SERIALIZABLE). Read operations implicitly acquire shared locks, which ensures mutual exclusion (lock table) between different transactions.
‘
Four levels gradually increase in intensity, each solving a problem.
· 1).Dirty reading. Another transaction has modified the data but has not yet committed it, and the SELECT in this transaction will read the uncommitted data.
· 2). No repeated reading. After solving the dirty read, you will encounter that during the execution of the same transaction, another transaction submitted new data, so the data results read twice by this transaction will be inconsistent.
· 3). Phantom reading. It solves the problem of non-repeated reading and ensures that in the same transaction, the results of query are in the state (consistency) at the beginning of the transaction. However, if another transaction submits new data at the same time, when this transaction updates, it will be "surprised" to find these new data. It seems that the data read before is a "ghost" illusion. .
Specifically:
## 1). Dirty read
First distinguish dirty pages and dirty data
Dirty pages are pages that have been modified in the memory buffer pool and have not been flushed to the hard disk in time, but have been written to the redo log middle. Reading and modifying pages in the buffer pool is normal and can improve efficiency. Flush can be synchronized. Dirty data means that the transaction has modified the row record in the buffer pool, but has not yet submitted it! ! ! , if uncommitted row data in the buffer pool is read at this time, it is called a dirty read, which violates the isolation of transactions. Dirty reading means that when a transaction is accessing data and has modified the data, but the modification has not yet been submitted to the database, another transaction also accesses the data and then uses the data. ## 2). Non-repeatable read
refers to reading the same data multiple times within a transaction. Before this transaction ends, another transaction also accesses the same data. Then, between the two reads of data in the first transaction, the second transaction has been committed due to the modifications of the second transaction. Then the data read twice by the first transaction may be different. In this way, the data read twice within a transaction is different, so it is called non-repeatable read. For example, an editor reads the same document twice, but between reads the author rewrites the document. When the editor reads the document a second time, the document has changed. Raw reads are not repeatable. This problem can be avoided if editors can only read the document after the author has finished writing it
## 3). Phantom reading:
It refers to a phenomenon that occurs when transactions are not executed independently. For example, the first transaction modifies the data in a table, and this modification involves all data rows in the table. At the same time, the second transaction also modifies the data in this table. This modification inserts a row of new data into the table. Then, in the future, the user who operates the first transaction will find that there are still unmodified data rows in the table, as if a hallucination has occurred. For example, an editor changes a document submitted by an author, but when production merges their changes into the master copy of the document, it is discovered that the author has added new, unedited material to the document. This problem can be avoided if no one can add new material to the document until the editors and production department have finished working on the original document.
2. Isolation level experiment The following experiment is based on the blogger MySQL Server 5.6
First create a table, as follows:
USE test; CREATE TABLE `t` ( `a` int(11) NOT NULL PRIMARY KEY ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Transaction A READ-UNCOMMITTED | Transaction B READ-COMMITTED , | ##Transaction C-1 REPEATABLE-READTransaction C-2 REPEATABLE-READ | TransactionD SERIALIZABLE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ##start trans | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
start transaction; | insert into t(a)values( 4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1,2,3,4(dirty read: read data in uncommitted transactions) |
select * from t; | 1,2,3 (solve dirty reads)
select * from t; | 1,2, 3
select * from t; | 1,2,3
select * from t; | 1,2,3||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##commit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1,2,3,4 |
select * from t: 1,2,3,4 |
##select * from t: 1,2,3,4 (not in the same transaction as the above, so the latest read after the transaction is committed, so 4 can be read) | select * from t:1,2,3 (Repeated reading: Since it is in the same transaction as the above, only the data at the beginning of the transaction is read, that is, repeated reading) | select * from t:1,2,3,4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
commit (submit the transaction, the following is a new transaction, so you can read the latest data after the transaction is submitted) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
select * from t:1,2,3,4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
READ-UNCOMMITTED will generate dirty reads and is rarely applicable to actual scenarios, so it is basically not used. |
##Transaction A | Transaction B READ-COMMITTED | Transaction C REPEATABLE-READ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
start transaction; | start transaction; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1,2,3 |
select * from t; | 1,2,3|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##commit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1,2,3,4 | select * from t:1,2,3 (repeated reading: Since it is in the same transaction as the above, only the data of the transaction start transaction is read, that is, repeated reading ) |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
commit (commit the transaction, the following is a new transaction, so you can read the transaction commit The latest data in the future) |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
select * from t: | 1,2, 3,4 |
##REPEATABLE-READ can ensure that the data read in a transaction is repeatable, that is, the same read (the first read In the future, even if other transactions have submitted new data, selecting again in the same transaction will not be read). | READ-COMMITTED just ensures that the data that the latest transaction has committed is read. ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
当然数据的可见性都是对不同事务来说的,同一个事务,都是可以读到此事务中最新数据的。如下,
2.3、实验三:测试SERIALIZABLE事务对其他的影响
2.4. Experiment 4: Phantom Read Some articles write that InnoDB’s repeatable read avoids “phantom read” (phantom read). This statement is not accurate. . Do an experiment: (All the following experiments should pay attention to the storage engine and isolation level)
实验4-1:
In this way, phantom reading occurs, thinking that there is no data in the table, but in fact the data It already existed. After submitting, I found that the data conflicted. Experiment 4-2:
You can see that the lock added with id<=1 only locks the range of id<=1. The record with id 2 can be added successfully. When adding the record with id 0, it will wait for the lock to be released. Attached note: Detailed explanation of locks in repeatable reads in the MySQL manual: http://dev.mysql.com/doc/refman/5.0 /en/set-transaction.html#isolevel_repeatable-read For locking reads ( Consistency read and commit read, first look at the experiment , Experiment 4-4:
Attached note: If you use ordinary reading, you will get consistent results. If you use locked reading, you will read the "latest" "committed" reading results. itself, repeatable read and committed read are contradictory. In the same transaction, if repeatable reading is guaranteed, the commits of other transactions will not be visible, which violates committed read; if committed read is guaranteed, the results of the two previous reads will be inconsistent, which violates repeatable. read. It can be said that InnoDB provides such a mechanism. In the default repeatable read isolation level, you can use locked read to query the latest data. http://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html If you want to see the “freshest” state of the database , you should use either theREAD COMMITTED isolation level or a locking read: ------
3. Summary
|
The above is the detailed content of What are the four transaction levels of MySQL InnoDB and dirty reads, non-repeated reads, and phantom reads?. For more information, please follow other related articles on the PHP Chinese website!