如何理解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的一致性读问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。

文章名称:如何理解MySQL的一致性读
链接URL:https://www.cdcxhl.com/article44/ggosee.html

成都网站建设公司_创新互联,为您提供定制开发网站改版营销型网站建设手机网站建设网站制作网站收录

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发