溫馨提示×

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

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

怎么在MySQL中實(shí)現(xiàn)行級(jí)鎖定

發(fā)布時(shí)間:2021-05-07 16:42:50 來(lái)源:億速云 閱讀:403 作者:Leah 欄目:MySQL數(shù)據(jù)庫(kù)

怎么在MySQL中實(shí)現(xiàn)行級(jí)鎖定?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

前言

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

行級(jí)鎖

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

特點(diǎn):

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

實(shí)現(xiàn)原理:

InnoDB行鎖是通過(guò)給索引項(xiàng)加鎖來(lái)實(shí)現(xiàn)的,這一點(diǎn)mysql和oracle不同,后者是通過(guò)在數(shù)據(jù)庫(kù)中對(duì)相應(yīng)的數(shù)據(jù)行加鎖來(lái)實(shí)現(xiàn)的,InnoDB這種行級(jí)鎖決定,只有通過(guò)索引條件來(lái)檢索數(shù)據(jù),才能使用行級(jí)鎖,否則,直接使用表級(jí)鎖。特別注意:使用行級(jí)鎖一定要使用索引

舉個(gè)栗子:

創(chuàng)建表結(jié)構(gòu)

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)通過(guò)主鍵索引來(lái)查詢數(shù)據(jù)庫(kù)使用行鎖

打開(kāi)三個(gè)命令行窗口進(jìn)行測(cè)試

命令行窗口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)查詢非索引的字段來(lái)查詢數(shù)據(jù)庫(kù)使用行鎖

打開(kāi)兩個(gè)命令行窗口進(jìn)行測(cè)試

命令行窗口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)查詢非唯一索引字段來(lái)查詢數(shù)據(jù)庫(kù)使用行鎖鎖住多行

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

打開(kāi)三個(gè)命令行窗口進(jìn)行測(cè)試

命令行窗口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

##### (4)條件中使用索引來(lái)操作檢索數(shù)據(jù)庫(kù)時(shí),是否使用索引還需有mysql通過(guò)判斷不同執(zhí)行計(jì)劃來(lái)決定,是否使用該索引,如需判定如何使用explain來(lái)判斷索引,請(qǐng)聽(tīng)下回分解

 

關(guān)于怎么在MySQL中實(shí)現(xiàn)行級(jí)鎖定問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

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

AI