设为首页 加入收藏

TOP

MySQL 一致性读 深入研究(一)
2015-11-21 01:27:39 来源: 作者: 【 】 浏览:0
Tags:MySQL 一致性 深入 研究
?
一致性读,又称为快照读。使用的是MVCC机制读取undo中的已经提交的数据。所以它的读取是非阻塞的。
?
相关文档:http://dev.mysql.com/doc/refman/5.6/en/innodb-consistent-read.html
?
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.
?
一致性读肯定是读取在某个时间点已经提交了的数据,有个特例:本事务中修改的数据,即使未提交的数据也可以在本事务的后面部分读取到。
?
1. RC 隔离 和 RR 隔离中一致性读的区别
?
根据隔离级别的不同,一致性读也是不一样的。不同点在于判断是否提交的“某个时间点”:
?
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.
?
文档中说的是:the first such read in that transaction。实际上实验的结果表明,并不是the first such read,而是事务中任何执行的第一条语句为snapshot的起始点,即使该条语句执行失败,也是以它的执行时间为snapshot的起始点。因为事务的起始点其实是以执行的第一条语句为起始点的,而不是以begin作为事务的起始点的。在该起始点之前提交的数据,就可以读取到。(原因应该是RR隔离级别是要支持可重复读的)
?
实验1:
?
sesseion A session B
mysql>?set?tx_isolation='repeatable-read';
Query?OK,?0?rows?affected?(0.00?sec)
?
mysql>?set?tx_isolation='repeatable-read';
Query?OK,?0?rows?affected?(0.00?sec)
?
mysql>?begin;
Query?OK,?0?rows?affected?(0.01?sec)
?
?
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
?
mysql>?insert?into?t1(c1,c2)?values(1,1);
Query?OK,?1?row?affected?(0.01?sec)
mysql>?select?*?from?t1;
+----+------+
|?c1?|?c2???|
+----+------+
|??1?|????1?|
+----+------+
1?row?in?set?(0.00?sec)
?
?
上面的实验说明:RR隔离级别下的一致性读,不是以begin开始的时间点作为snapshot建立时间点,而是以第一条语句的时间点作为snapshot建立的时间点。
?
实验2:
?
session A session B
mysql>?set?tx_isolation='repeatable-read'; mysql>?set?tx_isolation='repeatable-read';
?
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
mysql>?begin;
mysql>?set?i=1;
ERROR?1193?(HY000):?Unknown?system?variable?'i'
?
?
mysql>?insert?into?t1(c1,c2)?values(1,1);
Query?OK,?1?row?affected?(0.01?sec)
mysql>?select?*?from?t1;
+----+------+
|?c1?|?c2???|
+----+------+
|??1?|????1?|
+----+------+
1?row?in?set?(0.00?sec)
?
?
?
该使用说明:RR隔离级别下的一致性读,是以第一语句的执行点作为snapshot建立的时间点的,即使该语句执行失败了,也是如此。
?
实验3:
?
session A session B
mysql>?set?tx_isolation='repeatable-read';
mysql>?set?tx_isolation='repeatable-read';
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
mysql>?begin; ?
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
? mysql>?insert?into?t1(c1,c2)?values(1,1);
mysql>?select?*?from?t1;
Empty?set?(0.01?sec)
?
该实验中:session A 的第一条语句,发生在session B的 insert语句提交之前,所以session A中的第二条select还是不能读取到数据。因为RR中的一致性读是以事务中第一个语句执行的时间点作为snapshot建立的时间点的。而此时,session B的insert语句还没有执行,所以读取不到数据。
?
实验4:
?
session A session B
mysql>?set?tx_isolation='repeatable-read';
mysql>?set?tx_isolation='repeatable-read';
mysql>?select?*?from?t1;
Empty?set?(0.00?sec)
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL分析服务器状态 下一篇MySQL死锁问题实例分析及解决方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: