【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    |

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

|  1 |    1 |    2 |

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

1 row in set (0.00 sec)




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 |

+----+

|  1 |

|  2 |

+----+

2 rows in set (0.00 sec)

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    | b    |

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

|  1 |    1 |    2 |

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

1 row in set (0.00 sec)




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    | b    |

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

|  1 |    1 |    2 |

|  2 |    2 |    4 |

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

2 rows in set (0.00 sec)

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    | b    |

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

|  1 |    1 |    2 |

|  2 |    2 |    4 |

|  3 |    3 |    5 |

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

3 rows in set (0.00 sec)

test [RW] 10:43:14 >update  ty set a = 5 where id=3;

Query OK, 1 row affected (4.23 sec)

Rows matched: 1  Changed: 1  Warnings: 0

test [RW] 10:44:30 >select * from ty;

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

| id | a    | b    |

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

|  1 |    1 |    2 |

|  2 |    2 |    4 |

|  3 |    5 |    5 |

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

3 rows in set (0.00 sec)



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  Changed: 1  Warnings: 0

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    | b    |

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

|  2 |    5 |    4 |

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

1 row in set (10.73 sec)

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    | b    |

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

|  1 |    2 |    3 |

|  2 |    5 |    4 |

|  3 |    6 |    7 |  <-- 本事务还未commit,已经可以查看其他会话最新插入的数据

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

3 rows in set (0.00 sec)

这个例子要特别说明一下,从上面的实验结果上来看RR事务隔离级别下,一致性快照的建立仅仅和select语句第一次执行有关,不管是不是相关表。而且 与其他 DML(insert,update,delete) 语句、for update 语句无关 ,在事务中第一次执行DML,后面的其他select 查询会是当前读即可获取到最新的数据。
   
3.2 RC模式 
RC 支持在本事务内读取到最新提交的数据,所以 RC 事务隔离级别下的一致性读取比RR模式下的要简单很多。每个事务构建自己的快照,不相互干扰,除非其他事务已经提交,有兴趣的朋友自己测试吧。
  1. 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模式作为事务隔离级别的。
参考文章
[1]  一致性读深入研究
[2]  官方文档

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-2144551/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22664653/viewspace-2144551/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值