isolation level

    Note: Most of the content in this article is taken from the first chapter of "High Performance MySQL" - MySQL Architecture and History.

    As one of the four characteristics of transaction ACID, isolation is actually more complicated than expected because it involves concurrency control of the system. Four isolation levels are defined in the SQL standard (note: the isolation levels implemented by each storage engine are different), each of which specifies the degree of visibility of changes made in a transaction within and between transactions. Lower levels of isolation can generally perform higher concurrency with lower overhead to the system.

    The following is a brief introduction to these four isolation levels.
    1. READ UNCOMMITIED: At this level, changes in a transaction, even if not committed, are visible to other transactions. Transactions can read uncommitted data, known as dirty reads, which cause many problems and are rarely used in practice.
    2. READ COMMITED: This is the default level of most database systems, and it can only "see" the changes made by committed transactions. It is also sometimes called a non-repeatable read because executing the same query twice may yield different results.
    3. REPEATABLE READ: This is the default transaction isolation level of MySQL. It solves the problem of dirty reads and ensures that the results of reading the same record multiple times in the same transaction are consistent. But in theory, the repeatable read isolation level still cannot solve another phantom read problem. The so-called phantom read means that when a transaction reads records in a certain range, another transaction inserts new records in the range. When the previous transaction reads again, a phantom row will be generated. The InnoDB and XtraDB storage engines solve the problem of phantom reads through multi-version concurrency control.
    4. SERIALIZABLE: This is the highest isolation level, which avoids phantom read problems by forcing transactions to be executed serially. But because it locks each row of data read, it may cause a lot of timeouts and lock contention problems, so it is rarely used in practical applications.

    MySQL can set the isolation level by executing the "SET TRANSACTION ISOLATION LEVEL" command. The new isolation level takes effect at the start of the next transaction. You can set the isolation level of the entire database in the configuration file, or you can only change the isolation level of the current session:
    mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

    Finally, we use a table as a summary:
isolation level Dirty read possibility non-repeatable read possibility Phantom reading possibility lock read
uncommitted read yes yes yes no
Submit to read no yes yes no
repeatable read no no yes no
Serializable no no no yes

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326853425&siteId=291194637