当前位置:网站首页>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.
边栏推荐
猜你喜欢

Malicious attacks on mobile applications surge by 500%

Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers

WPF入门项目必知必会-初步了解数据绑定 binding

类神经网络训练不起来怎么办

What should I do if the neural network cannot be trained?

LeetCode 0149. Maximum number of points on a line

2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)

2022.7.26 模拟赛

基于MATLAB的BP神经网络进行语音特征信号分类

Using FiddlerScript caught poly FiddlerScript 】 【 download
随机推荐
Selenium:鼠标、键盘事件
NDK does not contain any platforms问题解决
【视觉SLAM十四讲】第一章理论详解
2022.7.27 Selected lectures on good topics
WPF项目-按着键盘方向键,移动格子盒子效果
Selenium:操作JS
Flip letters using string container
Selenium:弹窗处理
Qt Widget project loading example of qml
ORACLE 实现另外一个用户修改包(package)
Check控件
LeetCode 0150. 逆波兰表达式求值
The BP neural network based on MATLAB voice characteristic signal classification
mysql的行锁和间隙锁
Selenium: Element wait
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
LeetCode 0150. Reverse Polish Expression Evaluation
Robot_Framework:常用内置关键字
uva10825
uva10825