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

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

select * from tab1;
Empty set

 

update tab1 set col2 ="22" where col1>=2;

2 rows affected

 

select * from tab1;
+------+------+
| col1 | col2 |
+------+------+
| 2 | 22 |
| 3 | 22 |
+------+------+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

結論:雖然事務A看不到事務B做的修改,但是修改也會影響事務B已經提交的數據,且修改發生后,被修改的記錄(盡管是其他事務提交的),也會變為對該事務可見

另外:

1.select ... for update和select ... lock in share mode(8.0是select ... for share)會重新生成read view

2.select ... with consistent snapshot不會讀到在本事務開始后提交的數據,即使第一次select是在其他事務提交后

 

 

參考網址:

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM