使用二级索引时mysql锁定间隙

时间:2014-05-11 10:00:28

标签: mysql transactions

假设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如何使用二级索引进行搜索?

非常感谢您的帮助。

0 个答案:

没有答案