锁定共享模式锁定整个 table
LOCK IN SHARE MODE locks entire table
SELECT ... LOCK IN SHARE MODE sets a shared mode lock on any rows that are read. Other sessions can read the rows, but cannot modify them until your transaction commits. If any of these rows were changed by another transaction that has not yet committed, your query waits until that transaction ends and then uses the latest values.
但是,一些实验表明它锁定的行数多于读取的行数。
CREATE TABLE example (a int);
START TRANSACTION;
SELECT a FROM example WHERE a = 0 LOCK IN SHARE MODE;
然后在另一个连接上
INSERT INTO example VALUES (1);
后面连接块上锁
似乎 LOCK IN SHARE MODE
比 "any rows that are read" 锁定更多。
LOCK IN SHARE MODE
锁定什么?
确保您在 a
列上有一个索引。否则,为了计算 WHERE a = 0
,它必须读取 table 中的每一行,然后在读取时在每一行上设置一个锁。
ALTER TABLE example ADD INDEX (a);
SELECT ... LOCK IN SHARE MODE sets a shared mode lock on any rows that are read. Other sessions can read the rows, but cannot modify them until your transaction commits. If any of these rows were changed by another transaction that has not yet committed, your query waits until that transaction ends and then uses the latest values.
但是,一些实验表明它锁定的行数多于读取的行数。
CREATE TABLE example (a int);
START TRANSACTION;
SELECT a FROM example WHERE a = 0 LOCK IN SHARE MODE;
然后在另一个连接上
INSERT INTO example VALUES (1);
后面连接块上锁
似乎 LOCK IN SHARE MODE
比 "any rows that are read" 锁定更多。
LOCK IN SHARE MODE
锁定什么?
确保您在 a
列上有一个索引。否则,为了计算 WHERE a = 0
,它必须读取 table 中的每一行,然后在读取时在每一行上设置一个锁。
ALTER TABLE example ADD INDEX (a);