第二节:InnoDB的RR级别Consistent Nonlocking Reads实验

1)对于RR隔离:

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. This exception causes the following anomaly: If you update some rows in a table, a SELECT sees the latest version of the updated rows, but it might also see older versions of any rows. If other sessions simultaneously update the same table, the anomaly means that you might see the table in a state that never existed in the database.

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. You can get a fresher snapshot for your queries by committing the current transaction and after that issuing new queries.

官方文档地址:Consistent Nonlocking Reads

文档中说的是:the first such read in that transaction。实际上实验的结果表明,这里的 the first such read指的是:对同一个表或者不同表进行的第一次select语句就开始建立了该事务中一致性读的snapshot. 其它update, delete, insert 语句和一致性读snapshot的建立没有关系。在snapshot建立之后提交的数据,一致性读就读不到,之前提交的数据就可以读到。事务的起始点其实是以执行的第一条语句(不管是select, update, delete, insert)为起始点的,而不是以begin作为事务的起始点的。

第一个实验:

在这里插入图片描述
上面的实验说明:RR隔离级别下的一致性读,不是以begin开始的时间点作为snapshot建立时间点,而是以第一条select语句的时间点作为snapshot建立的时间点。

第二个实验:

在这里插入图片描述
上面的实验说明:RR隔离级别下的一致性读,是以第一条select语句的执行点作为snapshot建立的时间点的,即使是不同表的select语句。这里因为第一个会话在第二个会话insert之前对 t2 表执行了select,所以建立了snapshot,所以后面的select * from t1 不能读取到第二个会话insert的值。

第三个实验:

在这里插入图片描述
上面的实验说明:第一个会话中的第一条select语句(与上一个实现的不同点是:上一个实验查询的是不同表,本实验查询的是相同表),发生在第二个会话的 insert语句提交之前,所以第一个会话中的第二条select语句还是不能读取到数据。因为RR中的一致性读是以事务中第一个select语句执行的时间点作为snapshot建立的时间点的。

第四个实验:

在这里插入图片描述
上面的实验说明:本事务中进行修改的数据,即使没有提交,在本事务中的后面也可以读取到。且update 语句因为进行的是“当前读”,所以第一个会话它可以修改成功。

最后总结:

InnoDB的RR级别Consistent Nonlocking Reads中的snapshot是基于事务级别建立的,而之后我们要讲的InnoDB的RC级别Consistent Nonlocking Reads中的snapshot是基于statement级别建立的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值