MySQL for update鎖表還是鎖行校驗(yàn)(過(guò)程詳解)
select * from user where id = 1 for update ;
1. for update作用
在MySQL中,使用for update子句可以對(duì)查詢(xún)結(jié)果集進(jìn)行行級(jí)鎖定,以便在事務(wù)中對(duì)這些行進(jìn)行更新或者防止其他事務(wù)對(duì)這些行進(jìn)行修改。
當(dāng)使用for update時(shí),鎖定行的方式取決于where中的字段是否具有索引,而不是唯一索引。如果where
條件中的字段具有索引(無(wú)論是普通索引還是唯一索引),MySQL將鎖定與查詢(xún)結(jié)果集匹配的行,如果where中的字段沒(méi)有索引MySQL將鎖表
2. 擼代碼證明結(jié)果
創(chuàng)建一張表
其中id是主鍵,user_name建立索引
create table if not exists user ( id int auto_increment comment '主鍵ID' primary key, user_name varchar(32) null comment '用戶名', password varchar(64) null comment '密碼', phone varchar(11) null comment '手機(jī)號(hào)' ); create index user_user_name_index on test1.user (user_name);
插入三條數(shù)據(jù)
INSERT INTO user (id, user_name, password, phone) VALUES (1, '小明', '123', '13123920201'); INSERT INTO user (id, user_name, password, phone) VALUES (2, '小王', '1234', '13123920202'); INSERT INTO user (id, user_name, password, phone) VALUES (3, '小狗', '12345', '13123920203');
begin:表示開(kāi)啟一個(gè)事物
commit:表示提交事物
新開(kāi)3個(gè)查詢(xún)窗口
窗口1
begin; select * from user where id = 1 for update ; commit;
窗口2
begin; select * from user where id = 1 for update ; commit;
窗口3
begin; select * from user where id = 2 for update ; commit;
a. 查詢(xún)帶普唯一引字段
步驟1: 窗口1執(zhí)行下面代碼
begin; select * from user where id = 1 for update ;
結(jié)果如下
這時(shí)id = 1的行應(yīng)該被鎖住了
步驟2: 窗口2執(zhí)行下面代碼
begin; select * from user where id = 1 for update ;
結(jié)果如下,一直在等待
最后報(bào)鎖超時(shí)錯(cuò)誤
證明id = 1的行被鎖住了
步驟3:
查詢(xún)id=2的數(shù)據(jù)
begin; select * from user where id = 2 for update ;
可以查到結(jié)果
說(shuō)明select * from user where id = 1 for update ;只鎖了id = 1的行,id = 2行沒(méi)有被鎖
步驟4:
窗口1執(zhí)行下面代碼
commit;
結(jié)果如下
窗口2再執(zhí)行下面代碼
begin; select * from user where id = 1 for update ;
這時(shí)就可以查出結(jié)果來(lái)了
說(shuō)明id = 1行鎖被釋放,窗口2就可以查到結(jié)果了
證明唯一索引鎖行
b. 查詢(xún)帶普通索引字段
窗口1
begin; select * from user where user_name = '小明' for update ;
窗口2
begin; select * from user where user_name = '小王' for update ;
證明普通索引時(shí)鎖行
c. 查詢(xún)沒(méi)索引字段
窗口1:
begin; select * from user where phone = 13123920201 for update ;
窗口2:
begin; select * from user where id = 2 for update ;
一直再查詢(xún)
鎖等待超時(shí)
證明沒(méi)索引時(shí)鎖表
到此這篇關(guān)于MySQL for update鎖表還是鎖行校驗(yàn)的文章就介紹到這了,更多相關(guān)MySQL for update鎖表還是鎖行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL中select...for update鎖表
- mysql中的limit 1 for update的鎖類(lèi)型
- mysql行鎖(for update)解決高并發(fā)問(wèn)題
- Mysql中的select ...for update
- Mysql查詢(xún)時(shí)如何使用for update行鎖還是表鎖
- Mysql?for?update導(dǎo)致大量行鎖的問(wèn)題
- 解讀mysql的for update用法
- MySQL SELECT?...for?update的具體使用
- mysql事務(wù)select for update及數(shù)據(jù)的一致性處理講解
- MySQL中FOR UPDATE的具體用法
相關(guān)文章
MySQL入門(mén)完全指南及Linux系統(tǒng)下基本的安裝教程
這篇文章主要介紹了MySQL入門(mén)完全指南及Linux系統(tǒng)下基本的安裝教程,對(duì)MySQL的基本特性有一個(gè)較為詳細(xì)的整理,需要的朋友可以參考下2015-11-11SQL NULL值的定義測(cè)試處理空數(shù)據(jù)及SQL?UPDATE語(yǔ)句使用詳解
這篇文章主要為大家介紹了SQL NULL值的定義測(cè)試處理空數(shù)據(jù)及SQL?UPDATE語(yǔ)句使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11MySQL 8.0的關(guān)系數(shù)據(jù)庫(kù)新特性詳解
廣受歡迎的開(kāi)源數(shù)據(jù)庫(kù)MySQL 8中,包括了眾多新特性,下面這篇文章主要給大家介紹了關(guān)于MySQL 8.0的關(guān)系數(shù)據(jù)庫(kù)新特性的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-03-03linux安裝兩個(gè)mysql(8.0和5.7),并同時(shí)使用方式
這篇文章主要介紹了如何在CentOS?7上下載和安裝MySQL?8.0和MySQL?5.7.30,并詳細(xì)描述了安裝步驟,包括解壓、配置、初始化和啟動(dòng)等過(guò)程2024-12-12搞定mysql行轉(zhuǎn)列的7種方法以及列轉(zhuǎn)行
在MySQL數(shù)據(jù)庫(kù)中,有時(shí)候我們需要將一列數(shù)據(jù)轉(zhuǎn)化為行數(shù)據(jù),以便更好地進(jìn)行數(shù)據(jù)分析和處理,下面這篇文章主要給大家介紹了關(guān)于搞定mysql行轉(zhuǎn)列的7種方法以及列轉(zhuǎn)行的相關(guān)資料,需要的朋友可以參考下2024-03-03MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法,使用 binlog 恢復(fù)數(shù)據(jù)的預(yù)期效果是將誤刪的數(shù)據(jù)還原到誤刪之前的狀態(tài),以減少或消除數(shù)據(jù)丟失的影響,文中有相關(guān)的代碼示例和圖文介紹,需要的朋友可以參考下2024-05-05mysql-connector-java與Mysql、Java的對(duì)應(yīng)版本問(wèn)題
這篇文章主要介紹了mysql-connector-java與Mysql、Java的對(duì)應(yīng)版本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11