MySQL 在不同的事务隔离级别下提供两种读模式 一致性读 (非加锁) , 当前读 (加锁读) 。当前读比较简单,本文主要研究一致性读取。
二 原理概念
官方概念
- "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. "
三 不同事务隔离级别的一致性读
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) |
3.2 RC模式
RC 支持在本事务内读取到最新提交的数据,所以 RC 事务隔离级别下的一致性读取比RR模式下的要简单很多。每个事务构建自己的快照,不相互干扰,除非其他事务已经提交,有兴趣的朋友自己测试吧。
- 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/