InnoDB中鎖的演算法(2)

來源:互聯網
上載者:User

標籤:表鎖   physical   二級索引   排序   層級   isolation   next   var   cal   

Ⅰ、上節回顧

session1:

([email protected]) [test]> select * from l;+---+------+------+------+| a | b    | c    | d    |+---+------+------+------+| 2 |    4 |    6 |    8 || 4 |    6 |    8 |   10 || 6 |    8 |   10 |   12 || 8 |   10 |   12 |   14 |+---+------+------+------+4 rows in set (0.00 sec)([email protected]) [test]> show variables like ‘tx_isolation‘;+---------------+-----------------+| Variable_name | Value           |+---------------+-----------------+| tx_isolation  | REPEATABLE-READ |+---------------+-----------------+1 row in set (0.01 sec)([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where b = 6 for update;+---+------+------+------+| a | b    | c    | d    |+---+------+------+------+| 4 |    6 |    8 |   10 |+---+------+------+------+1 row in set (0.09 sec)pk  2   4   6   8key 4   6   8   10二級索引鎖住的是(4,6]&&(6,8)主鍵鎖住的是4

session2:

([email protected]) [(test)]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> insert into l values (3,4,14,20);hang~~~

session1:

([email protected]) [(none)]> show engine innodb status\G...MySQL thread id 1087, OS thread handle 139830446065408, query id 7300 localhost root updateinsert into l values (3,4,14,20)------- TRX HAS BEEN WAITING 19 SEC FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 1358 page no 5 n bits 72 index b of table `test`.`l` trx id 31220594 lock_mode X locks gap before rec insert intention waitingRecord lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000006; asc     ;; 1: len 4; hex 80000004; asc     ;;...4在被鎖住的範圍之內,所以插不進去

那(1,4,14,20)能插入嗎?先公布答案,是可以插入的

為什嗎?
這裡有一個隱晦的問題

二級索引排序的時候包含主索引值排序,鎖的時候範圍也是包含主索引值,如下:

((4,2),(6,4)],((6,4),(8,6))

(4,3)在這個範圍內,所以不能插,而(4,1)不在這範圍內,可以插

insert (5,4,14,20)也會阻塞,(4,5)在範圍中

Ⅱ、走索引查詢的記錄不存在2.1 rr交易隔離等級
([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where b = 12 for update;Empty set (0.00 sec)([email protected]) [test]> show engine innodb status\G...---TRANSACTION 31220600, ACTIVE 7 sec2 lock struct(s), heap size 1136, 1 row lock(s)MySQL thread id 1104, OS thread handle 139830452774656, query id 7383 localhost root startingshow engine innodb statusTABLE LOCK table `test`.`l` trx id 31220600 lock mode IXRECORD LOCKS space id 1358 page no 5 n bits 72 index b of table `test`.`l` trx id 31220600 lock_mode XRecord lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;;...heap no 1    0和1的heap no表示的是min和max的記錄,虛擬n_fields 1   只有一個列,偽列key min 4 6 8 10 max

在max上加了鎖,範圍為(10,正無窮),意味著插入任意大於10的記錄都是不可以的

rr層級時,如果搜尋一條記錄搜不到,就會在max上加鎖,意味著這條沒查到的記錄之後的範圍都插不了

為什麼呢?
如果第一次讀12沒有讀到不把10往後的都鎖住,那一個線程插入了12這條記錄,第二次再去讀就會讀到12,就發生了幻讀

再來
session1:

([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where b = 7 for update;Empty set (0.00 sec)([email protected]) [test]> show engine innodb status\G···---TRANSACTION 31220601, ACTIVE 51 sec2 lock struct(s), heap size 1136, 1 row lock(s)MySQL thread id 1104, OS thread handle 139830452774656, query id 7387 localhost root startingshow engine innodb statusTABLE LOCK table `test`.`l` trx id 31220601 lock mode IXRECORD LOCKS space id 1358 page no 5 n bits 72 index b of table `test`.`l` trx id 31220601 lock_mode X locks gap before recRecord lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000008; asc     ;; 1: len 4; hex 80000006; asc     ;;···在8上面加了一個gap鎖,8本身是不鎖的

session2:

([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where b = 8 for update;+---+------+------+------+| a | b    | c    | d    |+---+------+------+------+| 6 |    8 |   10 |   12 |+---+------+------+------+1 row in set (0.00 sec)這時候8這條記錄上又有了Next-key Lock鎖,鎖住6到8,8本身也被鎖住,8上面兩把鎖是不抵觸的
2.2 rc交易隔離等級
([email protected]) [test]> show variables like ‘tx_isolation‘;+---------------+----------------+| Variable_name | Value          |+---------------+----------------+| tx_isolation  | READ-COMMITTED |+---------------+----------------+1 row in set (0.00 sec)([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where b = 12 for update;Empty set (0.00 sec)([email protected]) [test]> show engine innodb status\G...------------TRANSACTIONS------------Trx id counter 31220604Purge done for trx‘s n:o < 31220593 undo n:o < 0 state: running but idleHistory list length 35LIST OF TRANSACTIONS FOR EACH SESSION:---TRANSACTION 421305875783280, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 421305875781456, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 31220603, ACTIVE 6 sec1 lock struct(s), heap size 1136, 0 row lock(s)MySQL thread id 1106, OS thread handle 139830446065408, query id 7436 localhost root startingshow engine innodb statusTABLE LOCK table `test`.`l` trx id 31220603 lock mode IX...

會發現並沒有鎖,讀到沒有12這條記錄,直接就釋放了,rc不解決幻讀,這麼看rc的insert並發效能會更好

Ⅲ、不走索引的情況怎麼鎖?3.1 rr交易隔離等級
([email protected]) [test]> show variables like ‘tx_isolation‘;+---------------+----------------+| Variable_name | Value          |+---------------+----------------+| tx_isolation  | READ-COMMITTED |+---------------+----------------+1 row in set (0.01 sec)([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where d = 10 for update;+---+------+------+------+| a | b    | c    | d    |+---+------+------+------+| 4 |    6 |    8 |   10 |+---+------+------+------+1 row in set (0.00 sec)([email protected]) [test]> show engine innodb status\G...LIST OF TRANSACTIONS FOR EACH SESSION:---TRANSACTION 421305875783280, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 421305875781456, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 31220604, ACTIVE 11 sec2 lock struct(s), heap size 1136, 1 row lock(s)MySQL thread id 1106, OS thread handle 139830446065408, query id 7446 localhost root startingshow engine innodb statusTABLE LOCK table `test`.`l` trx id 31220604 lock mode IXRECORD LOCKS space id 1358 page no 3 n bits 72 index PRIMARY of table `test`.`l` trx id 31220604 lock_mode X locks rec but not gapRecord lock, heap no 3 PHYSICAL RECORD: n_fields 6; compact format; info bits 0 0: len 4; hex 80000004; asc     ;; 1: len 6; hex 000001c1b93a; asc      :;; 2: len 7; hex e1000001a90110; asc        ;; 3: len 4; hex 80000006; asc     ;; 4: len 4; hex 80000008; asc     ;; 5: len 4; hex 8000000a; asc     ;;...

會產生一個記錄鎖,對d=10這條記錄對應的主鍵加鎖

3.2 rr交易隔離等級
([email protected]) [test]> show variables like ‘tx_isolation‘;+---------------+-----------------+| Variable_name | Value           |+---------------+-----------------+| tx_isolation  | REPEATABLE-READ |+---------------+-----------------+1 row in set (0.00 sec)([email protected]) [test]> begin;Query OK, 0 rows affected (0.00 sec)([email protected]) [test]> select * from l where d = 10 for update;+---+------+------+------+| a | b    | c    | d    |+---+------+------+------+| 4 |    6 |    8 |   10 |+---+------+------+------+1 row in set (0.00 sec)([email protected]) [test]> show engine innodb status\G...LIST OF TRANSACTIONS FOR EACH SESSION:---TRANSACTION 421305875783280, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 421305875781456, not started0 lock struct(s), heap size 1136, 0 row lock(s)---TRANSACTION 31220606, ACTIVE 22 sec2 lock struct(s), heap size 1136, 5 row lock(s)MySQL thread id 1106, OS thread handle 139830446065408, query id 7459 localhost root startingshow engine innodb statusTABLE LOCK table `test`.`l` trx id 31220606 lock mode IXRECORD LOCKS space id 1358 page no 3 n bits 72 index PRIMARY of table `test`.`l` trx id 31220606 lock_mode XRecord lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;;Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0 0: len 4; hex 80000002; asc     ;; 1: len 6; hex 000001c1b939; asc      9;; 2: len 7; hex e0000001a80110; asc        ;; 3: len 4; hex 80000004; asc     ;; 4: len 4; hex 80000006; asc     ;; 5: len 4; hex 80000008; asc     ;;Record lock, heap no 3 PHYSICAL RECORD: n_fields 6; compact format; info bits 0 0: len 4; hex 80000004; asc     ;; 1: len 6; hex 000001c1b93a; asc      :;; 2: len 7; hex e1000001a90110; asc        ;; 3: len 4; hex 80000006; asc     ;; 4: len 4; hex 80000008; asc     ;; 5: len 4; hex 8000000a; asc     ;;Record lock, heap no 4 PHYSICAL RECORD: n_fields 6; compact format; info bits 0 0: len 4; hex 80000006; asc     ;; 1: len 6; hex 000001c1b93f; asc      ?;; 2: len 7; hex e40000015d0110; asc     ]  ;; 3: len 4; hex 80000008; asc     ;; 4: len 4; hex 8000000a; asc     ;; 5: len 4; hex 8000000c; asc     ;;Record lock, heap no 5 PHYSICAL RECORD: n_fields 6; compact format; info bits 0 0: len 4; hex 80000008; asc     ;; 1: len 6; hex 000001c1b940; asc      @;; 2: len 7; hex e50000015f0110; asc     _  ;; 3: len 4; hex 8000000a; asc     ;; 4: len 4; hex 8000000c; asc     ;; 5: len 4; hex 8000000e; asc     ;;...

Next-key Lock鎖住了主鍵的2,4,6,8

(負無窮,2],(2,4],(4,6],(6,8],(8,正無窮)

這並不是表鎖(沒有表升級),只是表現形式類似整個鎖住,如果表有100w記錄,會產生100w個lock,鎖模式是Next-key Locking,任何記錄的插入和更新都是不可以的,鎖的代價很大

因為後面插入任何一條記錄都可以是d=10,所以任何一條記錄都要被鎖住,隨便插一個d=10的記錄都給插那就亂套了,又幻讀了,那就不允許任何記錄插入吧

tips:
rr本身不解決幻讀,只有在序列化的交易隔離等級之下才解決幻讀,但是MySQL的鎖比較特殊,在rr隔離等級下就解決幻讀

InnoDB中鎖的演算法(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.