read view初探

innodb为实现MVCC所使用的内部快照,RR(REPEATABLE READ)隔离级别下在第一次查询时创建read view,RC(READ COMMITTED)隔离级别下会在每次查询时创建read view
以下测试在RR隔离级别下,数据库版本为5.7.20
1.

session A session B
start transaction;  
  start transaction;

select * from tab1;
Empty set

 
  insert into tab1 values (1,"1");

select * from tab1;
Empty set

 
  commit;

select * from tab1;
Empty set

 

commit;

 

select * from tab1;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
+------+------+

结论:在已经查询后,其他事务做的修改,在本事务不可见

2.

session A session B
truncate table tab1;  

start transaction;

 
  start transaction;
  insert into tab1 values (1,"1");
  commit;

select * from tab1;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
+------+------+

 

结论:尽管事务A比事务B先开始,但是第一次查询在B事务提交后,所以可以查询到结果

3.

session A session B
truncate table tab1;  

start transaction;

 
  start transaction;

select * from tab1;
Empty set (0.00 sec)

 
  insert into tab1 values (1,"1");
  commit;

select * from tab1;
Empty set (0.00 sec)

 

update tab1 set col2 = 11 where col1 = 1;

1 row affected

 

select * from tab1;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 11 |
+------+------+

 

结论:虽然事务A看不到事务B做的修改,但是修改也会影响事务B已经提交的数据,且修改发生后,下次查询会重新刷新read view

参考网址:

1. https://dev.mysql.com/doc/refman/5.6/en/innodb-consistent-read.html

2. http://kabike.iteye.com/blog/1820553

猜你喜欢

转载自www.cnblogs.com/wangzheng1024/p/9403361.html