Mysql next-locking 間隙鎖
阿新 • • 發佈:2019-01-29
mysql> select x,a from t where x>200 for update;
+-------+------+
| x | a |
+-------+------+
| 202 | NULL |
| 202 | NULL |
| 300 | NULL |
| 300 | NULL |
| 500 | NULL |
| 3040 | NULL |
| 3040 | NULL |
| 30401 | NULL |
+-------+------+
8 rows in set (0.00 sec)
mysql> update t set a=600 where x=501;
Query OK, 1 row affected (8.55 sec)
Rows matched: 1 Changed: 1 Warnings: 0
這樣就會鎖住 [500,3040) 測試中501下界是500,上界是3039
此時插入x值[500,3030)範圍時,會等待
session 2:
mysql> insert into t(x) values(500);
等待
mysql> insert into t(x) values(400);
Query OK, 1 row affected (0.00 sec) 400不在這個範圍中間,可以插入
mysql> insert into t(x) values(600);
等待
mysql> insert into t(x) values(3040);
Query OK, 1 row affected (0.00 sec) 3040不在這個範圍
間隙鎖是否起作用,還有一個引數 innodb_locks_unsafe_for_binlog
起作用。
預設是關閉的,即間隙鎖是起作用的,
設定為1 ,間隙鎖不起作用,但是開啟這個引數會對binlog的記錄順序產生一定影響,從而在複製和恢復時,就會導致資料不一致,這點需要注意。
這個引數不能動態修改,只能在引數檔案中設定,然後重啟mysql服務生效。