标签:info const begin alt lib tac write exec ons
最近整理笔记,打算全部迁移到EVERNOTE。整理到锁这一部分,里边刚好有个自己记录下来的案例,重新整理分享下给大家。
某日中午,收到报警短信,DB死锁异常,单分钟死锁120个。
死锁的xml文件如下:
1 <deadlock-list> 2 <deadlock victim="process810b00cf8"> 3 <process-list> 4 <process id="process810b00cf8" taskpriority="0" logused="0" waitresource="RID: 13:1:1541136:62" waittime="7682" ownerId="3396587959" transactionname="UPDATE" lasttranstarted="2016-01-08T12:03:51.067" XDES="0xa99746d08" lockMode="U" schedulerid="41" kpid="17308" status="suspended" spid="108" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2016-01-08T12:03:51.067" lastbatchcompleted="2016-01-08T12:03:51.067" lastattention="1900-01-01T00:00:00.067" clientapp="Microsoft SQL Server Management Studio - 查询" hostname="test-server" hostpid="1433" loginname="xinysu" isolationlevel="read committed (2)" xactid="3396587959" currentdb="13" lockTimeout="4294967295" clientoption1="671098976" clientoption2="390200"> 5 <executionStack> 6 <frame procname="adhoc" line="7" stmtstart="214" stmtend="484" sqlhandle="0x020000003acf4f010561e479685209fb09a7fd15239977c60000000000000000000000000000000000000000"> 7 UPDATE FinanceReceiptNoRule SET NowSeqValue=@ReturnNum,ISRUNNING=‘0‘,LastWriteTime=GETDATE() WHERE IsRunning=‘1‘ AND SeqCode=@SeqCode </frame> 8 </executionStack> 9 <inputbuf> 10 declare @SeqCode varchar(60) 11 declare @ReturnNum bigint 12 set @SeqCode=‘CGJS20160106‘ 13 while(1=1) 14 begin 15 UPDATE FinanceReceiptNoRule SET NowSeqValue=@ReturnNum,ISRUNNING=‘0‘,LastWriteTime=GETDATE() WHERE IsRunning=‘1‘ AND SeqCode=@SeqCode 16 end </inputbuf> 17 </process> 18 <process id="process18fd5d8cf8" taskpriority="0" logused="248" waitresource="KEY: 13:72057594040090624 (b3ade7c5980c)" waittime="4" ownerId="3396522828" transactionname="user_transaction" lasttranstarted="2016-01-08T12:03:05.310" XDES="0x18c1db63a8" lockMode="U" schedulerid="57" kpid="16448" status="suspended" spid="161" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2016-01-08T12:03:58.737" lastbatchcompleted="2016-01-08T12:03:33.847" lastattention="2016-01-08T12:03:33.850" clientapp="Microsoft SQL Server Management Studio - 查询" hostname="test-server" hostpid="1433" loginname="xinysu" isolationlevel="read committed (2)" xactid="3396522828" currentdb="13" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200"> 19 <executionStack> 20 <frame procname="adhoc" line="6" stmtstart="210" stmtend="400" sqlhandle="0x020000001b4f23368af7bba99098c10dec46585804f1b4ce0000000000000000000000000000000000000000"> 21 Update dbo.FinanceReceiptNoRule Set [IsRunning]=‘1‘ where SeqCode=@SeqCode and IsRunning=‘0‘ </frame> 22 </executionStack> 23 <inputbuf> 24 declare @SeqCode varchar(60) 25 declare @ReturnNum bigint 26 set @SeqCode=‘CGJS20160106‘ 27 while(1=1) 28 begin 29 Update dbo.FinanceReceiptNoRule Set [IsRunning]=‘1‘ where SeqCode=@SeqCode and IsRunning=‘0‘ 30 end 31 </inputbuf> 32 </process> 33 </process-list> 34 <resource-list> 35 <ridlock fileid="1" pageid="1541136" dbid="13" objectname="fin_test.dbo.FinanceReceiptNoRule" id="lock51e8a3980" mode="X" associatedObjectId="72057594040025088"> 36 <owner-list> 37 <owner id="process18fd5d8cf8" mode="X" /> 38 </owner-list> 39 <waiter-list> 40 <waiter id="process810b00cf8" mode="U" requestType="wait" /> 41 </waiter-list> 42 </ridlock> 43 <keylock hobtid="72057594040090624" dbid="13" objectname="fin_test.dbo.FinanceReceiptNoRule" indexname="PK_FINANCERECEIPTNORULE" id="lock7b2c6bc80" mode="U" associatedObjectId="72057594040090624"> 44 <owner-list> 45 <owner id="process810b00cf8" mode="U" /> 46 </owner-list> 47 <waiter-list> 48 <waiter id="process18fd5d8cf8" mode="U" requestType="wait" /> 49 </waiter-list> 50 </keylock> 51 </resource-list> 52 </deadlock> 53 </deadlock-list>
表格结构跟模拟数据如下:
1 --涉及表格: 2 CREATE TABLE [dbo].[FinanceReceiptNoRule]( 3 [SeqCode] [varchar](60) NOT NULL, 4 [NowSeqValue] [bigint] NULL, 5 [SeqDate] [varchar](14) NOT NULL, 6 [IsRunning] [varchar](1) NULL, 7 [LastWriteTime] [datetime] NULL, 8 [Prefix] [varchar](4) NULL 9 ) ON [PRIMARY] 10 GO 11 --数据模拟 12 INSERT [dbo].[FinanceReceiptNoRule] ([SeqCode], [NowSeqValue], [SeqDate], [IsRunning], [LastWriteTime], [Prefix]) VALUES (N‘TEST20150108‘, 1469, N‘20150108‘, N‘0‘, CAST(N‘2015-01-08 05:05:49.163‘ AS DateTime), N‘TEST‘) 13 GO 14 INSERT [dbo].[FinanceReceiptNoRule] ([SeqCode], [NowSeqValue], [SeqDate], [IsRunning], [LastWriteTime], [Prefix]) VALUES (N‘TEST20150109‘, 1377, N‘20150109‘, N‘0‘, CAST(N‘2015-01-09 04:50:26.610‘ AS DateTime), N‘TEST‘) 15 GO 16 17 ALTER TABLE [dbo].[FinanceReceiptNoRule] ADD CONSTRAINT [pk_FinanceReceiptNoRule] PRIMARY KEY NONCLUSTERED 18 ( 19 [SeqCode] ASC 20 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 21 GO
捕获死锁有多种方式可以捕获,这里介绍2种:SQL SERVER Profiler工具跟Extended Events。Profiler相对比较耗资源,但是由于只监控死锁这一项,所以性能影响不是很大,其可视化界面较易上手;Extended Events耗费资源较少,实时记录到倒数第二个死锁,同时需要SQL语句来分析查询记录文件。
如何使用 Profiler监控?
打开 SSMS,点击<工具>,选择 <SQL Server Profiler>,如下图。
登录到需要监控的DB实例,填写相应的跟踪属性,首先是<常规>页面,如下图。这里注意2个方面,第一,选择 <TSQL-Locks>模板,这个模板即可以用来监控死锁,也可以拿来观察 锁申请与释放情况,非常详细,有事没事可以多拿来看SELECT UPDATE DELETE等语句对锁的申请及释放情况;第二,监控结果存储,建议可以存放到某个表格中去,方便定期分析与统计。
接着填写<事件选择>项,只需要选择 <deadlock graph> Events,其他都不需要打勾,最后点击运行就可以开始监控了。
可以用一个万年常用的例子来检查是否监控正常,开3个查询窗口,按照以下顺序执行则会发生资源占用及申请互斥导致死锁,执行完第5步,等待1-3s则发生死锁。脚本提供如下:
监控到的死锁界面如下:
如何使用Extended Events监控?
建立扩展事件监控的脚本如下:(扩展事件很赞,2012版支持可视化操作,感兴趣的可以上 MSDN了解:https://msdn.microsoft.com/zh-cn/library/bb630282.aspx,本文就不分析语法等知识点了)
1 CREATE EVENT SESSION [DeadLock] ON SERVER 2 ADD EVENT sqlserver.xml_deadlock_report 3 ADD TARGET package0.event_file(SET filename=N‘F:\events\deadlock\deadlock.xel‘,max_file_size=(20)), 4 ADD TARGET package0.ring_buffer(SET max_events_limit=(100),max_memory=(10240),occurrence_number=(50)) 5 WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=ON) 6 GO
查询SQL如下,这里需要注意:查询是基于buffer还是基于filer分析,一般buffer存储的个数都是有限的,比如上文我们只分配了4M存储,file分析则是完整的,但是要看保留的文件个数。这里我们给出buffer的查询SQL如下,file的查询大家感兴趣的可以动手写下。
DECLARE @deadlock_xml XML
SELECT @deadlock_xml=(
SELECT
(
SELECT
CONVERT(XML, target_data)
FROM sys.dm_xe_session_targets st
JOIN sys.dm_xe_sessions s ON s.address = st.event_session_address
WHERE s.name = ‘deadlock‘ AND st.target_name = ‘ring_buffer‘
) AS [x]
FOR XML PATH(‘‘) , TYPE
)
SELECT dateadd(hour,+6,tb.col.value(‘@timestamp[1]‘,‘varchar(max)‘)) TimePoint, tb.col.value(‘(data/value/deadlock/process-list/process/executionStack/frame)[1]‘,‘VARCHAR(MAX)‘) statement_parameter_k, tb.col.value(‘(data/value/deadlock/process-list/process/executionStack/frame)[2]‘,‘VARCHAR(MAX)‘) statement_k, tb.col.value(‘(data/value/deadlock/process-list/process/executionStack/frame)[3]‘,‘VARCHAR(MAX)‘) statement_parameter, tb.col.value(‘(data/value/deadlock/process-list/process/executionStack/frame)[4]‘,‘VARCHAR(MAX)‘) [statement], tb.col.value(‘(data/value/deadlock/process-list/process/@waitresource)[1]‘,‘VARCHAR(MAX)‘) waitresource_k, tb.col.value(‘(data/value/deadlock/process-list/process/@waitresource)[2]‘,‘VARCHAR(MAX)‘) waitresource, tb.col.value(‘(data/value/deadlock/process-list/process/@isolationlevel)[1]‘,‘VARCHAR(MAX)‘) isolationlevel_k, tb.col.value(‘(data/value/deadlock/process-list/process/@isolationlevel)[2]‘,‘VARCHAR(MAX)‘) isolationlevel, tb.col.value(‘(data/value/deadlock/process-list/process/@waittime)[1]‘,‘VARCHAR(MAX)‘) waittime_k, tb.col.value(‘(data/value/deadlock/process-list/process/@waittime)[2]‘,‘VARCHAR(MAX)‘) waittime, tb.col.value(‘(data/value/deadlock/process-list/process/@clientapp)[1]‘,‘VARCHAR(MAX)‘) clientapp_k, tb.col.value(‘(data/value/deadlock/process-list/process/@clientapp)[2]‘,‘VARCHAR(MAX)‘) clientapp, tb.col.value(‘(data/value/deadlock/process-list/process/@hostname)[1]‘,‘VARCHAR(MAX)‘) hostname_k, tb.col.value(‘(data/value/deadlock/process-list/process/@hostname)[2]‘,‘VARCHAR(MAX)‘) hostname FROM @deadlock_xml.nodes(‘//event‘) as tb(col)
这个SQL可以查询的出非常详细的资源争夺情况,如果想要有效的使用扩展事件,建议大家详细查看下官网的xml语法(SQL SERVER对xml的支持也是棒棒哒,期待2016版中的json支持)
是不是很清晰,一目了然,有了这个就可以去分析拉!
根据xml文件内容或者扩展事件的监控内容,都可以整理为以下信息(开头的那个死锁分析):
查看事务1及事务2的执行计划如下:
结合表格及执行计划,可以大致推测死锁过程:
会话1:
这个过程中,刚好会话2进行这样的锁申请:
假设这个时候,会话1 中又执行了一次update操作(同一个事务中):
那么这个时候死锁就产生了(详见下图):
想法子除去RID查找,直接index就找到数据,就不会发生这个死锁,也就是,在主键上面重新建立聚集索引,丢弃原先的非聚集索引主键。因为这样排除了RID的U锁申请与持有,直接是保持X锁 直至事务结束,同时可以直接根据主键来修改键值所在的数据页,减少的RID查询行的时间。
修改后的执行计划如下:
其锁申请释放的流程如下(详见截图):
标签:info const begin alt lib tac write exec ons
原文地址:http://www.cnblogs.com/Leo_wl/p/6539091.html