当前位置:网站首页>Deadlock caused by non clustered index in SQL Server
Deadlock caused by non clustered index in SQL Server
2022-07-07 10:05:00 【knight_ hf】
Recently, a message sending function encountered a deadlock problem caused by non clustered indexes during the test , The following is what my partner summarized afterwards , Now share it .
( Because some content involves company information , So I only shared some of them , But it should not affect the overall reading )
Situation introduction
Table name is Receiver, Field has id( Primary key , Clustered index ),msgId( Nonclustered indexes ),receiver( Nonclustered indexes ),createTime,updateTime
The statements executed concurrently are
1、insert into Receiver select msgId,receiver,createTime,updateTime from tempTable 2、update Receiver set updateTime=#{} where msgId=#{} and receiver in (#{})Occurrence scenario
1、 Multithreading (100 individual ) perform Receiver Insert statement of
2、 Multithreading (8 Threads ) perform Receiver UPDATE statement of
Lock analysis
1、insert Statement execution time , First apply for the line where the record is inserted ( Or page 、 surface ) Of X lock ( Exclusive lock ), After record insertion , Applying for non clustered indexes X lock , Finish updating the index , When it's all done , Release what is held X lock
2、update Statement execution time , apply msgId In a nonclustered index U lock , adopt msgId The non clustered index gets the location of the record that needs to be updated , Then apply for the clustered index of the corresponding record U lock ( Update lock ), Because it does not involve the update of nonclustered indexes , So after the data update , all U The lock is released
Deadlock analysis
Let's first assume insert priority ,insert Successfully apply to the row with inserted data X lock , Because multiple records are inserted at one time , So the actual application is to aggregate index pages pagelock. After record insertion ,insert The process will apply msgId、updateTime Nonclustered index X lock . Suppose this time insert The process is suspended , The right of execution is given to update process .update Priority application msgId Nonclustered index U Lock to query the set of records that need to be updated , After finding the corresponding record , I will apply for these records U lock ( The same is pagelock). But because this data page has been insert The process adds X lock , So we can't add U lock , therefore update The process enters a waiting state . meanwhile ,insert The process also needs to apply for indexing X lock , But it has been update The process holds U lock . Last , The two processes enter the state of waiting for each other , A deadlock occurs .
What happened
1、 In the case of high concurrency update And insert
2、update Of where The non clustered index of the table is used in the condition
terms of settlement
modify update sentence
The original statement is
update Receiver set updateTime=#{
} where msgId=#{
} and receiver in (#{
})1、 Add row level lock
update Receiver with(rowlock) set updateTime=#{
} where msgId=#{
} and receiver in (#{
}) After changing to row level lock , Can significantly reduce insert Requested lock and update The requested lock coincides
Inferiority : When updating multiple records, multiple row level locks will be held , Previously, it was possible to hold only one page lock
2、where Add with(nolock)
update Receiver set updateTime=#{} where id in (select r.id from Receiver r with(nolock) where r.msgId=#{} and r.receiver in (#{}))边栏推荐
- 2020 Zhejiang Provincial Games
- Bean operation domain and life cycle
- PostgreSQL reports an error when creating a trigger,
- The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
- 根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
- Garbage disposal method based on the separation of smart city and storage and living digital home mode
- 20排位赛3
- 【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
- 视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
- ORM模型--关联字段,抽象模型类
猜你喜欢

Applet popup half angle mask layer

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

First issue of JS reverse tutorial

一大波开源小抄来袭

小程序滑动、点击切换简洁UI

Performance optimization record of the company's product "yunzhujia"
![[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development](/img/21/6a183e4e10daed90c66235bdbdc3bf.png)
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development

Web3.0 series distributed storage IPFs

反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释

AI moves from perception to intelligent cognition
随机推荐
The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
2016 CCPC Hangzhou Onsite
Software modeling and analysis
2020 Zhejiang Provincial Games
根据热门面试题分析Android事件分发机制(一)
ORM模型--关联字段,抽象模型类
Use 3 in data modeling σ Eliminate outliers for data cleaning
Gym - 102219j kitchen plates (violent or topological sequence)
CDZSC_2022寒假个人训练赛21级(2)
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
高斯消元
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
Database multi table Association query problem
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
Flex flexible layout
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Luogu p2482 [sdoi2010] zhuguosha
How will fashion brands enter the meta universe?
【无标题】