MySQL,使用小于或等于锁定整个表?

时间:2017-07-03 04:17:30

标签: mysql transactions

当我使用c1<=4或甚至c1<4 OR c1=4时,事务A锁定整个表。 但如果我只使用其中一个,它就不会发生。这是为什么? “下一把钥匙锁”发生了什么?

DROP TABLE IF EXISTS t1;
CREATE TABLE t1(c1 INT AUTO_INCREMENT, PRIMARY KEY(c1));
INSERT INTO t1 (c1) VALUES (1),(5),(10);

# Transaction A and B starts at the same time

#teste 1
A does, SELECT * FROM t1 WHERE c1<=4 FOR UPDATE;
B does, SELECT * FROM t1 WHERE c1=10 FOR UPDATE; # blocked!

#teste 2
A does, SELECT * FROM t1 WHERE c1<4 FOR UPDATE;
B does, SELECT * FROM t1 WHERE c1=10 FOR UPDATE; # worked!

#teste 3
A does, SELECT * FROM t1 WHERE c1=4 FOR UPDATE;
B does, SELECT * FROM t1 WHERE c1=10 FOR UPDATE; # worked!

0 个答案:

没有答案