温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何理解MySQL的一致性读

发布时间:2021-11-16 09:54:16 来源:亿速云 阅读:217 作者:柒染 栏目:MySQL数据库

如何理解MySQL的一致性读,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一 前言
   MySQL 在不同的事务隔离级别下提供两种读模式 一致性读(非加锁)当前读(加锁读)。当前读比较简单,小编主要研究一致性读取。
二 原理概念

官方概念

  1. "A consistent read means that InnoDB uses multi-versioning to present to a query a snapshot of the database at a point in time. The query sees the changes made by transactions that committed before that point of time, and no changes made by later or uncommitted transactions.The exception to this rule is that the query sees the changes made by earlier statements within the same transaction. "

一致性读是指使用MVCC机制读取到某个事务已经提交的数据,其实是从undo里面获取的数据快照。不过也有特例: 在本事务内如果修改某个表之后的select 可以读取到该表最新的数据,后面的例子可以验证。   
三 不同事务隔离级别的一致性读
3.1 RR模式
从官方文档 "If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction."
 在RR模式下,同一个事务内的一致性读的快照都是基于第一次读取操作时所建立的。下面我们做测试进行对RR模式下一致性读进行解读。
a)RR模式下事务的起始点是以执行的第一条语句为起始点的,而不是以begin作为事务的起始点的。

session 1

session2

test [RW] 10:01:33 >begin;

Query OK, 0 rows affected (0.00 sec)



test [RW] 10:02:12 >begin;

Query OK, 0 rows affected (0.00 sec)


test [RW] 10:02:22 >select * from ty;

Empty set (0.00 sec)


test [RW] 10:02:36 >insert into ty(a,b) values(1,2);

Query OK, 1 row affected (0.00 sec)


test [RW] 10:02:51 >commit;

Query OK, 0 rows affected (0.00 sec)


test [RW] 10:02:33 >select * from ty;

+----+------+------+

| id | a






b)RR模式下的一致性读,是以第一条select语句的执行时间点作为snapshot建立的时间点的,即使是访问不同的表。

test [RW] 10:35:11 >begin;

Query OK, 0 rows affected (0.00 sec)

test [RW] 10:35:13 >select * from x;

+----+

| id |

+----+

|

test [RW] 10:34:32 >begin;

Query OK, 0 rows affected (0.00 sec)



test [RW] 10:34:51 >insert into ty(a,b) values(2,4);

Query OK, 1 row affected (0.00 sec)

test [RW] 10:35:39 >select * from ty;

+----+------+------+

| id | a





c)RR模式下,在本事务内如果修改某个表之后的对该表的select语句可以读取到该表最新的数据。

test [RW] 10:42:56 >begin;

Query OK, 0 rows affected (0.00 sec)


test [RW] 10:43:07 >select * from ty;

+----+------+------+

| id | a

test [RW] 10:35:34 >begin;

Query OK, 0 rows affected (0.00 sec)


test [RW] 10:43:25 >insert into ty(a,b) values(3,5);

Query OK, 1 row affected (0.00 sec)



test [RW] 10:43:38 >select * from ty;

+----+------+------+

| id | a

test [RW] 10:43:14 >update



d)RR模式下同一个事务内,第一次查询是当前读操作则后续查询可以查看最新的数据。

test [RW] 11:07:23 >begin;

Query OK, 0 rows affected (0.00 sec)

test [RW] 11:07:26 >update ty set a=5 where id=2;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1

test [RW] 11:07:31 >begin;

Query OK, 0 rows affected (0.00 sec)

test [RW] 11:07:33 >select * from ty where id=2 for update;

+----+------+------+

| id | a

test [RW] 11:07:36 >insert into ty(a,b) values(6,7);

Query OK, 1 row affected (0.00 sec)

test [RW] 11:07:55 >commit;

Query OK, 0 rows affected (0.00 sec)

test [RW] 11:07:58 >select * from ty;

+----+------+------+

| id | a

With READ COMMITTED isolation level, each consistent read within a transaction sets and reads its own fresh snapshot.

四 当前读

和一致性读不太一样 ,当前读需要使用select  xx  for update,或者 lock in share mode ,读取最新的数据并且锁定被访问的行,(RC 加行锁,RR加gap锁 唯一键除外) 不管另外一个事务是否提交,如果另外的事务已经获取了相关的锁,则 for update,lock in share mode 语句则继续等待直到其他事务释放锁,并且获取到最新的数据。

从上面的测试来看,RR模式下的一致性快照读会有比较多的特性(姑且叫做特性吧) 。RC模式本身支持不可重复读,能够查询到最新的其他事务最新提交的数据。基于上面的测试,还是比较推荐业务使用RC模式作为事务隔离级别的。

关于如何理解MySQL的一致性读问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI