死锁的奇怪的地方:死锁设计到的表示heap表,但是对key发生死锁,但是对key进行等待,死锁设计到的hotbid,无法再sys.partitions查询到objectid(这个误判是我们有索引的重建,导致hotbid变化,无法再表中查找到)
问题是出现在一个非聚集索引上,这个索引是key是uame,是走索引的,为什么和insert死锁呢
线索:表中delete的where 字段,参数化传进来使用的是nvarchar,但是表中却是使用varchar
死锁信息:
<deadlock-list> <deadlock> <victim-list> <victimProcess id="processd245288"/> </victim-list> <process-list> <process id="processd245288" taskpriority="0" logused="0" waitresource="KEY: 7:72057594592690176 (6630517ab975)" waittime="811" ownerId="9523151791" transactionname="user_transaction" lasttranstarted="2013-11-22T14:31:17.393" XDES="0x54aa7b950" lockMode="U" schedulerid="31" kpid="2752" status="suspended" spid="96" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2013-11-22T14:31:17.393" lastbatchcompleted="2013-11-22T14:31:17.393" clientapp=".Net SqlClient Data Provider" hostname="WIN-D03SNI5P5VJ" hostpid="27716" loginname="tianyi" isolationlevel="read committed (2)" xactid="9523151791" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056"> <executionStack> <frame procname="" line="1" stmtstart="52" sqlhandle="0x02000000daa8051aae1fa7d63016b7f92bff8cc79cd2601b"> </frame> <frame procname="" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000"> </frame> </executionStack> <inputbuf> (@Name nvarchar(4000))delete from [a] where Uame=@Name </inputbuf> </process> <process id="processd2274c8" taskpriority="0" logused="896" waitresource="KEY: 7:72057594592690176 (7f24bf9871b5)" waittime="810" ownerId="9523151781" transactionname="user_transaction" lasttranstarted="2013-11-22T14:31:17.387" XDES="0x3c692b3c0" lockMode="X" schedulerid="28" kpid="18272" status="suspended" spid="136" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2013-11-22T14:31:17.400" lastbatchcompleted="2013-11-22T14:31:17.397" clientapp=".Net SqlClient Data Provider" hostname="WIN-D03SNI5P5VJ" hostpid="27716" loginname="tianyi" isolationlevel="read committed (2)" xactid="9523151781" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056"> <executionStack> <frame procname="" line="1" stmtstart="290" sqlhandle="0x020000003e2b5d0915ab458804135b710c76331b2acad94d"> </frame> <frame procname="" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000"> </frame> </executionStack> <inputbuf> (@Time datetime,@ID int,@Gre int,@OerID int,@rID uniqueidentifier,@RUD nvarchar(4000),@Uame nvarchar(4000))INSERT INTO [a]([a],[CID],[rNe],[AddTime],[Gere],[RID],[OID]) VALUES(@ProduByUserID,@ClID,@Usame,@Adme,@Gee,@RD,@OID) </inputbuf> </process> </process-list> <resource-list> <keylock hobtid="72057594592690176" dbid="7" objectname="" indexname="" id="lock8c9066380" mode="X" associatedObjectId="72057594592690176"> <owner-list> <owner id="processd2274c8" mode="X"/> </owner-list> <waiter-list> <waiter id="processd245288" mode="U" requestType="wait"/> </waiter-list> </keylock> <keylock hobtid="72057594592690176" dbid="7" objectname="" indexname="" id="lock6d968ab80" mode="U" associatedObjectId="72057594592690176"> <owner-list> <owner id="processd245288" mode="U"/> </owner-list> <waiter-list> <waiter id="processd2274c8" mode="X" requestType="wait"/> </waiter-list> </keylock> </resource-list> </deadlock> </deadlock-list>
原因:
目前还没找到原因但是估计和nvarchar 和 varchar 的隐式类型转化有关
大概问题我已经发现了,虽然delete 是走索引的,但是在计划上发现GetRangeThroughConvert 对类型进行转化,转后后,出来3个计算标量,
然后再索引查找的时候对最小的,最大的标量进行 查找 就是 标量1<uame<标量3,这样导致delete 查找的范围扩大,导致和insert之间的锁请求冲突
其他资料:
资料包括:表结构,和delete 的执行计划,如果有兴趣研究在资料方面我会尽力,按需求补全
涉及到公司的一些表结构,不方便在公共场合透露,如果有朋友需要可以发我的邮箱索取,我的邮箱: fanzhouqi@gmail.com