Mysql加鎖過程詳解(3)-關於mysql 幻讀理解

crazyYong發表於2017-09-30

 

 

Mysql加鎖過程詳解(2)-關於mysql 幻讀理解出現了幻讀,那麼不是說mysql的重複讀解決了幻讀的麼?

 

那麼,InnoDB指出的可以避免幻讀是怎麼回事呢?

http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html

By default, InnoDB operates in REPEATABLE READ transaction isolation level and with the innodb_locks_unsafe_for_binlog system variable disabled. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows (see Section 13.6.8.5, “Avoiding the Phantom Problem Using Next-Key Locking”).

準備的理解是,當隔離級別是可重複讀,且禁用innodb_locks_unsafe_for_binlog的情況下,在搜尋和掃描index的時候使用的next-key locks可以避免幻讀。

關鍵點在於,是InnoDB預設對一個普通的查詢也會加next-key locks,還是說需要應用自己來加鎖呢?如果單看這一句,可能會以為InnoDB對普通的查詢也加了鎖,如果是,那和序列化(SERIALIZABLE)的區別又在哪裡呢?

MySQL manual裡還有一段:

13.2.8.5. Avoiding the Phantom Problem Using Next-Key Locking (http://dev.mysql.com/doc/refman/5.0/en/innodb-next-key-locking.html)

To prevent phantoms, InnoDB uses an algorithm called next-key locking that combines index-row locking with gap locking.

You can use next-key locking to implement a uniqueness check in your application: If you read your data in share mode and do not see a duplicate for a row you are going to insert, then you can safely insert your row and know that the next-key lock set on the successor of your row during the read prevents anyone meanwhile inserting a duplicate for your row. Thus, the next-key locking enables you to “lock” the nonexistence of something in your table.

我的理解是說,InnoDB提供了next-key locks,但需要應用程式自己去加鎖。manual裡提供一個例子:

SELECT * FROM child WHERE id > 100 FOR UPDATE;

這樣,InnoDB會給id大於100的行(假如child表裡有一行id為102),以及100-102,102+的gap都加上鎖。

可以使用show innodb status來檢視是否給表加上了鎖。

 

下面看列子

 

例子1

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(1,1);

 

 

鎖住了

INSERT test VALUES(1,1);

 

成功

 

COMMIT

 

 

 

 

 

COMMIT

 

避免幻讀可以select鎖住,再insert

 

 

 例子2

 

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

連2也被鎖住了?

INSERT test VALUES(1,1);

 

成功

 

COMMIT

 

 

 

 

這次提交成功

 

COMMIT

其他嘗試,這種情況無論插入2還是5都被鎖住等等

 

 例子3

 

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

COMMIT

COMMIT

成功

 

COMMIT

 

 

COMMIT

 

 

 

 

 例子 4

 

 

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='2' FOR UPDATE;

 

 

 

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

 

INSERT test VALUES(5,5);

 

 

 

COMMIT

COMMIT

 

例子 5

 

 

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

INSERT test VALUES(5,5);

 

 

插入5成功了

 

UPDATE test SET b=33 WHERE a='3'

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

2也可以

 

UPDATE test SET b=11 WHERE a='1'

 

 

1鎖住了

COMMIT

 

 

 

 

 

COMMIT

SELECT * FROM test

SELECT * FROM test

 

 

 

 

 

 

 

以上例子說明,forupdate時候,id為主鍵,RR策略時候,鎖住了的條件符合的行,但是如果條件找不到任何列,鎖住的是整個表,(主鍵,唯一索引,非唯一索引,(insert,update對於gab鎖不通),參考第一章,第七章,第九章

 

 

 ------------------------------------------------------------------

  再來看大神的解釋 :連結: http://blog.bitfly.cn/post/mysql-innodb-phantom-read/

 

再看一個實驗,要注意,表t_bitfly裡的id為主鍵欄位。實驗三:

t Session A                 Session B
|
| START TRANSACTION;        START TRANSACTION;
|
| SELECT * FROM t_bitfly
| WHERE id<=1
| FOR UPDATE;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           INSERT INTO t_bitfly
|                           VALUES (2, 'b');
|                           Query OK, 1 row affected
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           INSERT INTO t_bitfly
|                           VALUES (0, '0');
|                           (waiting for lock ...
|                           then timeout)
|                           ERROR 1205 (HY000):
|                           Lock wait timeout exceeded;
|                           try restarting transaction
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           COMMIT;
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
v

可以看到,用id<=1加的鎖,只鎖住了id<=1的範圍,可以成功新增id為2的記錄,新增id為0的記錄時就會等待鎖的釋放。

MySQL manual裡對可重複讀裡的鎖的詳細解釋:

http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read

For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE),UPDATE, and DELETE statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition. For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it. For other search conditions, InnoDB locks the index range scanned, using gap locks or next-key (gap plus index-record) locks to block insertions by other sessions into the gaps covered by the range.

------

一致性讀和提交讀,先看實驗,實驗四:

t Session A                      Session B
|
| START TRANSACTION;             START TRANSACTION;
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
|                                INSERT INTO t_bitfly
|                                VALUES (2, 'b');
|                                COMMIT;
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
|
| SELECT * FROM t_bitfly LOCK IN SHARE MODE;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| |  2 | b     |
| +----+-------+
|
| SELECT * FROM t_bitfly FOR UPDATE;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| |  2 | b     |
| +----+-------+
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
v

 

如果使用普通的讀,會得到一致性的結果,如果使用了加鎖的讀,就會讀到“最新的”“提交”讀的結果。

本身,可重複讀和提交讀是矛盾的。在同一個事務裡,如果保證了可重複讀,就會看不到其他事務的提交,違背了提交讀;如果保證了提交讀,就會導致前後兩次讀到的結果不一致,違背了可重複讀。

可以這麼講,InnoDB提供了這樣的機制,在預設的可重複讀的隔離級別裡,可以使用加鎖讀去查詢最新的資料。

http://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html

If you want to see the “freshest” state of the database, you should use either the READ COMMITTED isolation level or a locking read:
SELECT * FROM t_bitfly LOCK IN SHARE MODE;

 

 

結論:MySQL InnoDB的可重複讀並不保證避免幻讀,需要應用使用加鎖讀來保證。而這個加鎖度使用到的機制就是next-key locks。

 

結論:mysql 的重複讀解決了幻讀的現象,但是需要 加上 select for update/lock in share mode 變成當前讀避免幻讀,普通讀select存在幻讀

 

相關文章