mvcc中的read_view

发布时间:2020-08-04 12:39:12 作者:lff1530983327
来源:ITPUB博客 阅读:244
innodb的mvcc和read view
最近读High Performance MySQL,里面提到了innodb事务隔离级别是REPEATABLE-READ时,有这样一段话
引用

SELECT
InnoDB must examine each row to ensure that it meets two criteria:
a. InnoDB must find a version of the row that is at least as old as the transaction
(i.e., its version must be less than or equal to the transaction’s version). This
ensures that either the row existed before the transaction began, or the trans-
action created or altered the row.
b. The row’s deletion version must be undefined or greater than the transaction’s
version. This ensures that the row wasn’t deleted before the transaction began.
Rows that pass both tests may be returned as the query’s result.

来验证一下
show create table 20130302t1;

 CREATE TABLE `20130302t1` (
  `id` int(11) NOT NULL,
  `b` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

表中有数据(1,1)
autocommit为false, tx_isolation 是REPEATABLE-READ
考虑以下两种情况
情况1
session A session B
start transaction;(A)
start transaction
update 20130302t1 set b=2 where id=1;
commit;
select * from 20130302t1;(B)

B处结果为(1,2),似乎不符合那段话里的a条件,A事务看到了transaction version更大的B事务

情况2
session A     session B
                           start transaction
                          update 20130302t1 set b=2 where id=1;
start transaction
select * from 20130302t1;
                            commit;
select * from 20130302t1;(C)


C处结果为(1,1),也就是说,A事务没有看到transaction version更小的B事务

是不是那段话有问题呢,后来终于找到了官方的文档,innodb通过read view来确定一致性读时的数据库snapshot,innodb的read view确定一条记录能否看到,有两条法则
1 看不到read view创建时刻以后启动的事务
2 看不到read view创建时活跃的事务

引用
Rule 1: When the read view object is created it notes down the smallest transaction identifier that is not yet used as a transaction identifier (trx_sys_t::max_trx_id).   The read view calls it the low limit. So the transaction using the read view must not see any transaction with identifier greater than or equal to this low limit.

Rule 2: The transaction using the read view must not see a transaction that was active when the read view was created.


在情况1中,代码A处并没有创建read view,read view是在代码B处创建的.
如果把A处代码改为 START TRANSACTION WITH CONSISTENT SNAPSHOT;
才会创建read view,使得代码B返回(1,1)

在情况2中,B事务在A事务创建read view时处于ACTIVE状态,所以B事务不会被A事务看到.


这篇文章还提到了mysql5.6 在read only事务的优化,值得一看
推荐阅读:
  1. PostgreSQL的MVCC vs InnoDB的MVCC
  2. MVCC详细讲解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mvcc read view

上一篇:SAP使用实例分析

下一篇:重新审视Netflix:祛魅流媒体成功秘法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》