
mysql 版本 5.7.2
隔离级别 rr
mysql 间隙锁 !不是锁定行,也不是锁定某个列,是锁定对应的索引。测试表 tx_test ,age 字段加了索引了。
-- Table structure for tx_test
DROP TABLE IF EXISTS `tx_test`; CREATE TABLE `tx_test` ( `id` int(11) NOT NULL, `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL, `age` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `age` (`age`), KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -- Records of tx_test
BEGIN; INSERT INTO `tx_test` (`id`, `name`, `age`) VALUES (1, '123', 3); INSERT INTO `tx_test` (`id`, `name`, `age`) VALUES (2, '44', 4); INSERT INTO `tx_test` (`id`, `name`, `age`) VALUES (3, '55', 5); INSERT INTO `tx_test` (`id`, `name`, `age`) VALUES (4, '41', 50); COMMIT; #session1
begin;
SELECT * from tx_test;
update tx_test set name='aaaaa' where age >1 and age <20;
SELECT * from tx_test;
COMMIT;
#session2
begin;
SELECT * from tx_test;
INSERT INTO tx_test (id, name, age) VALUES (88, '41', 77);
SELECT * from tx_test;
COMMIT;
为什么 session2 的 INSERT INTO tx_test (id, name, age) VALUES (88, '41', 77); 会阻塞呢 。session1 的查询不是应该 锁定了 -无穷到 50 吗
1 movq 2023 年 2 月 10 日 第一个事务后面有 SELECT * from tx_test;,所以要全锁住,不然不是 repeatable read |
2 rqxiao OP @movq session1 改成 update tx_test set name='aaaaa' where age >1 and age <5;。session2 就可以插入 |