Oh-Why_not 2022-08-03 14:58
浏览 27
已结题

奇怪的MySQL锁机制

版本信息:MySQL 5.6.30 RR repeatable read

MySQL初始状态:

img

下面4种情况有人可以解释一下为什么吗?锁的范围是哪些?为什么有的会阻塞,有的不会阻塞?

**情况1. **

img

**情况2. **

img

情况3.

img

**情况4. **

img

  • 写回答

1条回答 默认 最新

  • Oh-Why_not 2022-08-03 15:00
    关注

    不知道为什么截图显示这么小,提问如下:

    MySQL 5.6.30 RR(repeatable read)

    MySQL initial state:

    DROP TABLE if EXISTS t;
    CREATE TABLE t(
    idint(11) NOT NULL,
    cint(11) DEFAULT NULL,
    d int(11) DEFAULT NULL,
    PRIMARY KEY (id),
    KEY c (c) )
    ENGINE=InnoDB;

    insert into t values(0,0,0),(5,5,5),(10,10,10),(15,15,15),(20,20,20),(25,25,25);

    下面4种情况有人可以解释一下为什么吗?锁的范围是哪些?为什么有的会阻塞,有的不会阻塞?

    1. execution order:
      session A:
      begin;
      select * from t where c>=15 and c<=20 order by c desc for update;
      session B:
      insert into t values(4,5,6); #not block
    1. execution order:
      session A:
      begin;
      select id from t where c>=15 and c<=20 order by c desc for update;
      session B:
      insert into t values(4,5,6); #blocked

    2. execution order:
      session A:
      begin;
      select * from t where c>=15 and c<=20 order by c desc lock in share mode;
      session B:
      insert into t values(4,5,6); #blocked

    1. execution order:
      session A:
      begin;
      select id from t where c>=15 and c<=20 order by c desc lock in share mode;
      session B:
      insert into t values(4,5,6); #not block
    评论

报告相同问题?

问题事件

  • 系统已结题 8月11日
  • 创建了问题 8月3日

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法