溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

InnoDB行鎖的實現(xiàn)方法

發(fā)布時間:2021-08-17 10:18:34 來源:億速云 閱讀:145 作者:chen 欄目:MySQL數(shù)據(jù)庫

本篇內(nèi)容主要講解“InnoDB行鎖的實現(xiàn)方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習“InnoDB行鎖的實現(xiàn)方法”吧!

session_1


session_2


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab_no_index where id = 1 ;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab_no_index where id = 2 ;

+------+------+

| id   | name |

+------+------+

| 2    | 2    |

+------+------+

1 row in set (0.00 sec)


mysql> select * from tab_no_index where id = 1 for update;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)




mysql> select * from tab_no_index where id = 2 for update;

等待



在如表20-9所示的例子中,看起來session_1只給一行加了排他鎖,但session_2在請求其他行的排他鎖時,卻出現(xiàn)了鎖等待!原因就是在沒有索引的情況下,InnoDB只能使用表鎖。當我們給其增加一個索引后,InnoDB就只鎖定了符合條件的行,如表20-10所示。

session_1


session_2


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab_with_index where id = 1 ;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab_with_index where id = 2 ;

+------+------+

| id   | name |

+------+------+

| 2    | 2    |

+------+------+

1 row in set (0.00 sec)


mysql> select * from tab_with_index where id = 1 for update;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)




mysql> select * from tab_with_index where id = 2 for update;

+------+------+

| id   | name |

+------+------+

| 2    | 2    |

+------+------+

1 row in set (0.00 sec)



(2)由于MySQL的行鎖是針對索引加的鎖,不是針對記錄加的鎖,所以雖然是訪問不同行的記錄,但是如果是使用相同的索引鍵,是會出現(xiàn)鎖沖突的。應(yīng)用設(shè)計的時候要注意這一點。


在如表20-11所示的例子中,表tab_with_index的id字段有索引,name字段沒有索引:

表20-11                InnoDB存儲引擎使用相同索引鍵的阻塞例子      

在如表20-12所示的例子中,表tab_with_index的id字段有主鍵索引,name字段有普通索引:

表20-12                                  InnoDB存儲引擎的表使用不同索引的阻塞例子

在下面的例子中,檢索值的數(shù)據(jù)類型與索引字段不同,雖然MySQL能夠進行數(shù)據(jù)類型轉(zhuǎn)換,但卻不會使用索引,從而導(dǎo)致InnoDB使用表鎖。通過用explain檢查兩條SQL的執(zhí)行計劃,我們可以清楚地看到了這一點。

<div align="left" font-size:14px;white-space:normal;background-color:#FFFFFF;"> 例子中tab_with_index表的name字段有索引,但是name字段是varchar類型的,如果where條件中不是和varchar類型進行比較,則會對name進行類型轉(zhuǎn)換,而執(zhí)行的全表掃描。


  1. mysql> alter table tab_no_index add index name(name);

  2. Query OK, 4 rows affected (8.06 sec)

  3. Records: 4  Duplicates: 0  Warnings: 0

  4. mysql> explain select * from tab_with_index where name = 1 \G

  5. *************************** 1. row ***************************

  6.            id: 1

  7.   select_type: SIMPLE

  8.         table: tab_with_index

  9.          type: ALL

  10. possible_keys: name

  11.           key: NULL

  12.       key_len: NULL

  13.           ref: NULL

  14.          rows: 4

  15.         Extra: Using where

  16. 1 row in set (0.00 sec)

  17. mysql> explain select * from tab_with_index where name = '1' \G

  18. *************************** 1. row ***************************

  19.            id: 1

  20.   select_type: SIMPLE

  21.         table: tab_with_index

  22.          type: ref

  23. possible_keys: name

  24.           key: name

  25.       key_len: 23

  26.           ref: const

  27.          rows: 1

  28.         Extra: Using where

  29. 1 row in set (0.00 sec)

到此,相信大家對“InnoDB行鎖的實現(xiàn)方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI