有人可以告诉我为什么以下SQL Server查询是死锁的,修复它的解决方案是什么?
<deadlock-list>
<deadlock victim="process88b5b8">
<process-list>
<process id="process88b5b8" taskpriority="0" logused="76132" waitresource="RID: 32:1:151867:174" waittime="5093" ownerId="65554098" transactionguid="0xedf3314c05f1124cbe8d480cd092e03e" transactionname="DTCXact" lasttranstarted="2011-09-02T19:00:29.690" XDES="0x1029e040" lockMode="S" schedulerid="1" kpid="5108" status="suspended" spid="118" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2011-09-02T19:00:31.317" lastbatchcompleted="2011-09-02T19:00:31.300" hostname="MELWFPL382S" hostpid="0" loginname="MM4" isolationlevel="repeatable read (3)" xactid="65554098" currentdb="32" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x020000004b4b0a0d63e1040095143cbaa0174ffc3e076067"> delete from PARTIES where PARTYEXTERNALREF=@P0 and ISCOUNTERPARTY='N' and PARTYID in (select PARTYID from NAB_PARTY_EXTEND (nolock) where PARTYTYPE=@P1) </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">unknown </frame>
</executionStack>
<inputbuf>(@P0 nvarchar(4000),@P1 nvarchar(4000))delete from PARTIES where PARTYEXTERNALREF=@P0 and ISCOUNTERPARTY='N' and PARTYID in (select PARTYID from NAB_PARTY_EXTEND (nolock) where PARTYTYPE=@P1) </inputbuf>
</process>
<process id="process9196a8" taskpriority="0" logused="132612" waitresource="RID: 32:1:140302:31" waittime="5046" ownerId="65554657" transactionguid="0x7313c78fecc8914dac3ed821cd7c21fe" transactionname="DTCXact" lasttranstarted="2011-09-02T19:00:34.100" XDES="0x12835778" lockMode="S" schedulerid="2" kpid="3692" status="suspended" spid="94" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2011-09-02T19:00:35.690" lastbatchcompleted="2011-09-02T19:00:35.687" hostname="MELWFPL382S" hostpid="0" loginname="MM4" isolationlevel="repeatable read (3)" xactid="65554657" currentdb="32" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x020000004b4b0a0d63e1040095143cbaa0174ffc3e076067">delete from PARTIES where PARTYEXTERNALREF=@P0 and ISCOUNTERPARTY='N' and PARTYID in (select PARTYID from NAB_PARTY_EXTEND (nolock) where PARTYTYPE=@P1) </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">unknown </frame>
</executionStack>
<inputbuf>(@P0 nvarchar(4000),@P1 nvarchar(4000))delete from PARTIES where PARTYEXTERNALREF=@P0 and ISCOUNTERPARTY='N' and PARTYID in (select PARTYID from NAB_PARTY_EXTEND (nolock) where PARTYTYPE=@P1) </inputbuf>
</process>
</process-list>
<resource-list>
<ridlock fileid="1" pageid="140302" dbid="32" objectname="mm4_melwfpl382s.dbo.COUNTERPARTYSSI" id="lock170fa500" mode="X" associatedObjectId="72057595803336704">
<owner-list>
<owner id="process88b5b8" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process9196a8" mode="S" requestType="wait" />
</waiter-list>
</ridlock>
<ridlock fileid="1" pageid="151867" dbid="32" objectname="mm4_melwfpl382s.dbo.COUNTERPARTYSSI" id="lock20e65d80" mode="X" associatedObjectId="72057595803336704">
<owner-list>
<owner id="process9196a8" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process88b5b8" mode="S" requestType="wait" />
</waiter-list>
</ridlock>
</resource-list>
</deadlock>
</deadlock-list>
我不明白的是两个进程如何对同一个对象进行独占锁定。
PARTIES表上有一个索引(IDX_NC_PARTIES_PARTYEXTERNALREF_ISCOUNTERPARTY_PARTYID),数据库设置为读取已提交的快照。
谢谢,
韦恩。
答案 0 :(得分:9)
isolationlevel="repeatable read (3)"
因此死锁发生在表的基本堆上(RID锁而不是密钥锁意味着堆不是Btree)。高隔离级别(可能由DTC引起,从xact名称判断)使RCSI设置无关紧要。
PARTYEXTERNALREF和PARTYTYPE列是什么类型的?传入的参数是NVARCHAR(即Unicode),如果列是VARCHAR(即Ascii),那么由于data type precedence的规则,将不使用NC索引。由于涉及表扫描,以及使用中的高隔离级别,因此死锁几乎是不可避免的。
解决方案是对@ P0和@ P1使用VARCHAR类型参数,以便利用NC索引来避免表扫描。
如果参数已经是VARCHAR类型,并且您可以从执行计划中确认使用NC上的搜索,那么我的第一个问题是 else 是什么事务正在做什么,除了删除语句?
顺便说一下,您只提供NC索引的名称,但我假设它在(PARTYEXTERNALREF, ISCOUNTERPARTY, PARTYID)
上。
更新
由于你的评论说列是 NVARCHAR,因此表扫描假设可能是错误的。还有三种可能导致需要调查的死锁:
对于前两个假设,您现在可以做任何事情(调查它们是否正确)。对于最后一个,我可以告诉你如何验证它,但不是微不足道的。它不太可能发生并且有点难以证明,但它是可能的。既然您知道死锁情况(附加的XML),请将其用作调查基础:
DBCC TRACEON(3604,-1)
DBCC PAGE (<restored db id>, 1, 151867, 3)
检查插槽174中的值SELECT %%lockres%% FROM PARTIES WHERE PARTYEXTERNALREF = ... AND ISCOUNTERPARTY='N' and PARTYID=...
并传入上面列出的值