Home>Article>Database> Let you understand the transaction isolation level in MySQL through cases

Let you understand the transaction isolation level in MySQL through cases

青灯夜游
青灯夜游 forward
2021-09-23 11:09:00 1602browse

This article will help you understand the transaction isolation level in MySQL through four cases. I hope it will be helpful to everyone!

Let you understand the transaction isolation level in MySQL through cases

Many friends have always been confused about the isolation level of MySQL. In fact, this problem is not difficult at all. The key depends on how you explain it! Just looking at the theory will definitely make you dizzy, but if we demonstrate it through a few actual SQLs, everyone will find that this thing is so simple! [Related recommendations:mysql video tutorial]

Today Brother Song would like to demonstrate the transaction isolation level issue in MySQL through a few simple cases.

1. Theory

There are four types of transaction isolation levels in MySQL, as follows:

  • Serialization (SERIALIZABLE)
  • REPEATABLE READ
  • READ COMMITTED
  • READ UNCOMMITTED

Four different The meanings of the isolation levels are as follows:

  • SERIALIZABLE

If the isolation level is serialization, users pass one after another Execute current transactions sequentially. This isolation level provides maximum isolation between transactions.

  • REPEATABLE READ

In repeatable read at this isolation level, transactions are not considered a sequence. However, the changes in the currently executing transaction are still not visible to the outside. That is to say, if the user executes the same SELECT statement several times in another transaction, the result will always be the same. (Because the data changes caused by the executing transaction cannot be seen by the outside world).

  • READ COMMITTED

##READ COMMITTED isolation level is less secure than REPEATABLE READ isolation level. Transactions at READ COMMITTED level can see modifications to data by other transactions. That is, multiple SELECT statements for the same transaction may return different results if other transactions modify the corresponding tables during the transaction.

  • READ UNCOMMITTED

##READ UNCOMMITTED provides minimal isolation between transactions. In addition to easily generating phantom read operations and non-repeatable read operations, transactions at this isolation level can read data that other transactions have not yet committed. If this transaction uses changes that are not committed by other transactions as the basis for calculation, then those uncommitted changes will Commited changes are undone by their parent transaction, which results in a large number of data changes.

In the MySQL database, the default transaction isolation level is REPEATABLE READ

2. SQL practice

Next, we will verify the above theory to readers through a few simple SQLs.

2.1 Check the isolation level

You can check the default global isolation level of the database instance and the isolation level of the current session through the following SQL:

Used before MySQL8 Run the following command to view the MySQL isolation level:

SELECT @@GLOBAL.tx_isolation, @@tx_isolation;

The query results are as shown in the figure:

Let you understand the transaction isolation level in MySQL through casesAs you can see, the default isolation level is REPEATABLE-READ, the global isolation level This is true for both the current session isolation level and the current session isolation level.

Starting from MySQL8, use the following command to view the MySQL default isolation level

:

SELECT @@GLOBAL.transaction_isolation, @@transaction_isolation;
The keywords have changed, and everything else is the same.

The isolation level can be modified through the following command (it is recommended that developers modify the current session isolation level when modifying, without modifying the global isolation level):

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

The above SQL indicates that the current The database isolation level of the session is set to READ UNCOMMITTED. After the setting is successful, the isolation level is queried again and it is found that the isolation level of the current session has changed, as shown in Figure 1-2:

Let you understand the transaction isolation level in MySQL through cases

Note that if you only modify the isolation level of the current session, after changing the session, the isolation level will return to the default isolation level, so when we test, just modify the isolation level of the current session.

2.2 READ UNCOMMITTED

2.2.1 Prepare test data

READ UNCOMMITTED is the lowest isolation level. In this isolation level, there are

dirty reads, unavailable Repeated reading and phantom reading

problems, so here we first look at this isolation level, so that everyone can understand what these three problems are.They are introduced separately below.

First create a simple table, preset two pieces of data, as follows:

表的数据很简单,有 javaboy 和 itboyhub 两个用户,两个人的账户各有 1000 人民币。现在模拟这两个用户之间的一个转账操作。

注意,如果读者使用的是 Navicat 的话,不同的查询窗口就对应了不同的 session,如果读者使用了 SQLyog 的话,不同查询窗口对应同一个 session,因此如果使用 SQLyog,需要读者再开启一个新的连接,在新的连接中进行查询操作。

2.2.2 脏读

一个事务读到另外一个事务还没有提交的数据,称之为脏读。具体操作如下:

  • 首先打开两个SQL操作窗口,假设分别为 A 和 B,在 A 窗口中输入如下几条 SQL (输入完成后不用执行):

START TRANSACTION; UPDATE account set balance=balance+100 where name='javaboy'; UPDATE account set balance=balance-100 where name='itboyhub';COMMIT;
  • 在 B 窗口执行如下 SQL,修改默认的事务隔离级别为 READ UNCOMMITTED,如下:

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  • 接下来在 B 窗口中输入如下 SQL,输入完成后,首先执行第一行开启事务(注意只需要执行一行即可):

START TRANSACTION;SELECT * from account;COMMIT;
  • 接下来执行 A 窗口中的前两条 SQL,即开启事务,给 javaboy 这个账户添加 100 元。

  • 进入到 B 窗口,执行 B 窗口的第二条查询 SQL(SELECT * from user;),结果如下:

Let you understand the transaction isolation level in MySQL through cases

可以看到,A 窗口中的事务,虽然还未提交,但是 B 窗口中已经可以查询到数据的相关变化了。

这就是脏读问题。

2.2.3 不可重复读

