【发布时间】:2020-12-15 11:24:33
【问题描述】:
我使用 mysql 5.6。然后我运行下面的查询。
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`passport_id` varchar(20) NOT NULL,
`age` int(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `passport_id` (`passport_id`),
KEY `idx_user_age` (`age`)
) ENGINE=InnoDB ;
insert into user values(1, 'A', 'A1', 1), (2, 'B', 'B1', 1), (3, 'C', 'C3', 1),
(4, 'A', 'A4', 2), (5, 'B', 'B5', 2), (6, 'C', 'C6', 2);
set autocommit = 0;
select * from user where age = 1 for update;
我希望以上select...for update 查询锁定3 行。因为查询返回 3 行。但是select * from information_schema.innodb_trx; 查询的trx_rows_locked数据是7,连总行数也是6。
另外,下面的查询只返回一行。但是trx_rows_locked 是 2。
select * from user where age = 1 and passport_id = 'A1' for update;
Mysql文档解释trx_row_locked这样The approximate number or rows locked by this transaction. 。但我不明白为什么trx_row_locked 有大概的数字。
以及如何获得正确的锁定行数?
【问题讨论】:
-
也许:(1)两个唯一键; (2)“间隙锁定”
-
能否详细解释一下
-
你能显示
select ... from innodb_trx的完整结果吗