我面临着一些反复出现的死锁,其中之一是键锁,并且包含带有XLOCK提示的SELECT查询,该查询成为了死锁的受害者。另一个语句是对其中一个表的INSERT,该表是第一个查询的视图的一部分。
视图:
create view dbo.viewE
as
select * from dbo.E
where myValue > 13000
选择查询:
select * from dbo.viewE with (XLOCK) where A > GETUTCDATE()
INSERT语句:
INSERT INTO [dbo].[E] (myValue,A) VALUES (10,GetDate())
基础表dbo.E在大约20列中拥有约300万行,其中有些是ntext。
取出查询并使用两个事务手动进行模拟,该行为是可重现的。如果从选择中删除了XLOCK,则行为会更改。
死锁图:
<deadlock-list>
<deadlock victim="process222222221">
<process-list>
<process id="process222222221" taskpriority="0" logused="0" waitresource="KEY: 5:72057604035644444 (ccdf51accc0c)" waittime="2522" ownerId="27202256401" transactionname="SELECT" lasttranstarted="2015-09-14T16:32:36.160" XDES="0x2f1ec5ca0" lockMode="RangeX-X" schedulerid="15" kpid="12936" status="suspended" spid="359" sbid="0" ecid="0" priority="0" trancount="0" lastbatchstarted="2015-09-14T16:32:36.160" lastbatchcompleted="2015-09-14T16:32:36.160" clientapp="x" hostname="x" hostpid="14536" loginname="x" isolationlevel="serializable (4)" xactid="27202256401" currentdb="5" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="48" sqlhandle="0x02000000611e4523142b2318c47c87313a9b2ba587ff3130">
SELECT * FROM viewE WITH (XLOCK) WHERE A < GetUtcDate() </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(@UICulture nvarchar(5))SELECT * FROM viewE WITH (XLOCK) WHERE A < GetUtcDate() </inputbuf>
</process>
<process id="process6022222" taskpriority="0" logused="161152" waitresource="KEY: 5:72057604035644444 (cd874c2ba438)" waittime="1370" ownerId="27202248438" transactionguid="0x8de5ccd6eeef67469c6234af59e44ca5" transactionname="DTCXact" lasttranstarted="2015-09-14T16:32:34.767" XDES="0x4aa0bf950" lockMode="RangeI-N" schedulerid="14" kpid="6636" status="suspended" spid="329" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2015-09-14T16:32:37.300" lastbatchcompleted="2015-09-14T16:32:37.300" clientapp="x" hostname="x" hostpid="14536" loginname="x" isolationlevel="read uncommitted (1)" xactid="27202248438" currentdb="5" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="936" sqlhandle="0x020000004853462f09790a4ddedc0d574c2afa539aef1c0e">
INSERT INTO [E] ([a], [b], [c],...) VALUES (@aDate, @bDate, @c, ...)
</frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>INSERT INTO [E] ([a], [b], [c],...) VALUES (@aDate, @bDate, @c, ...)
</inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057604035644444" dbid="5" objectname="db.dbo.E" indexname="IX_index1" id="lock258b6dc80" mode="X" associatedObjectId="72057604035644444">
<owner-list>
<owner id="process6022222" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process222222221" mode="RangeX-X" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057604035644444" dbid="5" objectname="db.dbo.E" indexname="IX_index1" id="lock7b145c400" mode="RangeX-X" associatedObjectId="72057604035644444">
<owner-list>
<owner id="process222222221" mode="RangeX-X"/>
</owner-list>
<waiter-list>
<waiter id="process6022222" mode="RangeI-N" requestType="wait"/>
</waiter-list>
</keylock>
</resource-list>
</deadlock>
</deadlock-list>
据我了解,我正在查看KEYLOCK死锁,它基本上是由未发现的索引查询引起的,该查询使用非聚集索引和聚集索引来收集所需的值,对吗?
我的问题:
- 由于涉及必需的NTEXT列,因此无法创建覆盖索引。大大减少行数在这里有帮助吗?
- 有什么好理由我只是不知道SELECT是用XLOCK执行的?没有XLOCK也会发生死锁吗?