日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

mysql行级锁实现原理是什么(五分钟了解Mysql的行级锁)

發布時間:2023/12/15 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 mysql行级锁实现原理是什么(五分钟了解Mysql的行级锁) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mysql行級鎖實現原理:

鎖是在執行多線程時用于強行限定資源訪問的同步機制,數據庫鎖根據鎖的粒度可分為行級鎖,

表級鎖和頁級鎖

行級鎖

行級鎖是mysql中粒度最細的一種鎖機制,表示只對當前所操作的行進行加鎖,行級鎖發生沖突

的概率很低,其粒度最小,但是加鎖的代價最大。行級鎖分為共享鎖和排他鎖。

特點:

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

實現原理:

InnoDB行鎖是通過給索引項加鎖來實現的,這一點mysql和oracle不同,后者是通過在數據庫中

對相應的數據行加鎖來實現的,InnoDB這種行級鎖決定,只有通過索引條件來檢索數據,才能使用行

級鎖,否則,直接使用表級鎖。

特別注意:

使用行級鎖一定要使用索引

舉個栗子:

創建表結構

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;

登錄后復制

插入數據

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

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

'123456');

登錄后復制

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

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

命令行窗口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)查詢非索引的字段來查詢數據庫使用行鎖

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

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

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

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

命令行窗口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)條件中使用索引來操作檢索數據庫時,是否使用索引還需有mysql通過判斷不同執行計劃來

決定,是否使用該索引,如需判定如何使用explain來判斷索引,請聽下回分解

以上就是mysql行級鎖實現原理是什么的詳細內容,更多請關注風君子博客其它相關文章!

總結

以上是生活随笔為你收集整理的mysql行级锁实现原理是什么(五分钟了解Mysql的行级锁)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。