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日