共計 1167 個字符,預(yù)計需要花費 3 分鐘才能閱讀完成。
本篇內(nèi)容主要講解“怎么理解 mysql innodb 的行鎖方式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學(xué)習(xí)“怎么理解 mysql innodb 的行鎖方式”吧!
真正的方式是:基于索引找到對應(yīng)的行(不是使用所有的 where 條件找到對應(yīng)的行 ), 然后基于行數(shù)據(jù)加鎖。
root@sakila 10:27:15 show indexes from tab_no_index \G;
*************************** 1. row ***************************
Table: tab_no_index
Non_unique: 1
Key_name: id
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: tab_no_index
Non_unique: 1
Key_name: name
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec)
會話 1:
root@sakila 10:26:17 select * from tab_no_index where id=1 for update;
+——+——+
| id | name |
+——+——+
| 1 | 1 |
| 1 | 4 |
+——+——+
2 rows in set (0.00 sec)
會話 2:通過 name index 找到 id=1 and name=4 的這條記錄,想加鎖,但是不成功,因為該條記錄已經(jīng)被會話 1 加鎖了。
root@sakila 10:27:04 select * from tab_no_index where name= 4 for update;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
到此,相信大家對“怎么理解 mysql innodb 的行鎖方式”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!