稍作收拾发表

1、模拟死锁

首先创造测量检验数据,然后展开供给的追踪,最终试行四个语句模拟死锁。

1.2、开启死锁会话

张开贰个会话进行校勘

图片 1图片 2

----模拟高频update操作
declare @i int
set @i = 100
while 1=1
begin
    update testklup set cont1 = @i where clskey = 1  
    set @i = @i+1
end

View Code

拉开此外四个对话举办询问

图片 3图片 4

----模拟高频select操作
declare @cont2 char(3000)
while 1=1
begin
    select @cont2=cont2 from testklup where nlskey=1
end

View Code

两条语句一同运转,无须多久就能有内部一个老是遇到死锁的大谬否则:
图片 5

2.2、死锁图

在追踪文件里,能够看出那样的死锁图形:
图片 6
大家能够从追踪文件中,提取死锁事件数量,将死锁图保存为死锁XML文件(.xdl)。然后针对.xdl文件使用记事本打开,整个死锁的剧情了然入怀。

图片 7图片 8

/** 死锁事件数据是后期提取的,与原死锁图稍有不同 **/
-- 第一部分
<deadlock-list>
    <deadlock victim="process2b6b000">
        -- 第二部分
        <process-list>
            <process id="process2b6b000" taskpriority="0" logused="0" waitresource="KEY: 8:72057594040025088 (8194443284a0)" waittime="636" ownerId="758239" transactionname="SELECT" lasttranstarted="2016-10-26T09:59:00.300" XDES="0x5dba408" lockMode="S" schedulerid="1" kpid="4008" status="suspended" spid="57" sbid="0" ecid="0" priority="0" trancount="0" lastbatchstarted="2016-10-26T09:59:00.300" lastbatchcompleted="2016-10-26T09:58:55.807" clientapp="Microsoft SQL Server Management Studio - 查询" hostname="LIXUANYAO" hostpid="5872" loginname="LIXUANYAO\Administrator" isolationlevel="read committed (2)" xactid="758239" currentdb="8" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
                <executionStack>
                    <frame procname="adhoc" line="5" stmtstart="134" stmtend="232" sqlhandle="0x02000000c3289f0a3b1699a92a155bf6611d1a82d2dbd79a">
                        select @cont2=cont2 from testklup where nlskey=1     
                    </frame>
                </executionStack>
                <inputbuf>
                    ----模拟高频select操作
                    declare @cont2 char(3000)
                    while 1=1
                    begin
                        select @cont2=cont2 from testklup where nlskey=1
                    end    
                </inputbuf>
            </process>
            <process id="process2dc2aa8" taskpriority="0" logused="228" waitresource="KEY: 8:72057594040090624 (8194443284a0)" waittime="636" ownerId="758240" transactionname="UPDATE" lasttranstarted="2016-10-26T09:59:00.307" XDES="0x5bf6c08" lockMode="X" schedulerid="4" kpid="5832" status="suspended" spid="55" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2016-10-26T09:58:59.470" lastbatchcompleted="2016-10-26T09:58:58.073" lastattention="2016-10-26T09:58:58.073" clientapp="Microsoft SQL Server Management Studio - 查询" hostname="LIXUANYAO" hostpid="5872" loginname="LIXUANYAO\Administrator" isolationlevel="read committed (2)" xactid="758240" currentdb="8" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
                <executionStack>
                    <frame procname="adhoc" line="6" stmtstart="140" stmtend="248" sqlhandle="0x0200000087695c3ac89c30f0ca649915b3e2cb1c22db1292">
                        update testklup set cont1 = @i where clskey = 1     
                    </frame>
                </executionStack>
                <inputbuf>
                    ----模拟高频update操作
                    declare @i int
                    set @i = 100
                    while 1=1
                    begin
                        update testklup set cont1 = @i where clskey = 1  
                        set @i = @i+1
                    end    
                </inputbuf>
            </process>
        </process-list>
        -- 第三部分
        <resource-list>
            <keylock hobtid="72057594040025088" dbid="8" objectname="Test.dbo.testklup" indexname="inx_cls" id="locka49e800" mode="X" associatedObjectId="72057594040025088">
                <owner-list>
                    <owner id="process2dc2aa8" mode="X"/>
                </owner-list>
                <waiter-list>
                    <waiter id="process2b6b000" mode="S" requestType="wait"/>
                </waiter-list>
            </keylock>
            <keylock hobtid="72057594040090624" dbid="8" objectname="Test.dbo.testklup" indexname="inx_nlcs" id="locka47ab40" mode="S" associatedObjectId="72057594040090624">
                <owner-list>
                    <owner id="process2b6b000" mode="S"/>
                </owner-list>
                <waiter-list>
                    <waiter id="process2dc2aa8" mode="X" requestType="wait"/>
                </waiter-list>
            </keylock>
        </resource-list>
    </deadlock>
