zoukankan      html  css  js  c++  java
  • innodb临键锁锁定范围

    临键锁解决当前读的幻读问题(通过锁定范围,使另外一个事务不能插入),mvcc解决不加锁读的幻读问题。

    create table test(
    `id` int,
    `grade` char(1),
    `name` varchar(20),
    primary key (`id`),
    key grade_idx (`grade`)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;


    insert into test values(1,'a','xiaoa');
    insert into test values(4,'e','xiaoe');
    insert into test values(8,'l','xiaol');
    insert into test values(12,'o','xiaoo');
    insert into test values(16,'r','xiaor');
    insert into test values(20,'u','xiaou');
    insert into test values(24,'w','xiaow');
    insert into test values(28,'z','xiaoz');

    #############################
    insert into test values(1,'a','xiaoa'); (-,a]
    insert into test values(4,'e','xiaoe'); (a,e]
    insert into test values(8,'l','xiaol'); (e,l]
    insert into test values(12,'o','xiaoo'); (l,o]
    insert into test values(16,'r','xiaor'); (o,r]
    insert into test values(20,'u','xiaou'); (r,u]
    insert into test values(24,'w','xiaow'); (u,w]
    insert into test values(28,'z','xiaoz'); (w,z]
                    (z,+)

    innodb 默认才用临键锁,
    如果查询没有命中索引,则退化为表锁;
    如果等值查询唯一索引且命中唯一1条记录,则退化为行锁;
    如果等值查询唯一索引且没有命中记录,则退化为临近结点的间隙锁;

    如果范围查询唯一索引或查询非唯一索引且命中记录,则锁定所有命中行的临键锁 ,并同时锁定最大记录行下一个区间的间隙锁。
    如果范围查询非唯一索引且没有命中记录,退化为临近结点的间隙锁(包括结点也被锁定)。

    注意:被锁定的范围其他事务无法进行插入和删除操作,但可以select..for update;

    参考:https://www.cnblogs.com/crazylqy/p/7773492.html

    session 2(id=3可插入;id=108无法插入,存在gap lock;id=123的记录无法select..in share mode,因为该记录上存在record lock;id=125可以被select..in share mode和update,这点比较奇怪,应该这也算是当前读,不过后来查看官方文档得知,gap锁只会阻塞insert操作,因为gap间隙中是不存在任何记录的,除了insert操作,其他的操作结果应该都等价于空操作,mysql就不去阻塞它了)

  • 相关阅读:
    KubeSphere 社区开源负载均衡器 Porter 进入 CNCF 云原生全景图
    The Overview of KubeSphere 3.0
    对象存储在无人驾驶高精度地图的场景实践
    谁来打通混合云“最后一公里”?唯有容器混合云
    QingStor 对象存储架构设计及最佳实践
    如何通过IAM打造零信任安全架构
    智能家居巨头 Aqara 基于 KubeSphere 打造物联网微服务平台
    微信小程序集成jenkins自动打码
    windows运行python,提示import win32file ImportError: DLL load failed: 找不到指定的程序。
    centos7搭建easy-mock服务
  • 原文地址:https://www.cnblogs.com/shijianchuzhenzhi/p/12300661.html
Copyright © 2011-2022 走看看