SQL SERVER - 談死鎖的監控分析解決思路

1 背景

1.1 報警狀況

最近整理筆記,打算所有遷移到EVERNOTE。整理到鎖這一部分,裏邊恰好有個本身記錄下來的案例,從新整理分享下給你們。node

某日中午,收到報警短信,DB死鎖異常,單分鐘死鎖120個。sql

死鎖的xml文件以下:json

 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>

表格結構跟模擬數據以下:session

 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

1.2 如何監控

捕獲死鎖有多種方式能夠捕獲,這裏介紹2種:SQL SERVER Profiler工具跟Extended Events。Profiler相對比較耗資源,可是因爲只監控死鎖這一項,因此性能影響不是很大,其可視化界面較易上手;Extended Events耗費資源較少,實時記錄到倒數第二個死鎖,同時須要SQL語句來分析查詢記錄文件。app

如何使用 Profiler監控?
打開 SSMS,點擊<工具>,選擇 <SQL Server Profiler>,以下圖。ide

登陸到須要監控的DB實例,填寫相應的跟蹤屬性,首先是<常規>頁面,以下圖。這裏注意2個方面,第一,選擇 <TSQL-Locks>模板,這個模板便可以用來監控死鎖,也能夠拿來觀察 鎖申請與釋放狀況,很是詳細,有事沒事能夠多拿來看SELECT UPDATE DELETE等語句對鎖的申請及釋放狀況;第二,監控結果存儲,建議能夠存放到某個表格中去,方便按期分析與統計。工具

接着填寫<事件選擇>項,只須要選擇 <deadlock graph> Events,其餘都不須要打勾,最後點擊運行就能夠開始監控了。sqlserver

 

能夠用一個萬年經常使用的例子來檢查是否監控正常,開3個查詢窗口,按照如下順序執行則會發生資源佔用及申請互斥致使死鎖,執行完第5步,等待1-3s則發生死鎖。腳本提供以下:性能

 1 --session 1
 2 CREATE TABLE Test_DL(
 3 id int not null primary key ,
 4 name varchar(100));
 5 
 6 INSERT INTO Test_DL(id,name) select 1,'a';
 7 INSERT INTO Test_DL(id,name) select 2,'b';
 8 
 9 --session2 2 2 2 2 2 2 2 2 2 
10 BEGIN TRANSACTION
11 UPDATE Test_DL SET Name='a-test' WHERE ID=1
12 
13 --session3 3 3 3 3 3 3 3 3 3 
14 BEGIN TRANSACTION
15 UPDATE Test_DL SET Name='b-test' WHERE ID=2
16 
17 --session2 2 2 2 2 2 2 2 2 2 
18  SELECT * FROM Test_DL WHERE ID=2
19 
20 --session3 3 3 3 3 3 3 3 3 3
21  SELECT * FROM Test_DL WHERE ID=1
模擬死鎖SQL

監控到的死鎖界面以下:spa

如何使用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支持)

是否是很清晰,一目瞭然,有了這個就能夠去分析拉!

2 分析

根據xml文件內容或者擴展事件的監控內容,均可以整理爲如下信息(開頭的那個死鎖分析):

查看事務1及事務2的執行計劃以下:

 

 結合表格及執行計劃,能夠大體推測死鎖過程:

會話1:

  • 根據主鍵SeqCode查找到鍵值所在的 索引頁 Index_Page,找到該頁上面的 keyhashvalue 鍵值行 Index_key,對Index_Page持有IU鎖,對Index_key持有U鎖;
  • 因爲該表是堆表,bookmark lookup是經過 RID查找 ,即經過行標識符查找,找到RID所對應的行數據所在的 數據頁  Data_Page,而後在該頁面上找到RID指向槽號上的行數據,對該行數據持有U鎖;
  • 這個時候,已經查找到了須要更新的行數據,能夠把數據頁 Data_Page上的IU鎖 升級爲IX鎖,RID指向的行數據 從U鎖升級爲X鎖,升級結束後,釋放索引頁跟鍵值行上面的 IU鎖及U鎖。
  • 則此時,會話1 持有 Data_Page 上的IX鎖、RID行上的 X鎖.

這個過程當中,恰好會話2進行這樣的鎖申請:

  • 找出事務2中持有鎖資源是哪一個索引,能夠根據sys.partitions 能夠查看到72057594038910976是主鍵pk_FinanceReceiptNoRule,主鍵列是:SeqCode。
  • 根據主鍵SeqCode查找到鍵值所在的 索引頁 Index_Page,找到該頁上面的 鍵值行 Index_key,對Index_Page持有IU鎖,對Index_key持有U鎖;
  • 因爲該表是堆表,bookmark lookup是經過 RID查找 ,即經過行標識符查找,找到RID所對應的行數據所在的 數據頁  Data_Page,而後在該頁面上找到RID指向槽號上的行數據,準備該行數據持有U鎖,可是發現RID行上被會話1持有了X鎖,致使其申請 U鎖 Timeout。
  • 則此時 會話2 持有 Index_Page上的IU鎖、Index_key上的U鎖、Data_Page上的IU鎖,請求 RID行的 U鎖。

假設這個時候,會話1 中又執行了一次update操做(同一個事務中):

  • 根據主鍵SeqCode查找到鍵值所在的 索引頁 Index_Page,找到該頁上面的 鍵值行 Index_key,對Index_Page持有IU鎖,準備對Index_key持有U鎖,可是發現 Index_key被會話2持有了U鎖。

那麼這個時候死鎖就產生了(詳見下圖):

  • 會話1 持有 Data_Page 上的IX鎖、RID行上的 X鎖,申請 Index_key 的U鎖(等待會話2釋放)
  • 會話2 持有 Index_Page上的IU鎖、Index_key上的U鎖、Data_Page上的IU鎖,請求 RID行的 U鎖(等待會話1釋放)

3 解決

想法子除去RID查找,直接index就找到數據,就不會發生這個死鎖,也就是,在主鍵上面從新創建彙集索引,丟棄原先的非彙集索引主鍵。由於這樣排除了RID的U鎖申請與持有,直接是保持X鎖 直至事務結束,同時能夠直接根據主鍵來修改鍵值所在的數據頁,減小的RID查詢行的時間。

修改後的執行計劃以下:

其鎖申請釋放的流程以下(詳見截圖):

  • 根據主鍵SeqCode查找到鍵值所在的 索引頁 Index_Page,找到該頁上面的 keyhashvalue 鍵值行 Index_key,對Index_Page持有IU鎖,對Index_key持有U鎖;
  • 因爲該表已是彙集索引表,主鍵所在的頁上包含 行數據,則能夠直接 對Index_Page持有IU鎖升級爲IX鎖,對Index_key持有U鎖升級爲X鎖,避免了RID逐個找行數據的鎖申請

相關文章
相關標籤/搜索