当前位置:网站首页>MySQL row locks and gap locks
MySQL row locks and gap locks
2022-08-01 05:48:00 【loner's carnival】
Whether it is update or select for update, as long as the field in the where condition does not have an index, the entire table will be locked.If an index exists on the updated row, the updated record is locked when updating.
1, row lock
Test:
Item table, lock on the id and price fields.
Opens two windows.Turn off autocommit separately: set autocommit=0;
1.1, the field has an index, row lock
a window execution:
update item2 set stat = 1 where price = 1500;
bWindow execution:
update item2 set stat = 1 where price = 30;
Since there is an index in the price field, the update statement of the a windowOnly one record with price=1500 is locked.The b window normally updates the record with price=30.
1.2, there is no index in the field, table lock
a window execution:
update item2 set stat = 1 where name= 'dd';
bWindow execution:
update item2 set stat = 1 where price = 'ee';
Since the name field has no index, the a window is limited wherename='dd', before the commit, the query of the record with name='ee' in the b window is still blocked.So if the field does not have an index, a table lock will be performed on update.
1.3, the case of joint index
Create a new joint index on the three fields of name, address, and port
create index index_name_address_port on item2(name,address,port);
1.3.1, when the joint index is valid
a window execution:
update item2 set stat=1 where name = 'cc4' and address='Zhangjia Village';
bWindow execution:
update item2 set stat=1 where name = 'ff' and address='bcbc';
As long as the where query index is valid, the joint index will still onlyLocking a row does not affect updates of other rows.
1.3.2, the joint index does not take effect
a window execution:
update item2 set stat=1 where address='Zhangjia Village' and port='67';
bWindow execution:
update item2 set stat=1 where address='bcbc' and port='24';
If the joint index in the where condition is invalid, it will still beLock the entire table.
Joint indexing conditions
Mysql uses the fields in the index from left to right. A query can only use a part of the index, but only the leftmost part.For example the index is key index (a,b,c).Can support a | a, b | a, b, c 3 combinations for search, but do not support b, c for search. When the leftmost field is a constant reference, the index is very effective.
2, gap lock
A special case of row locks: gap locks: value in range, but not present
a window execution:
update item2 set stat = 1 where price>100 and price<400;
bWindow execution
INSERT INTO `item2` VALUES (12, 'gt', 'vxc', 234, 288, NULL, NULL);
c window execution:
update item2 set stat = 1 where price = 300;
After a window is executed, b and c windows are blocked.The records indicating (100, 400) are locked.Unable to insert records with price before 100-400, even if the record with price=288 does not exist yet, magic degree can be avoided.There is also no way to update records that already existed before 100-400.
边栏推荐
- 第6章——数据库的安全性
- 微信小程序获取手机号phonenumber.getPhoneNumber接口开发
- Matlab simulink particle swarm optimization fuzzy pid control motor pump
- WebSocket implements chat function
- 微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
- Seleniu: Common operations on elements
- 仿牛客网讨论社区项目—项目总结及项目常见面试题
- 【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
- vim configuration + ctag is as easy to read code as source insight
- torch
猜你喜欢
随机推荐
Selenium: upload and download files
实战演练 Navicat 中英文模式切换
【FiddlerScript】利用FiddlerScript抓包保利威下载
微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
2022.7.26 Mock Competition
uva10825
测试工具(四)Jenkins环境搭建与使用
NDK does not contain any platforms problem solving
Selenium: element positioning
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
类神经网络训练不起来怎么办
用控件当画笔获得bitmap代码记录
第5章——以程序方式处理MySQL数据表的数据
图片更新之后Glide加载依旧是原来的图片问题
七、MFC序列化机制和序列化类对象
关于给Qt做一个软件初始化的进度条
Selenium:操作JS
湖仓一体电商项目(一):项目背景和架构介绍
uva12326
uva12326