假设InnoDB表vv具有以下结构:
CREATE TABLE `vv` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idx` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `vv_idx` (`idx`)
) ENGINE=InnoDB;
和
mysql> select * from vv;
+----+------+
| id | idx |
+----+------+
| 1 | 20 |
| 2 | 25 |
| 3 | 30 |
现在假设两个会话按顺序执行以下操作:
会话1:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from vv where idx = 25;
Query OK, 1 rows affected (0.00 sec)
会话2:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from vv where idx = 19;
Query OK, 1 rows affected (0.00 sec)
mysql> delete from vv where idx = 20; -- blocked
mysql> delete from vv where idx = 29; -- blocked
mysql> delete from vv where idx = 30;
Query OK, 1 rows affected (0.00 sec)
锁定间隙是[20, 30)
,为什么? mysql如何使用二级索引进行搜索?
非常感谢您的帮助。