为什么这个SQL Server查询会死锁?

时间:2011-09-03 06:07:31

标签: sql-server sql-server-2005 deadlock

有人可以告诉我为什么以下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),数据库设置为读取已提交的快照。

谢谢,

韦恩。

1 个答案:

答案 0 :(得分:9)

  • 过程9196a8在X模式下具有页面151867插槽174并且希望在S模式下页面140302插槽31
  • 过程88b5b8在X模式下具有页面140302插槽31并且希望在S模式下页面151867插槽174
  • 两次删除在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,因此表扫描假设可能是错误的。还有三种可能导致需要调查的死锁:

  • DELETE之前由事务运行的任何其他语句(这是最有可能的)
  • 死锁中涉及的两个DELETE语句选择的行中的任何重叠
  • hash collision

对于前两个假设,您现在可以做任何事情(调查它们是否正确)。对于最后一个,我可以告诉你如何验证它,但不是微不足道的。它不太可能发生并且有点难以证明,但它是可能的。既然您知道死锁情况(附加的XML),请将其用作调查基础:

  • 恢复数据库with stop at 2011-09-02T19:00:29.690
  • 的时间点副本
  • 运行DBCC TRACEON(3604,-1)
  • 使用DBCC PAGE (<restored db id>, 1, 151867, 3)检查插槽174中​​的值
  • 使用DBCC PAGE(,1,140302,3)`检查插槽31的值
  • 运行SELECT %%lockres%% FROM PARTIES WHERE PARTYEXTERNALREF = ... AND ISCOUNTERPARTY='N' and PARTYID=...并传入上面列出的值
  • 比较结果锁定哈希值,如果它们匹配则会发生哈希冲突,这会导致死锁。