在引擎为 InnoDB 的 table 中,最坏情况下将锁定多少行?
How many rows will be locked at worst in a table whose engine is InnoDB?
MySQL社区服务器,服务器版本:5.6.24
mysql> show create table user\G
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`number` int(11) DEFAULT NULL,
KEY `idx_number` (`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> SELECT * FROM user;
+--------+
| number |
+--------+
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 14 |
+--------+
6 rows in set (0.00 sec)
tableuser
的引擎是InnoDB。
问题一:
当执行以下语句时:
select * from user where number = 10 for update;
,最坏情况下会锁定多少行?多于一排?
问题二:
当执行以下语句时:
select * from user where number = 14 for update;
,最坏情况下会锁定多少行?超过两行 ?
因为这个字段被索引所以在第一种情况下只有单行会被阻塞,在第二种情况下 2 行因为 innodb 使用基于索引的行级锁定。
因为字段被索引所以在 INNODB 的情况下
回答 1
只有 1 行被锁定。
答案 2
只有 2 行被锁定。
MySQL社区服务器,服务器版本:5.6.24
mysql> show create table user\G
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`number` int(11) DEFAULT NULL,
KEY `idx_number` (`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> SELECT * FROM user;
+--------+
| number |
+--------+
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 14 |
+--------+
6 rows in set (0.00 sec)
tableuser
的引擎是InnoDB。
问题一:
当执行以下语句时:
select * from user where number = 10 for update;
,最坏情况下会锁定多少行?多于一排?
问题二:
当执行以下语句时:
select * from user where number = 14 for update;
,最坏情况下会锁定多少行?超过两行 ?
因为这个字段被索引所以在第一种情况下只有单行会被阻塞,在第二种情况下 2 行因为 innodb 使用基于索引的行级锁定。
因为字段被索引所以在 INNODB 的情况下
回答 1
只有 1 行被锁定。
答案 2
只有 2 行被锁定。