当前位置:网站首页>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.
边栏推荐
猜你喜欢
2022年湖南工学院ACM集训第六次周测题解
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
Motion analysis and parameter optimization of crank-slider mechanism
Check控件
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
2022.7.26 模拟赛
Flip letters using string container
Hunan institute of technology in 2022 ACM training sixth week antithesis
Dell PowerEdge Server R450 RAID Configuration Steps
Robot_Framework: Assertion
随机推荐
Challenge 52 days to memorize Peppa Pig (Day 01)
混合型界面:对话式UI的未来
权重等比分配
Compare two objects are the same depth
NUMPY
Speed up your programs with bitwise operations
用位运算为你的程序加速
小白的0基础教程SQL: 安装MYSQL 03
Seleniu: Common operations on elements
Selenium:元素等待
Selenium:表单切换
NUMPY
WebSocket实现聊天功能
[Translation] Securing cloud-native communications: From ingress to service mesh and beyond
crypto-js使用
Dell PowerEdge Server R450 RAID Configuration Steps
Robot_Framework: keyword
图片更新之后Glide加载依旧是原来的图片问题
About making a progress bar for software initialization for Qt
NDK does not contain any platforms problem solving