不可重复读是指一个事务先后读取同一条记录,但两次读取的数据不同,称之为不可重复读。具体操作步骤如下(操作之前先将两个账户的钱都恢复为1000):

  1. 首先打开两个查询窗口 A 和 B ,并且将 B 的数据库事务隔离级别设置为 READ UNCOMMITTED。具体 SQL 参考上文,这里不赘述。

  2. 在 B 窗口中输入如下 SQL,然后只执行前两条 SQL 开启事务并查询 javaboy 的账户:

START TRANSACTION;SELECT * from account where name='javaboy';COMMIT;

前两条 SQL 执行结果如下:

Let you understand the transaction isolation level in MySQL through cases

  1. 在 A 窗口中执行如下 SQL,给 javaboy 这个账户添加 100 块钱,如下:
START TRANSACTION; UPDATE account set balance=balance+100 where name='javaboy';COMMIT;

4.再次回到 B 窗口,执行 B 窗口的第二条 SQL 查看 javaboy 的账户,结果如下:

Let you understand the transaction isolation level in MySQL through cases

javaboy 的账户已经发生了变化,即前后两次查看 javaboy 账户,结果不一致,这就是不可重复读

和脏读的区别在于,脏读是看到了其他事务未提交的数据,而不可重复读是看到了其他事务已经提交的数据(由于当前 SQL 也是在事务中,因此有可能并不想看到其他事务已经提交的数据)。

2.2.4 幻象读

幻象读和不可重复读非常像,看名字就是产生幻觉了。

我举一个简单例子。

在 A 窗口中输入如下 SQL:

START TRANSACTION;insert into account(name,balance) values('zhangsan',1000);COMMIT;

然后在 B 窗口输入如下 SQL:

START TRANSACTION;SELECT * from account;delete from account where name='zhangsan';COMMIT;

我们执行步骤如下:

  • 首先执行 B 窗口的前两行,开启一个事务,同时查询数据库中的数据,此时查询到的数据只有 javaboy 和 itboyhub。

  • 执行 A 窗口的前两行,向数据库中添加一个名为 zhangsan 的用户,注意不用提交事务。

  • 执行 B 窗口的第二行,由于脏读问题,此时可以查询到 zhangsan 这个用户。

  • 执行 B 窗口的第三行,去删除 name 为 zhangsan 的记录,这个时候删除就会出问题,虽然在 B 窗口中可以查询到 zhangsan,但是这条记录还没有提交,是因为脏读的原因才看到了,所以是没法删除的。此时就产生了幻觉,明明有个 zhangsan,却无法删除。

这就是幻读

看了上面的案例,大家应该明白了脏读不可重复读以及幻读各自是什么含义了。

2.3 READ COMMITTED

和 READ UNCOMMITTED 相比,READ COMMITTED 主要解决了脏读的问题,对于不可重复读和幻象读则未解决。

将事务的隔离级别改为READ COMMITTED之后,重复上面关于脏读案例的测试,发现已经不存在脏读问题了;重复上面关于不可重复读案例的测试,发现不可重复读问题依然存在。

上面那个案例不适用于幻读的测试,我们换一个幻读的测试案例。

还是两个窗口 A 和 B,将 B 窗口的隔离级别改为READ COMMITTED

然后在 A 窗口输入如下测试 SQL:

START TRANSACTION;insert into account(name,balance) values('zhangsan',1000);COMMIT;

在 B 窗口输入如下测试 SQL:

START TRANSACTION;SELECT * from account;insert into account(name,balance) values('zhangsan',1000);COMMIT;

测试方式如下:

  • 首先执行 B 窗口的前两行 SQL,开启事务并查询数据,此时查到的只有 javaboy 和 itboyhub 两个用户。

  • 执行 A 窗口的前两行 SQL,插入一条记录,但是并不提交事务。

  • 执行 B 窗口的第二行 SQL,由于现在已经没有了脏读问题,所以此时查不到 A 窗口中添加的数据。

  • 执行 B 窗口的第三行 SQL,由于 name 字段唯一,因此这里会无法插入。此时就产生幻觉了,明明没有 zhangsan 这个用户,却无法插入 zhangsan。

2.4 REPEATABLE READ

和 READ COMMITTED 相比,REPEATABLE READ 进一步解决了不可重复读的问题,但是幻象读则未解决。

REPEATABLE READ 中关于幻读的测试和上一小节基本一致,不同的是第二步中执行完插入 SQL 后记得提交事务。

由于 REPEATABLE READ 已经解决了不可重复读,因此第二步即使提交了事务,第三步也查不到已经提交的数据,第四步继续插入就会出错。

注意,REPEATABLE READ 也是 InnoDB 引擎的默认数据库事务隔离级别

2.5 SERIALIZABLE

SERIALIZABLE 提供了事务之间最大限度的隔离,在这种隔离级别中,事务一个接一个顺序的执行,不会发生脏读、不可重复读以及幻象读问题,最安全。

如果设置当前事务隔离级别为 SERIALIZABLE,那么此时开启其他事务时,就会阻塞,必须等当前事务提交了,其他事务才能开启成功,因此前面的脏读、不可重复读以及幻象读问题这里都不会发生。

3. 总结

总的来说,隔离级别和脏读、不可重复读以及幻象读的对应关系如下:

隔离级别 脏读 不可重复读 幻象读
READ UNCOMMITTED 允许 允许 允许
READ COMMITED 不允许 允许 允许
REPEATABLE READ 不允许 不允许 允许
SERIALIZABLE 不允许 不允许 不允许

性能关系如图:

Let you understand the transaction isolation level in MySQL through cases

好了,这篇文章就和小伙伴们先说这么多,大家不妨写几行 SQL 试一试。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Let you understand the transaction isolation level in MySQL through cases. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete