佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MySQL中的行級鎖定示例詳解

MySQL中的行級鎖定示例詳解

熱門標(biāo)簽:呼叫中心市場需求 百度競價(jià)排名 AI電銷 地方門戶網(wǎng)站 網(wǎng)站排名優(yōu)化 服務(wù)外包 鐵路電話系統(tǒng) Linux服務(wù)器

前言

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

行級鎖

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

特點(diǎn):

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

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

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

舉個栗子:

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

打開三個命令行窗口進(jìn)行測試

命令行窗口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ù)庫使用行鎖

打開兩個命令行窗口進(jìn)行測試

命令行窗口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)鎖住不同記錄的場景

打開三個命令行窗口進(jì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

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。 

您可能感興趣的文章:
  • 關(guān)于MySQL死鎖問題的深入分析
  • MySql 索引、鎖、事務(wù)知識點(diǎn)小結(jié)
  • mysql共享鎖與排他鎖用法實(shí)例分析
  • MySQL全局鎖和表鎖的深入理解
  • MySQL鎖的知識點(diǎn)總結(jié)

標(biāo)簽:湖南 湘潭 銅川 蘭州 黃山 仙桃 崇左 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL中的行級鎖定示例詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    桂东县| 股票| 巴中市| 佛教| 宜良县| 安岳县| 雅安市| 福建省| 天祝| 襄樊市| 万载县| 宽甸| 邵阳市| 石楼县| 宜城市| 日喀则市| 丘北县| 普兰店市| 吴堡县| 湟中县| 东城区| 襄城县| 确山县| 阳朔县| 梧州市| 孟津县| 漳州市| 霍林郭勒市| 潮安县| 克东县| 嘉禾县| 安新县| 云南省| 麻江县| 东台市| 紫阳县| 阿鲁科尔沁旗| 涪陵区| 白玉县| 大兴区| 屏南县|