</deadlock-list>

View Code

SPID=57:查询语句使用非聚焦索引inx_nlcs查找nlskey=1的记录,持有inx_nlcs上的S锁;由于索引未覆盖列cont2,需通过键查询拿到cont2,于是申请inx_cls上的S锁。
SPID=55:更新语句使用聚焦索引inx_cls查找clskey=1的记录,持有inx_cls上的X锁;由于非聚焦索引inx_nlcs蕴涵列cont1,在更新表中记录后,还需纠正非集中索引中的数值,于是提请inx_ncls上的X锁。

图片 9图片 10

--hobtid
SELECT OBJECT_NAME(p.object_id) AS TableName,
       i.name AS IndexName
FROM sys.partitions AS p
     INNER JOIN sys.indexes AS i ON p.object_id = i.object_id
                                    AND p.index_id = i.index_id
WHERE p.hobt_id = 72057594040090624
--associatedObjectId
SELECT OBJECT_NAME(p.object_id) AS TableName,
       i.name AS IndexName
FROM sys.partitions AS p
     INNER JOIN sys.indexes AS i ON p.object_id = i.object_id
                                    AND p.index_id = i.index_id
WHERE p.partition_id = 72057594040025088

--case when type = 1|3 then container_id = sys.partitions.hobt_id
--case when type = 2 then container_id = sys.partitions.partition_id
SELECT * FROM sys.allocation_units WHERE container_id=72057594040090624
SELECT * FROM sys.allocation_units WHERE container_id=72057594040025088

SELECT %%lockres%% AS keyhashvalue,* FROM testklup WHERE %%lockres%% ='(8194443284a0)'

View Code

2.1、1222标志

这会儿在Errorlog文件里,就拜会到上面包车型大巴输出

图片 11图片 12

spid16s     deadlock-list
spid16s      deadlock victim=process5f3000
spid16s       process-list
spid16s        process id=process5f3000 taskpriority=0 logused=0 waitresource=KEY: 8:72057594040025088 (8194443284a0) waittime=3300 ownerId=16991311 transactionname=SELECT lasttranstarted=2016-09-28T15:07:26.720 XDES=0xe291448 lockMode=S schedulerid=4 kpid=5748 status=suspended spid=57 sbid=0 ecid=0 priority=0 trancount=0 lastbatchstarted=2016-09-28T15:07:26.717 lastbatchcompleted=2016-09-28T15:02:29.293 clientapp=Microsoft SQL Server Management Studio - 查询 hostname=LIXUANYAO hostpid=7584 loginname=LIXUANYAO\Administrator isolationlevel=read committed (2) xactid=16991311 currentdb=8 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
spid16s         executionStack
spid16s          frame procname=adhoc line=5 stmtstart=134 stmtend=232 sqlhandle=0x02000000c3289f0a3b1699a92a155bf6611d1a82d2dbd79a
spid16s     select @cont2=cont2 from testklup where nlskey=1     
spid16s         inputbuf
spid16s     ----模拟高频select操作
spid16s     declare @cont2 char(3000)
spid16s     while 1=1
spid16s     begin
spid16s         select @cont2=cont2 from testklup where nlskey=1
spid16s     end    
spid16s        process id=process5f3558 taskpriority=0 logused=228 waitresource=KEY: 8:72057594040090624 (8194443284a0) waittime=3300 ownerId=16991312 transactionname=UPDATE lasttranstarted=2016-09-28T15:07:26.720 XDES=0xe291860 lockMode=X schedulerid=4 kpid=7240 status=suspended spid=55 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2016-09-28T15:07:24.843 lastbatchcompleted=2016-09-28T15:07:23.603 clientapp=Microsoft SQL Server Management Studio - 查询 hostname=LIXUANYAO hostpid=7584 loginname=LIXUANYAO\Administrator isolationlevel=read committed (2) xactid=16991312 currentdb=8 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
spid16s         executionStack
spid16s          frame procname=adhoc line=6 stmtstart=140 stmtend=248 sqlhandle=0x0200000087695c3ac89c30f0ca649915b3e2cb1c22db1292
spid16s     update testklup set cont1 = @i where clskey = 1     
spid16s         inputbuf
spid16s     ----模拟高频update操作
spid16s     declare @i int
spid16s     set @i = 100
spid16s     while 1=1
spid16s     begin
spid16s         update testklup set cont1 = @i where clskey = 1  
spid16s         set @i = @i+1
spid16s     end    
spid16s       resource-list
spid16s        keylock hobtid=72057594040025088 dbid=8 objectname=Test.dbo.testklup indexname=inx_cls id=locka2bc5c0 mode=X associatedObjectId=72057594040025088
spid16s         owner-list
spid16s          owner id=process5f3558 mode=X
spid16s         waiter-list
spid16s          waiter id=process5f3000 mode=S requestType=wait
spid16s        keylock hobtid=72057594040090624 dbid=8 objectname=Test.dbo.testklup indexname=inx_nlcs id=locka2b8d80 mode=S associatedObjectId=72057594040090624
spid16s         owner-list
spid16s          owner id=process5f3000 mode=S
spid16s         waiter-list
spid16s          waiter id=process5f3558 mode=X requestType=wait

