死鎖的四個(gè)必要條件:
互斥條件(Mutual exclusion):資源不能被共享,只能由一個(gè)進(jìn)程使用。
請求與保持條件(Hold and wait):已經(jīng)得到資源的進(jìn)程可以再次申請新的資源。
非剝奪條件(No pre-emption):已經(jīng)分配的資源不能從相應(yīng)的進(jìn)程中被強(qiáng)制地剝奪。
循環(huán)等待條件(Circular wait):系統(tǒng)中若干進(jìn)程組成環(huán)路,該環(huán)路中每個(gè)進(jìn)程都在等待相鄰進(jìn)程正占用的資源。
倉庫揀貨卡死,排查了數(shù)據(jù)庫的很多地方,都沒有頭緒,最后到SQL Server 錯(cuò)誤日志里查看,終于發(fā)現(xiàn)了蛛絲馬跡
EXEC xp_readerrorlog 0,1,NULL,NULL,'2015-09-21','2015-10-10','DESC'
waiter id=process5c30e08 mode=U requestType=wait
waiter-list
owner id=process5c26988 mode=X
owner-list
keylock hobtid=72057597785604096 dbid=33 objectname=stoxxx.dbo.Orderxxx indexname=IX_PricingExpressProductCode_State id=lock17fa96980 mode=X associatedObjectId=72057597785604096
waiter id=process5c26988 mode=U requestType=wait
waiter-list
owner id=process5c30e08 mode=X
owner-list
keylock hobtid=72057597785604096 dbid=33 objectname=stoxxx.dbo.Orderxxx indexname=IX_PricingExpressProductCode_State id=lock87d69e780 mode=X associatedObjectId=72057597785604096
resource-list
(@OperateState money,@HandledByNewWms bit,@State int,@OrderOut int)
UPDATE [Orderxx] SET [OperateState] = @OperateState,[HandledByNewWms] = @HandledByNewWms WHERE (([Orderxxx].[State] = @State) And ([Orderxxx].[OrderOut] = @OrderOut) And ([Orderxxx].[PricingExpressProductCode] IN ('UKNIR')))
inputbuf
unknown
frame procname=unknown line=1 sqlhandle=0x000000000000000000000000000000000000000000000000
UPDATE [Orderxxx] SET [OperateState] = @OperateState,[HandledByNewWms] = @HandledByNewWms WHERE (([Orderxxx].[State] = @State) And ([Orderxxx].[OrderOut] = @OrderOut) And ([Orderxxx].[PricingExpressProductCode] IN ('UKNIR')))
frame procname=adhoc line=1 stmtstart=134 sqlhandle=0x020000009d376d18a17e7ea51289d8caa2fb4de65c976389
executionStack
process id=process5c30e08 taskpriority=0 logused=10320 waitresource=KEY: 33:72057597785604096 (112399c2054a) waittime=4813 ownerId=31578743038 transactionname=user_transaction lasttranstarted=2015-09-24T10:22:58.410 XDES=0x372e95950 lockMode=U schedulerid=17 kpid=8496 status=suspended spid=153 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2015-09-24T10:22:58.540 lastbatchcompleted=2015-09-24T10:22:58.540 clientapp=.Net SqlClient Data Provider hostname=CK1-WIN-WEB02 hostpid=37992 loginname=ck1.biz isolationlevel=read committed (2) xactid=31578743038 currentdb=33 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
(@OperateState money,@HandledByNewWms bit,@State int,@OrderOut int)UPDATE [Orderxxx] SET [OperateState] = @OperateState,[HandledByNewWms] = @HandledByNewWms WHERE (([Orderxxx].[State] = @State) And ([Orderxxx].[OrderOut] = @OrderOut) And ([Orderxxx].[PricingExpressProductCode] IN ('UKNIR')))
inputbuf
unknown
frame procname=unknown line=1 sqlhandle=0x000000000000000000000000000000000000000000000000
UPDATE [Orderxxx] SET [OperateState] = @OperateState,[HandledByNewWms] = @HandledByNewWms WHERE (([Orderxxx].[State] = @State) And ([Orderxxx].[OrderOut] = @OrderOut) And ([Orderxxx].[PricingExpressProductCode] IN ('UKNIR')))
frame procname=adhoc line=1 stmtstart=134 sqlhandle=0x020000009d376d18a17e7ea51289d8caa2fb4de65c976389
executionStack
process id=process5c26988 taskpriority=0 logused=9892 waitresource=KEY: 33:72057597785604096 (70f5b089bb2b) waittime=4813 ownerId=31579268946 transactionname=user_transaction lasttranstarted=2015-09-24T10:27:01.357 XDES=0x98312f950 lockMode=U schedulerid=16 kpid=9184 status=suspended spid=454 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2015-09-24T10:27:01.490 lastbatchcompleted=2015-09-24T10:27:01.487 clientapp=.Net SqlClient Data Provider hostname=CK1-WIN-WEB02 hostpid=37992 loginname=ck1.biz isolationlevel=read committed (2) xactid=31579268946 currentdb=33 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
process-list
deadlock victim=process5c26988
deadlock-list
咋一看上面的錯(cuò)誤信息,可以發(fā)現(xiàn)兩條相同的語句造成的死鎖,但是這么短的語句不可能持有排他鎖太久
再仔細(xì)分析一下錯(cuò)誤日志,發(fā)現(xiàn)都死鎖在同一個(gè)非聚集索引上,再問了一下開發(fā),開發(fā)那邊說,這條語句是在一個(gè)大事務(wù)里面,這個(gè)事務(wù)會(huì)做7、8件事
索引屬性
還有索引里面的數(shù)據(jù),發(fā)現(xiàn)很多重復(fù)值
SQL語句是這樣的
(@OperateState money,@HandledByNewWms bit,@State int,@OrderOut int)
@HandledByNewWms=(1) @OperateState=($1.0000) @OrderOut=(4055484) @State=(3)
UPDATE [Orderxxx] SET [OperateState] = $1.0000,[HandledByNewWms] = 1
WHERE (([Orderxxx].[State] = 3) And ([Orderxxx].[OrderOut] = 4055484) And ([Orderxxx].[PricingExpressProductCode] IN ('UKRRM','UKRLE')))
下圖為語句生成的執(zhí)行計(jì)劃
當(dāng)時(shí)的情況是大量SQL語句被阻塞,而阻塞的語句正是下面這條語句
UPDATE [Orderxxx] SET [OperateState] = $1.0000,[HandledByNewWms] = 1
WHERE (([Orderxxx].[State] = 3) And ([Orderxxx].[OrderOut] = 4055484) And ([Orderxxx].[PricingExpressProductCode] IN ('UKRRM','UKRLE')))
解決方法
上面得出幾個(gè)癥狀
1、update語句是在一個(gè)大事務(wù)里面,事務(wù)太大導(dǎo)致其他session等待排他鎖的時(shí)間變長
2、大家都在使用同一個(gè)非聚集索引,并掃描PricingExpressProductCode字段
3、索引里的重復(fù)值很多
從上面的癥狀基本可以判斷,這個(gè)非聚集索引無啥用,可以禁用之
ALTER INDEX [IX_PricingExpressProductCode_State] ON [dbo].[Orderxxx] DISABLE
禁用之后,死鎖消失,問題解決,倉庫的怨氣也隨之消失
這一次排查過程時(shí)間有點(diǎn)長,但是很好定位,SQL Server錯(cuò)誤日志給出了足夠的信息定位死鎖問題,所以遇到問題的時(shí)候一定要分析清楚日志
您可能感興趣的文章:- mysql 數(shù)據(jù)庫死鎖原因及解決辦法
- Mysql 數(shù)據(jù)庫死鎖過程分析(select for update)
- 簡單說明Oracle數(shù)據(jù)庫中對死鎖的查詢及解決方法
- InnoDB數(shù)據(jù)庫死鎖問題處理
- Mybatis update數(shù)據(jù)庫死鎖之獲取數(shù)據(jù)庫連接池等待
- MySQL數(shù)據(jù)庫的一次死鎖實(shí)例分析
- 講解Oracle數(shù)據(jù)庫中結(jié)束死鎖進(jìn)程的一般方法
- 查詢Sqlserver數(shù)據(jù)庫死鎖的一個(gè)存儲(chǔ)過程分享
- MySQL數(shù)據(jù)庫之Purge死鎖問題解析
- 5分鐘快速了解數(shù)據(jù)庫死鎖產(chǎn)生的場景和解決方法