溫馨提示×

溫馨提示×

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

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

InnoDB行鎖的實現方法

發布時間:2021-08-17 10:18:34 來源:億速云 閱讀:184 作者:chen 欄目:MySQL數據庫

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

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在請求其他行的排他鎖時,卻出現了鎖等待!原因就是在沒有索引的情況下,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的行鎖是針對索引加的鎖,不是針對記錄加的鎖,所以雖然是訪問不同行的記錄,但是如果是使用相同的索引鍵,是會出現鎖沖突的。應用設計的時候要注意這一點。


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

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

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

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

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

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


  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行鎖的實現方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女