老师,您好。假期的没跟上,今天补到了这节课,看了之后有几点不是太明白。望能解答一下。
1. 索引c上的锁算不算是行锁。假如索引c上的next-key lock为(0,5] (5,10],那么5算不算是c上的行锁?
2. 在案例六中,执行 “delete from t where c=10;” 语句,索引c上的next-key lock是(5,10],(10,10],(10,15)。那么主键索引上的锁是什么呢?是只有行锁,锁住的是 (10,10,10) 和 (30,10,30) 两行吗?
3. 也是在案例六中,session A不变,在session B中执行 “update t_20 set d=50 where c=5;”、“update t_20 set d=50 where c=15;”、“insert into t_20 values(40,15,40);”均执行成功,但执行“insert into t_20 values(50,5,50);” 时,却被阻塞。为什么呢?具体执行语句如下
session A
mysql> begin;
mysql> explain delete from t_20 where c=10;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 DELETE t_20 range c c 5 const 2 100 Using where
mysql> delete from t_20 where c=10;
session B
mysql> update t_20 set d=50 where c=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update t_20 set d=50 where c=15;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> insert into t_20 values(40,15,40);
Query OK, 1 row affected (0.00 sec)
mysql> explain insert into t_20 values(50,5,50);
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | INSERT | t_20 | NULL | ALL | c | NULL | NULL | NULL | NULL | NULL | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set (0.00 sec)
mysql> insert into t_20 values(50,5,50);
(block)
我使用的mysql版本是:5.7.23-0ubuntu0.16.04.1
show variables的结果太多,我截取了一部分,或许对您分析有帮助:
innodb_version 5.7.23
protocol_version 10
slave_type_conversions
tls_version TLSv1,TLSv1.1
version 5.7.23-0ubuntu0.16.04.1
version_comment (Ubuntu)
version_compile_machine x86_64
version_compile_os Linux
展开
作者回复: 1. Next-key lock 就是间隙锁➕行锁,所以包含=5这一行
2. 对
3. (c=5,id=50)是在这个gap里哦,你试试插入(1,5,50)对比一下。好问题