MySQL Innodb行锁失败 与 explain 详解
已知
innodb
- 默认使用表锁
- 加索引,可能使用行锁,根据sql来定
- 锁行 = 锁索引
实际问题
锁表
explain select * from test_table where id = 1 for update;
id1 | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | test_table | ALL | pk_id | 4 | 25 | Using where |
锁行
explain select * from test_table where id = '1' for update;
id1 | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | test_table | const | pk_id | pk_id | 130 | const | 1 | 100 |
原因
id是varchar类型,使用where id = 1 ,类型转换导致没有使用索引,因此锁全表而不是行