锁的范围是根据sql扫描范围来定(正常是根据使用到的索引)。
事务一select id from b where b1 like '12%'的这条SQL正常应该要使用索引b1来锁定所有以12开头的行,也就是12,121,122...等1111行的数据,其它行就不锁定。
explain看下↓dba test>explain select id from b where b1 like '12%' ;
+----+-------------+-------+------------+-------+---------------+------+---------+------+--------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+------+---------+------+--------+----------+--------------------------+
| 1 | SIMPLE | b | NULL | index | b1 | b1 | 5 | NULL | 100256 | 11.11 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+------+---------+------+--------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
有使用到key b1,但扫描的rows 100256!!!
说明什么,全表扫描,锁住了整张表,也就是事务二的b1=1也会被锁住。
为什么全表扫描?因为b1的类型是int,而事务一中where条件需要转换成字符串去匹配,存在隐式转换,导致使用不到索引,只能全表扫。
怎么证明只锁事务中使用索引的指定行?把b1的类型改成varchar(10),再来执行你上面的三个事务,看事务二和三是否还阻塞住。(我自测不会)