舉個例子:
CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`class` int(11) DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
資料為:
select * from t2;
+------+-------+
| id | class |
+------+-------+
| 1 | 1 |
| 2 | 2 |
| 10 | 10 |
+------+-------+
事務1:
select * from t2 where id>4 and id<5 for update;
事務2
select * from t2 where id=2 for update;正常
select * from t2 where id=10 for update;阻塞
也就是鎖住的范圍是(2,10]。這個符合next-key lock。
但是
insert into t2 values(2, 2);阻塞
insert into t2 values(10, 10);正常
剛好和select for update相反。這個是什么原因導致的呢?
insert intention lock是索引值不存在的時候,會在插入前檢查gap鎖,但是存在的情況下,是如何檢查的呢?
還有為什么mysql會將gap范圍擴展到前后有索引值的范圍呢?并且加上了后一個索引值的行索引,也就是next-key lock。這種設計是出于什么原因考慮的?
自己又做了一些嘗試,放到個人主頁里了,歡迎批評指點:
http://wangyoubin.com/?p=105
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/13336.html
標籤:MySQL
