如何理解MySQL的一致性读

56次阅读
没有评论

共计 2888 个字符,预计需要花费 8 分钟才能阅读完成。

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

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

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

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计2888字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)