锁定分区死锁

时间:2013-03-14 10:02:24

标签: sql-server deadlock

我有一张约有350万行的表格。在数据库上启用了锁定分区[1]。该表在白天获得了大量插入,并且我在锁分区上遇到了很多死锁。这些类型的死锁在http://sqlindian.com/2012/07/07/deadlocks-involving-lock-partitions/很好地描述,但是作者说这些类型的死锁非常罕见。在我们的案例中,它们看起来并不罕见!

我可以使用跟踪标志1229禁用锁定分区,但不建议这样做。有没有人对如何避免这些类型的死锁提出一些建议,或者我如何进一步分析这种情况,看看为什么我们会得到这么多"稀有"死锁类型?

[1] http://msdn.microsoft.com/en-us/library/ms187504(v=sql.105).aspx

更新:添加示例死锁图

<deadlock>
  <victim-list>
    <victimProcess id="process5004748" />
  </victim-list>
  <process-list>
    <process id="process5004748" taskpriority="0" logused="0" waitresource="OBJECT: 5:1423344135:0 " waittime="3008" ownerId="2379819613" transactionname="user_transaction" lasttranstarted="2013-03-14T09:28:55.803" XDES="0x77ab8f950" lockMode="X" schedulerid="11" kpid="5416" status="suspended" spid="507" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2013-03-14T09:28:55.817" lastbatchcompleted="2013-03-14T09:28:55.807" clientapp=".Net SqlClient Data Provider" hostname="ExampleHost" hostpid="8664" loginname="ExampleUser" isolationlevel="read uncommitted (1)" xactid="2379819613" currentdb="5" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
      <executionStack>
        <frame procname="" line="1" stmtstart="616" stmtend="1504" sqlhandle="0x020000002468011b993c824e2e0ce3fd2783a30e8e591641" />
        <frame procname="" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000" />
      </executionStack>
      <inputbuf>
(@p0 datetime,@p1 bigint ...) INSERT INTO tblExample (Column1, Column2, ...); select SCOPE_IDENTITY()   
      </inputbuf>
    </process>
    <process id="processd4a988" taskpriority="0" logused="0" waitresource="OBJECT: 5:1423344135:10 " waittime="3008" ownerId="2379819595" transactionname="user_transaction" lasttranstarted="2013-03-14T09:28:55.663" XDES="0x2fe4323b0" lockMode="X" schedulerid="2" kpid="6756" status="suspended" spid="473" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2013-03-14T09:28:55.677" lastbatchcompleted="2013-03-14T09:28:55.667" clientapp=".Net SqlClient Data Provider" hostname="ExampleHost" hostpid="8664" loginname="ExampleUser" isolationlevel="read uncommitted (1)" xactid="2379819595" currentdb="5" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
      <executionStack>
        <frame procname="" line="1" stmtstart="616" stmtend="1504" sqlhandle="0x020000002468011b993c824e2e0ce3fd2783a30e8e591641" />
        <frame procname="" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000" />
      </executionStack>
      <inputbuf>
        (@p0 datetime,@p1 bigint ...) INSERT INTO tblExample (Column1, Column2, ...); select SCOPE_IDENTITY()
      </process>
  </process-list>
  <resource-list>
    <objectlock lockPartition="0" objid="1423344135" subresource="FULL" dbid="5" objectname="" id="lock5d745ae00" mode="X" associatedObjectId="1423344135">
      <owner-list>
        <owner id="processd4a988" mode="X" />
      </owner-list>
      <waiter-list>
        <waiter id="process5004748" mode="X" requestType="wait" />
      </waiter-list>
    </objectlock>
    <objectlock lockPartition="10" objid="1423344135" subresource="FULL" dbid="5" objectname="" id="lock55da8ea00" mode="IX" associatedObjectId="1423344135">
      <owner-list>
        <owner id="process5004748" mode="IX" />
      </owner-list>
      <waiter-list>
        <waiter id="processd4a988" mode="X" requestType="wait" />
      </waiter-list>
    </objectlock>
  </resource-list>
</deadlock>

更新2:添加由NHibernate生成的INSERT

begin transaction with isolation level: ReadUncommitted

INSERT INTO tblExample
            (Column1,
             Column2,
             Column2,
             Column3,
             Column4,
             Column5,
             Column6,
             Column7,
             Column8,
             Column9,
             Column10,
             Column11,
             Column12,
             Column13,
             Column14,
             Column15,
             Column16,
             Column17,
             Column18,
             Column19,
             Column20,
             Column21)
VALUES      ('2013-03-14T12:47:26.00' /* @p0 */,
             NULL /* @p1 */,
             75 /* @p2 */,
             'Test Text with some characters' /* @p3 */,
             'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22' /* @p4 */,
             2130706433 /* @p5 */,
             NULL /* @p6 */,
             NULL /* @p7 */,
             0 /* @p8 */,
             'Test Title' /* @p9 */,
             '11223344' /* @p10 */,
             0 /* @p11 */,
             '2013-03-14T12:47:26.00' /* @p12 */,
             0 /* @p13 */,
             '2013-03-14T12:47:26.00' /* @p14 */,
             'en' /* @p15 */,
             '2013-03-14T12:47:26.00' /* @p16 */,
             0 /* @p17 */,
             'SomeName' /* @p18 */,
             NULL /* @p19 */,
             917278 /* @p20 */,
             2805683 /* @p21 */);



select SCOPE_IDENTITY()

commit transaction

2 个答案:

答案 0 :(得分:3)

承担尽职调查(即您正确地进行了调查),并让我们明确说明我们正在谈论的是lock partitioning,而不是partition locking

不幸的是,除了确保运行最新的SP和最新的CU之外,您无能为力。最好是最新产品版本。这个领域有很多修复方法。 如果您为SP应用了最新的SP和最新CU,问题仍然存在,请与产品支持部门联系。

  

我可以使用跟踪标志1229禁用锁定分区,但不建议这样做

您有多少个核心?你可以随时尝试和测试。

答案 1 :(得分:0)

我们遇到了类似的问题,经过适当的调查,发现主要问题是在没有启用row_lock和page_lock的情况下创建了一些索引。因此,任何插入,更新,删除操作都会影响索引在表级生成X锁。并发操作以死锁结束。

SELECT name indexname,allow_row_locks,allow_page_locks
FROM sys.indexes
WHERE 1=1 
--and object_id = object_id('tablename') 
AND allow_row_locks = 0 
AND allow_page_locks = 0