• mysql update 等值 锁区间


    CREATE TABLE `p300` (
      `sn` int(11) NOT NULL AUTO_INCREMENT,
      `uuid` int(11) NOT NULL,
      `mobilePhone` int(11) NOT NULL,
      PRIMARY KEY (`sn`),
      KEY `p100_idx1` (`uuid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
    
    mysql> insert into p300 select sn,sn,mobilePhone from p300;
    Query OK, 17736 rows affected (0.20 sec)
    Records: 17736  Duplicates: 0  Warnings: 0
    
    
    
    Session 1:
    
    mysql> select connection_id();  
    +-----------------+
    | connection_id() |
    +-----------------+
    |               1 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> select * from p300 where sn<20;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  1 |    1 |         111 |
    |  3 |    3 |        3333 |
    |  5 |    5 |        5555 |
    | 10 |   10 |         222 |
    +----+------+-------------+
    4 rows in set (0.00 sec)
    
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from p300 where uuid = 6 for update;
    Empty set (0.00 sec)
    
    
    
    Session 2:
    mysql> select connection_id();
    +-----------------+
    | connection_id() |
    +-----------------+
    |               2 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> insert into p300 values(1,1,1);
    ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
    mysql> insert into p300(uuid,mobilePhone) values(1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(2,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(3,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(4,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(5,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(6,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(7,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(8,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(9,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(10,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(11,1);
    Query OK, 1 row affected (0.00 sec)
    
    
    
    虽然select * from p300 where uuid = 6 for update; uud=6并不存在,但是还是锁了[5,10)
    
    
    
    
    那么有值的情况 mysql 是怎么锁区间的呢?
    
    
    Session 1:
    mysql> select connection_id(); 
    +-----------------+
    | connection_id() |
    +-----------------+
    |               1 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> select * from p300 where sn<20;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  1 |    1 |         111 |
    |  3 |    3 |        3333 |
    |  5 |    5 |        5555 |
    | 10 |   10 |         222 |
    +----+------+-------------+
    4 rows in set (0.00 sec)
    
    
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>  select * from p300 where uuid =5 for update;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  5 |    5 |        5555 |
    +----+------+-------------+
    1 row in set (0.00 sec)
    
    
    
    
    Session 2:
    
    mysql> select connection_id(); 
    +-----------------+
    | connection_id() |
    +-----------------+
    |               2 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(2,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(3,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(3,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(4,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(5,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(6,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(7,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(8,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(9,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(10,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(11,1);
    Query OK, 1 row affected (0.00 sec)
    
    
    select * from p300 where uuid =5 for update; 有值 此时锁住的区间是[5,10);
    

  • 相关阅读:
    机器重装后几个要记的问题
    JS中split用法和数组中元素的删除
    免费软件的盈利方式
    web程序的发布及相关问题
    无法直接启动带有“类库输出类型”的项目
    System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
    orcale不同版本数据导入、导出及库版本查询
    VSc# web程序:gridview保存Excel文件遇到的问题
    油田生产中的几个“三”
    web跨页面传值——FORM表单(c#)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349953.html
Copyright © 2020-2023  润新知