当前位置:网站首页>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.
边栏推荐
- Windows taskbar icon abnormal solution
- WPF入门项目必知必会-初步了解数据绑定 binding
- Robot_Framework:常用内置关键字
- About making a progress bar for software initialization for Qt
- 微信小程序用户登录auth.code2Session接口开发
- matplotlib pyplot
- uva12326
- Selenium:表单切换
- Using FiddlerScript caught poly FiddlerScript 】 【 download
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
猜你喜欢
随机推荐
Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
vsce package 后出现 Command failed: npm list --production --parseable --depth=99999 --loglevel=error异常
深度比较两个对象是否相同
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Dell PowerEdge Server R450 RAID Configuration Steps
AspNet.WebApi.Owin 自定义Token请求参数
解决浏览器滚动条导致的页面闪烁问题
LeetCode 0150. 逆波兰表达式求值
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
使用string 容器翻转 字母
实战演练 Navicat 中英文模式切换
从离线到实时对客,湖仓一体释放全量数据价值
AspNet.WebApi.Owin custom Token request parameters
Selenium:元素定位
What should I do if the neural network cannot be trained?
Leetcode第 304 场周赛
企业员工人事管理系统(数据库课设)
LeetCode 0149. 直线上最多的点数
Malicious attacks on mobile applications surge by 500%
Selenium:元素判断