select ... for update& lock in share mode加锁情况

今天测试了下两种情况下加锁情况,怎么加锁个数不一样。for update的话,二级索引对应聚集索引会加锁,lock in share mode的话,不会加。

for update对应聚集索引加了锁,但是lock in share mode没加


1、表结构:
CREATE TABLE `c` (
  `id1` int(11) NOT NULL DEFAULT '0',
  `id2` int(11) DEFAULT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id1`),
  KEY `id2` (`id2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 
2、数据
MariaDB [mytest]> select *from c;
+-----+------+------+
| id1 | id2  | id3  |
+-----+------+------+
|   6 |    1 |    2 |
|   7 |    2 |    5 |
|   8 |    3 |    5 |
|   9 |    4 |    5 |
|  10 |    5 |    5 |
+-----+------+------+
5 rows in set (0.00 sec)
3、对应执行计划:
MariaDB [mytest]> explain select id1 from c where id2<2  lock in share mode;
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| id   | select_type | table | type  | pos

你可能感兴趣的:(MySQL源码分析)