久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

InnoDB行鎖的實現方法

149次閱讀
沒有評論

共計 2670 個字符,預計需要花費 7 分鐘才能閱讀完成。

本篇內容主要講解“InnoDB 行鎖的實現方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“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 進行類型轉換,而執行的全表掃描。

mysql alter table tab_no_index add index name(name);

Query OK, 4 rows affected (8.06 sec)

Records: 4 Duplicates: 0 Warnings: 0

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

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

       id: 1

 select_type: SIMPLE

     table: tab_with_index

       type: ALL

possible_keys: name

        key: NULL

     key_len: NULL

        ref: NULL

       rows: 4

     Extra: Using where

1 row in set (0.00 sec)

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

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

       id: 1

 select_type: SIMPLE

     table: tab_with_index

       type: ref

possible_keys: name

        key: name

     key_len: 23

        ref: const

       rows: 1

     Extra: Using where

1 row in set (0.00 sec)

到此,相信大家對“InnoDB 行鎖的實現方法”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-01發表,共計2670字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 巴林右旗| 新乡县| 孟村| 米林县| 乐陵市| 南雄市| 石阡县| 化州市| 确山县| 卢氏县| 汝阳县| 克拉玛依市| 丹江口市| 洮南市| 丰宁| 临朐县| 饶河县| 灵武市| 烟台市| 井陉县| 新兴县| 民和| 林州市| 洱源县| 沾化县| 甘孜| 长葛市| 丰顺县| 大竹县| 肃南| 延边| 磴口县| 青田县| 淮北市| 杭州市| 通辽市| 邢台县| 中阳县| 富川| 德保县| 南靖县|