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);