V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
main1234
V2EX  ›  MySQL

对于 Gap 锁的一些疑问

  •  
  •   main1234 · 68 天前 · 1134 次点击
    这是一个创建于 68 天前的主题,其中的信息可能已经有所发展或是发生改变。
    事务隔离级别为 RR 可重复读
    表结构和数据如下

    mysql> show create table test;
    +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table |
    +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
    | test | CREATE TABLE `test` (
    `id` int(11) NOT NULL,
    `num` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `num` (`num`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.01 sec)

    mysql> insert into test values (10, 10), (20, 20), (30, 30), (40, 40), (50, 50)
    mysql> select * from test;
    +----+-----+
    | id | num |
    +----+-----+
    | 10 | 10 |
    | 20 | 20 |
    | 30 | 30 |
    | 40 | 40 |
    | 50 | 50 |
    +----+-----+
    5 rows in set (0.05 sec)


    事务 A 的 sql 如下

    mysql> begin;
    Query OK, 0 rows affected (0.01 sec)

    mysql> select num from test where num > 10 and num < 15 for update;

    事务 B 的 sql 如下
    mysql> begin;
    Query OK, 0 rows affected (0.03 sec)

    mysql> insert into test values(120, 31);


    此时事务 B 会阻塞,我理解事务 A 会添加 GAP 锁,我理解 GAP 的范围是[10,20],为什么事务 B 会阻塞??
    9 条回复    2024-02-20 13:18:43 +08:00
    main1234
        1
    main1234  
    OP
       68 天前
    show engine innodb status 看到的事务为插入意向锁阻塞
    lock_mode X insert intention waiting
    wangxin3
        2
    wangxin3  
       68 天前
    我测了下 mysql 8.0.29 没有你说的问题,间隙锁范围是[10,20]
    wangxin3
        3
    wangxin3  
       68 天前
    @wangxin3 更正下间隙锁的范围是[10,20)
    main1234
        4
    main1234  
    OP
       68 天前
    @wangxin3 我也发现了,我用 5.6.23 会出现这样的问题,即使 sql 为 insert into test values(999, 999);也会阻塞
    main1234
        5
    main1234  
    OP
       68 天前
    5.6.23 会出现,8 不会有这样的问题,是那块做了修改么
    charmsongo
        6
    charmsongo  
       67 天前
    版本过低了?
    main1234
        7
    main1234  
    OP
       67 天前
    @charmsongo 应该是
    Pythoner666666
        8
    Pythoner666666  
       67 天前
    5.7 不会
    wangxin3
        9
    wangxin3  
       67 天前
    间隙锁的范围随着版本变化一直在改,不必纠结了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   907 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:57 · PVG 03:57 · LAX 12:57 · JFK 15:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.