经常看到有人说,如果一个DML不能走索引,就要锁全表。这正确吗?
我曾经也这样以为,直到现在我才发现,对一半错一半吧。
答案应该是:
例如如下一个表:
mysql> show create table sam\G
*************************** 1. row ***************************
Table: sam
Create Table: CREATE TABLE `sam` (
`id` int(11) NOT NULL,
`col1` int(11) DEFAULT NULL,
`col2` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_c2` (`col2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select * from sam;
+----+------+------+
| id | col1 | col2 |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
+----+------+------+
3 rows in set (0.00 sec)
当执行update sam set col1=4 where col1=3
1.如果隔离级别是RR,那么确实会锁全表,是覆盖全表的next-key锁;
2.如果隔离级别是RC,那么只会锁住col1=3这一行,