[MYSQL] What are the four isolation levels of transactions?

I. Overview

The reasons for dirty writes, dirty reads, non-repeatable reads, and phantom reads in scenarios where multiple transactions are executed concurrently. This article will give you a detailed introduction to how MySQL prevents the above situations from happening.

​ Looking back at what I have learned before, transactions have four characteristics of ACID, where I refers to the isolation of transactions. For isolation, there are different isolation levels. Different isolation levels are essentially changing or querying transactions at the same time The trade-offs between performance, reliability, consistency, and reproducibility of results differ when operating.

​ The isolation levels supported by InnoDB are: SERIALIZABLE, REPEATABLE READ, READ COMMITTED, and READ UNCOMMITTED. The Chinese meanings for them are: serializable, repeatable read, read committed, and read uncommitted. The default isolation level of InnoDB is REPEATABLE READ. Let's analyze these transactions one by one

Two, READ UNCOMMITTED (read uncommitted)

Under this isolation level, transactions can read values ​​that have not been committed by other things. In this case, dirty writes will not occur, because two uncommitted transactions cannot read the value of the same row of data, of which One transaction reads the uncommitted value of another transaction. Under this isolation level, dirty reads, non-repeatable and phantom reads will definitely occur

​ This isolation level is only applicable to scenarios where only query operations are performed, so it can be said that this isolation level is basically not used in many places

3. READ COMMITTED (read submitted)

Under this isolation level, transactions can only read other transactions and committed data, and cannot read uncommitted dirty data, so dirty writes and dirty reads will not occur, but non-repeatability and phantoms will occur. read.

The reason is very simple. This thing performs multiple read operations, and the read data may be submitted and different. Therefore, non-repeatability occurs. The reason for phantom reads is also very simple. When you perform range queries, Other things insert new data and commit.

Four, REPEATABLE READ (repeatable read)

Before a thing is submitted, the same data is queried multiple times, and the value of each query is the same, even if other things have modified the data and submitted it during this period, it will not be affected. This is also the default isolation level of the InnoDB engine.

​Dirty reads, dirty writes, and non-repeatable reads do not occur in this case. Phantom reads may occur because although the same value can be read repeatedly, it cannot be avoided that different numbers of values ​​are read when doing range queries.

Five, SERIALIZABLE (serializable)

This isolation level is relatively simple and rude. Under this isolation level, all things can only be executed serially and cannot be executed concurrently. Under this isolation level, dirty writes, dirty reads, non-repeatable reads, and phantom reads are all It won't happen, but the disadvantage is obvious, the performance is extremely poor! Therefore, this isolation level is hardly used in normal work

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/127860466