Detailed explanation of MySQL database transaction isolation levels

Detailed explanation of MySQL database transaction isolation levels

Database transaction isolation level

There are 4 isolation levels for database transactions, from low to high:

  1. Read uncommitted: Dirty reads are allowed.
  2. Read committed: Prevents dirty reads, the most commonly used isolation level, and is the default isolation level for most databases.
  3. Repeatable read: It can prevent dirty read and non-repeatable read.
  4. Serializable: It can prevent dirty reads, non-repeatable reads and phantom reads, and (transaction serialization) will reduce the efficiency of the database.

These four levels can solve the problems of dirty read, non-repeatable read, and phantom read one by one.

√: May appear ×: Will not appear

Transaction Level Dirty Read Non-repeatable read Phantom Read
Read uncommitted
Read committed ×
Repeatable read × ×
Serializable × × ×

Note: We discuss the isolation level scenario mainly when multiple transactions are concurrent.

Dirty reads, phantom reads, and non-repeatable reads

Dirty read:

Dirty read means that when a transaction is accessing data and modifying the data, and this modification has not been submitted to the database, another transaction also accesses the data and uses the data.

Non-repeatable read:

It means reading the same data multiple times within a transaction. Before this transaction ends, another transaction also accesses the same data. Therefore, between the two reads in the first transaction, the data read twice by the first transaction may be different due to the modification of the second transaction. This means that the data read twice in a transaction is different, so it is called non-repeatable read. (That is, the same data content cannot be read)

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 by inserting a new row of data into the table. Then, it may happen that the user operating the first transaction finds that there are still unmodified data rows in the table, as if he were hallucinating.

example:

surface:

CREATE TABLE `cc_wsyw126_user_test_isolation_copy` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `password` varchar(64) NOT NULL,
 `age` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `ix_age` (`age`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;

Simulated data:

INSERT INTO `cc_wsyw126_user_test_isolation_copy` (`password`, `age`) 
VALUES 
('1', 1),
('twenty two),
('3', 3),
('4', 4);

The first transaction A:

start transaction 
insert into cc_wsyw126_user_test_isolation_copy (password, age) values ​​('5',5)
commit

The second transaction B:

start transaction 
update cc_wsyw126_user_test_isolation_copy set age = 2 where password >='2'
select * from cc_wsyw126_user_test_isolation_copy where password >= '2';
commit

Steps to reproduce:

As long as the insert statement of transaction A is before the select statement and after the update statement of transaction B, it will be fine.

The MySQL InnoDB storage engine implements a multi-version concurrency control protocol - MVCC (Multi-Version Concurrency Control) plus a next-key locking strategy to prevent phantom reads at the Repeatable Read (RR) isolation level. If you want to test phantom reads, experiment with MyISAM.

In a clustered index (primary key index), if there is a unique constraint, InnoDB downgrades the default next-key lock to a record lock.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Tutorial on the relationship between Innodb transaction isolation level and lock in MySQL
  • Introduction to MySQL isolation level, lock and MVCC
  • MySQL transaction, isolation level and lock usage example analysis
  • In-depth understanding of the four isolation levels of MySQL
  • Detailed explanation and comparison of the four transaction isolation levels in MySQL
  • Detailed explanation of the four transaction isolation levels in MySQL
  • Detailed explanation of Mysql transaction isolation level read commit
  • In-depth explanation of MySQL isolation level and locking mechanism

<<:  How to encapsulate the table component of Vue Element

>>:  Introduction to Nginx log management

Recommend

CSS style writing order and naming conventions and precautions

The significance of writing order Reduce browser ...

Implementing a web player with JavaScript

Today I will share with you how to write a player...

MySQL replication advantages and principles explained in detail

Replication is to transfer the DDL and DML operat...

CSS syntax for table borders

<br /> CSS syntax for table borders The spec...

MySQL method steps to determine whether it is a subset

Table of contents 1. Problem 2. Solution Option 1...

Introduction to Enterprise Production MySQL Optimization

Compared with other large databases such as Oracl...

Detailed explanation of MySQL Truncate usage

Table of contents MySQL Truncate usage 1. Truncat...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

Detailed explanation of MySQL binlog usage

binlog is a binary log file that records all DML ...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...

5 solutions to CSS box collapse

First, what is box collapse? Elements that should...

Web2.0: Causes and Solutions of Information Overload

<br />Information duplication, information o...

JavaScript implements H5 gold coin function (example code)

Today I made a Spring Festival gold coin red enve...