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