溫馨提示×

溫馨提示×

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

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

mysql行級鎖的工作原理

發(fā)布時間:2020-10-27 09:48:55 來源:億速云 閱讀:166 作者:小新 欄目:MySQL數(shù)據(jù)庫

小編給大家分享一下mysql行級鎖的工作原理,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

mysql行級鎖實現(xiàn)原理:1、InnoDB行鎖是通過給索引項加鎖來實現(xiàn)的,這一點mysql和 oracle不同;2、InnoDB這種行級鎖決定,只有通過索引條件來檢索數(shù)據(jù),才能使用行級鎖,否則, 直接使用表級鎖。

mysql行級鎖實現(xiàn)原理:

鎖是在執(zhí)行多線程時用于強行限定資源訪問的同步機制,數(shù)據(jù)庫鎖根據(jù)鎖的粒度可分為行級鎖, 表級鎖和頁級鎖

行級鎖

行級鎖是mysql中粒度最細的一種鎖機制,表示只對當前所操作的行進行加鎖,行級鎖發(fā)生沖突 的概率很低,其粒度最小,但是加鎖的代價最大。行級鎖分為共享鎖和排他鎖。

特點:

開銷大,加鎖慢,會出現(xiàn)死鎖;鎖定粒度最小,發(fā)生鎖沖突的概率最大,并發(fā)性也高;

實現(xiàn)原理:

InnoDB行鎖是通過給索引項加鎖來實現(xiàn)的,這一點mysql和oracle不同,后者是通過在數(shù)據(jù)庫中 對相應的數(shù)據(jù)行加鎖來實現(xiàn)的,InnoDB這種行級鎖決定,只有通過索引條件來檢索數(shù)據(jù),才能使用行 級鎖,否則,直接使用表級鎖。

特別注意: 使用行級鎖一定要使用索引

舉個栗子:

創(chuàng)建表結構

CREATE TABLE `developerinfo` (
  `userID` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `passWord` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`userID`),
  KEY `PASSWORD_INDEX` (`passWord`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入數(shù)據(jù)

INSERT INTO `developerinfo` VALUES ('1', 'liujie', 

'123456');
INSERT INTO `developerinfo` VALUES ('2', 'yitong', '123');
INSERT INTO `developerinfo` VALUES ('3', 'tong', 

'123456');

(1)通過主鍵索引來查詢數(shù)據(jù)庫使用行鎖

打開三個命令行窗口進行測試

命令行窗口1命令行窗口2命令行窗口3
mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '1' for update;
+-------- +--------+----------+
| userID | name   | passWord |
+--------+-------- +----------+
|      1 | liujie | 123456   |
+-------- +--------+----------+
1 row in set
mysql> set autocommit = 0;
Query OK, 0 rows affected

mysql> select * from developerinfo where userid = '1' for update;

等待

mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '3' for update;
+--------+------+----------+
| userID | name | passWord |
+-------- +------+----------+
|      3 | tong | 123456   |
+-------- +------+----------+
1 row in set
mysql> commit;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '1' for update;
+--------+-------- +----------+
| userID | name   | passWord |
+--------+-------- +----------+
|      1 | liujie | 123456   |
+-------- +--------+----------+
1 row in set


(2)查詢非索引的字段來查詢數(shù)據(jù)庫使用行鎖

打開兩個命令行窗口進行測試

命令行窗口1命令行窗口2
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'liujie' for update;
+-------- +--------+----------+
| userID | name   | passWord |
+--------+-------- +----------+
|      1 | liujie | 123456   |
+-------- +--------+----------+
1 row in set
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'tong' for update;

等待

mysql> commit;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'liujie' for update;
+--------+--------+----------+
| userID | name   | passWord |
+--------+--------+----------+
|      1 | liujie | 123456   |
+--------+--------+----------+
1 row in set

(3)查詢非唯一索引字段來查詢數(shù)據(jù)庫使用行鎖鎖住多行

mysql的行鎖是針對索引假的鎖,不是針對記錄,所以可能會出現(xiàn)鎖住不同記錄的場景

打開三個命令行窗口進行測試

命令行窗口1命令行窗口2命令行窗口3
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where password = '123456
' for update;
+--------+--------+----------+
| userID | name   | passWord |
+-------- +--------+----------+
|      1 | liujie | 123456   |
|     3 | tong   | 123456   |
+--------+--------+---------- +
2 rows in set
mysql> set autocommit =0 ;
Query OK, 0 rows affected

mysql> select * from developerinfo where userid = '1' for update;

等待

mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '2
' for update;
+--------+--------+----------+
| userID | name   | passWord |
+--------+--------+----------+
|      2 | yitong | 123     |
+--------+--------+----------+
1 row in set
commit;mysql> select * from developerinfo where userid = '1' for update;
+--------+--------+----------+
| userID | name   | passWord |
+--------+--------+----------+
|      1 | liujie | 123456   |
+--------+--------+----------+
1 row in set


以上是mysql行級鎖的工作原理的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

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

AI