A Deadlock Occurence and Resolution
转贴一篇SQL的好文章 :-)
http://www.sqlservercentral.com/articles/deadlock/64315/
A Deadlock Occurence and Resolution
By Praveen Voleti, 2009/02/03
Introduction
An upgrade was performed at my company recently to the application code on the SQL Server, which involved basically the addition of columns to few tables. After the upgrade, the customer started facing severe deadlocks. Around 8 out of 10 transactions were getting deadlocked and were failing.
Steps Taken in resolving the deadlocks:
I will walk through the steps we took to determine what was causing the deadlock and resolve the issue.
1. First I enabled the trace flag 1204 on the server to capture the deadlock details in the error log with the command listed below:
DBCC traceon(1204,-1)
Next I confirmed that the trace was enabled with the below command:
DBCC Tracestatus(-1)
The output shown below indicated that the trace flag 1204 was enabled.
TraceFlag Status Global Session --------- ------ ------ ------- 1204 1 1 0
2. I then gathered the deadlock details from the SQL error log with the command 'xp_readerrorlog'. One instance of deadlock occurred was given below:
Deadlock encountered .... Printing deadlock information Wait-for graph Node:1 PAG: 9:1:18061 CleanCnt:2 Mode: SIU Flags: 0x2 Grant List 1:: Grant List 2:: Owner:0x27c007e0 Mode: S Flg:0x0 Ref:0 Life:00000001 SPID:84 ECID:0 SPID: 84 ECID: 0 Statement Type: UPDATE Line #: 11 Input Buf: RPC Event: dbo.Example_Stored_proc Requested By: 0 ResType:LockOwner Stype:'OR' Mode: IX SPID:78 ECID:0 Ec:(0x44AA55F0) Value:0x3affcd00 Cost:(0/0) Node:2 PAG: 9:1:18134 CleanCnt:2 Mode: SIU Flags: 0x2 Grant List 1:: Owner:0x28e6f060 Mode: S Flg:0x0 Ref:0 Life:00000001 SPID:78 ECID:0 SPID: 78 ECID: 0 Statement Type: UPDATE Line #: 11 Input Buf: RPC Event: dbo. Example_Stored_proc Grant List 2:: Requested By: ResType:LockOwner Stype:'OR' Mode: IX SPID:84 ECID:0 Ec:(0x239955F0) Value:0x3affc940 Cost:(0/0) Victim Resource Owner: ResType:LockOwner Stype:'OR' Mode: IX SPID:84 ECID:0 Ec:(0x239955F0) Value:0x3affc940 Cost:(0/0)
3. From the captured details, it was found that the deadlocks were occurring due to UPDATE statements in the stored procedure 'Example_stored_proc' while trying to get an Intent exclusive lock on the pages.
4. My next step was to read the contents of the stored procedure 'Example_stored_proc' through sp_helptext.
5. The procedure was calling around 5 procedures and each of those were calling 4 more procedures.
6. As going through all the procedures to identify the deadlock statements was difficult, I was able to get to the correct update statement through the Page numbers in the deadlock details captured earlier. The deadlock details captured had the page numbers that are involved in the deadlock as shown below:
Node:1 PAG: 9:1:18061 CleanCnt:2 Mode: SIU Flags: 0x2
7. I gathered the Page numbers that were being deadlocked and used the undocumented DBCC command 'DBCC Page' to get the table name associated with the page. The details of the command syntax are in this link:http://support.microsoft.com/kb/83065
Though the article refers to earlier SQL versions, the command is still valid in SQL Server 2000 and provides the page information.
I then used this command to get the page details:
DBCC page(9,1,18061,0)
The parameters were taken from the deadlock details shown above. The output obtained was pasted below. It contains the object name associated with that page. Under the PAGE HEADER, the value m_objId gives the table name associated with the deadlock:
PAGE: (1:18061) --------------- BUFFER: ------- BUF @0x01665900 --------------- bpage = 0x1DF58000 bhash = 0x00000000 bpageno = (1:18061) bdbid = 9 breferences = 1 bstat = 0xb bspin = 0 bnext = 0x00000000 PAGE HEADER: ------------ Page @0x1DF58000 ---------------- m_pageId = (1:18061) m_headerVersion = 1 m_type = 1 m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x8000 m_objId = 1013578649 m_indexId = 0 m_prevPage = (0:0) m_nextPage = (0:0) pminlen = 52 m_slotCnt = 82 m_freeCnt = 3075 m_freeData = 5009 m_reservedCnt = 0 m_lsn = (2689:87968:2) m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 m_tornBits = 0 .................
Once the object id was obtained, table name was found out with the command:
Use DBNAME Select object_name(OBJECT_ID)
8. I then reviewed the code of the dependent stored procedures on the table and found one update procedure among the list.
9. The code of the update procedure was similar to that shown below:
Update TABLE Set Column1 =@C1, Column2=@C2, Column2=@C3, Where ID = @id
10. I then verified the indexes on the table and found that the table did not have an index on the ID column. This was forcing the update to perform a table scan causing it to take an INTENT exclusive lock on all the pages thereby causing the deadlock in the process.
11. As there was only one non-clustered index existing on that table, I created a clustered index on the table on the ID column with this command:
IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_Clustered_index_column') CREATE CLUSTERED INDEX IX_Clustered_index_column ON dbo.TABLE(ID) ON [PRIMARY]
12. After the index creation, the intent exclusive lock was not requested on all the pages of the update statement and as a result the deadlocks got resolved.
Conclusion
On further discussion with the application team about the reason for the deadlock occurrence after the upgrade performed by them, I came to know the following:
- Before the upgrade, for every insertion, one record was inserted or updated in the table identified in the deadlock
- As part of the upgrade, the application team performed modifications to the stored procedure code such that every insertion may cause up to 10 updates in the table.
This confirmed the reason why the deadlocks started occurring after the upgrade. As the table access increased heavily due to table scans, transactions took a longer time for each update thereby causing the deadlock scenario.
A Deadlock Occurence and Resolution相关推荐
- 2023年4月国产数据库大事记-墨天轮
本文为墨天轮社区整理的2023年4月国产数据库大事件和重要产品发布消息. 目录 4月国产数据库大事记 TOP10 4月国产数据库大事记(时间线) 产品/版本发布 兼容认证 代表厂商大事记 厂商活动 相 ...
- Go 知识点(02)— channel 使用不当导致的 deadlock
运行下面这段代码输出的结果是什么? package mainimport ("fmt" )func main() {c := make(chan string) // 创建一个无缓 ...
- fatal error all goroutines are asleep - deadlock!
先看下面代码 package mainimport ("fmt""time" )func main() {ch1 := make(chan int)go fun ...
- Google Pixel 超分辨率--Super Resolution Zoom
Google Pixel 超分辨率–Super Resolution Zoom Google 的Super Res Zoom技术,主要用于在zoom时增强画面细节以及提升在夜景下的效果. 文章的主要贡 ...
- R语言ggplot2可视化保存高分辨率的图片(high resolution)实战
R语言ggplot2可视化保存高分辨率的图片(high resolution)实战 目录 R语言ggplot2可视化保存高分辨率的图片(high resolution)实战
- 实体识别+entity resolution
实体识别+entity resolution 定义:不同的数据提供方对同一个事物即实体 (Entity)可能会有不同的描述 (这里的描述包括数据格式 .表示方法等) ,每一个对实体的描述称为该实体的一 ...
- Opencv cv2 Overload resolution failed
报错: cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'circle' > Overload resolu ...
- Temporary failure in name resolution
最近在使用yum方式安装perl-DBD-MySQL时碰到了Temporary failure in name resolution,Trying other mirror.即命名解析失败,尝 ...
- python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法>
python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法> 参考文章: (1)python---方法解析顺序MRO(Method ...
最新文章
- 使用Python,OpenCV构建透明的叠加层
- mysql监控内存cpu使用率_监控 cpu 内存 网卡的使用情况的一个命令 比较实用
- SCCM2012R2七:msi软件分发和卸载
- chrome怎么隐藏浏览器_如何使用Google Chrome的隐藏阅读器模式
- 3、play中的模板引擎
- 如何设置Hyper-V的虚拟机快捷方式
- java实现系列化的jdk_Java反序列化之与JDK版本无关的利用链挖掘
- 自走棋电脑版_手游版《自走棋》上线试玩
- csdn博客图片复制不过来怎么办?如何转载?
- FL Studio 12是什么软件?Mac音乐制作软件推荐FL Studio 12 for Mac!
- 贪心算法 --- 例题2.哈夫曼编码问题
- ubuntu mysql卸载教程_ubuntu下安装mysql及卸载mysql详细教程/方法
- 使用Python快速获取哥白尼数据开放访问中心购物车里的数据下载链接
- Windows server 2008 密码策略不能修改的解决办法
- 串口之DCB结构体详解
- mysql五种查询句子和作用_MYSQL
- 微软极品工具箱-Sysinternals Suite
- 东莞横沥塑料模具设计与制造的主要工艺流程
- 客户关系管理系统所遇问题以及需要注意点汇总
- friendly发音_“friendly”的反义词是什么? friendly 用中文怎么读