View Code

先来拜望这个输出是如何看头,达成下表:

参与者 process5f3000 process5f3558
牺牲者  
SPID 57 55
连接背景

Microsoft SQL Server Management Studio – 查询

hostname=LIXUANYAO

hostpid=7584

loginname=LIXUANYAO\Administrator

Microsoft SQL Server Management Studio – 查询

hostname=LIXUANYAO

hostpid=7584

loginname=LIXUANYAO\Administrator

正在申请中资源/类型 KEY: 8:72057594040025088 (8194443284a0)/ Mode:S KEY: 8:72057594040090624 (8194443284a0)/ Mode:X
当前开启了几层事务 0 2
事务隔离级别 read committed (2) read committed (2)
当前正在运行的语句 select @cont2=cont2 from testklup where nlskey=1 update testklup set cont1 = @i where clskey = 1
当前正在运行的批处理

–模拟高频select操作

declare @cont2 char(3000)

while 1=1

begin

    select @cont2=cont2 from testklup where nlskey=1

end

–模拟高频update操作

declare @i int

set @i = 100

while 1=1

begin

    update testklup set cont1 = @i where clskey = 1  

    set @i = @i+1

end

死锁资源 资源2 资源1
资源类型 keylock keylock
具体内容

hobtid=72057594040090624 dbid=8 objectname=Test.dbo.testklup 

indexname=inx_nlcs id=locka2b8d80

hobtid=72057594040025088 dbid=8 objectname=Test.dbo.testklup 

indexname=inx_cls id=locka2bc5c0

持有资源进程/类型 process5f3000(spid57) / mode:S process5f3558(spid55) / mode:X
等待资源进程/类型 process5f3558(spid55) / mode:X process5f3000(spid57) / mode:S

1.1、创造测验数据

成立测验数据表、创制索引

图片 13图片 14

create table testklup
(
clskey int not null,
nlskey int not null,
cont1  int not null,
cont2  char(3000)
)

create unique clustered index inx_cls on testklup(clskey)

create unique nonclustered index inx_nlcs  on testklup(nlskey) include(cont1)

insert into testklup select 1,1,100,'aaa'
insert into testklup select 2,2,200,'bbb'
insert into testklup select 3,3,300,'ccc'

View Code

在测试在此以前,先张开监视死锁的按钮1222,让SQL
Server遭受死锁时,在Errorlog文件里打印出死锁的详细内容。

DBCC TRACEON(1222,-1)

也足以并且接纳SQL
Trace来捕捉和死锁相关的消息。首要选取事件Locks->Daedlock graph

草稿箱八十篇小说未有宣布,零零碎碎记录着早就感到还不易的知识点。稍作整理公布,方便今后翻看。2014-11-26
18:04 整理,未透露

2、分析死锁

在拉开供给的追踪并抓获到死锁后,就足以对其打开拆解剖判。

3、Deadlocks-Example

SQL
Server上的一个意料之外的Deadlock及其解析方法:
SQL Server Deadlocks by
Example:

相关文章