幻讀概念:
幻讀是指在同一事務下當A用戶讀取某一範圍的數據行時,B事務在該範圍內插入了新行,當A用戶再讀取該範圍的數據行時,會發現有新的“幻影”行(即讀取到了B事務插入的數據)。 即違背事務隔離性要求。
爲解決這個問題,出現了謂詞鎖(predict lock)。
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
session 2
root@localhost(zhp) 21:46>insert into test_group values(5,'test5');
Query OK, 1 row affected (0.00 sec)
root@localhost(zhp) 22:20>commit;
Query OK, 0 rows affected (0.01 sec)
session 1
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
root@localhost(zhp) 22:21>commit;
Query OK, 0 rows affected (0.00 sec)
root@localhost(zhp) 22:21>select * from test_group where id<10;
+----+-------+
| id | name |
+----+-------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
幻讀是指在同一事務下當A用戶讀取某一範圍的數據行時,B事務在該範圍內插入了新行,當A用戶再讀取該範圍的數據行時,會發現有新的“幻影”行(即讀取到了B事務插入的數據)。 即違背事務隔離性要求。
爲解決這個問題,出現了謂詞鎖(predict lock)。
next-key locking算法就是爲了解決幻讀問題。
測試:
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
session 2
root@localhost(zhp) 21:46>insert into test_group values(5,'test5');
Query OK, 1 row affected (0.00 sec)
root@localhost(zhp) 22:20>commit;
Query OK, 0 rows affected (0.01 sec)
session 1
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
root@localhost(zhp) 22:21>commit;
Query OK, 0 rows affected (0.00 sec)
root@localhost(zhp) 22:21>select * from test_group where id<10;
+----+-------+
| id | name |
+----+-------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
| 5 | test5 |
由此可知,在rr隔離級別下,解決了幻讀問題。