如何排查SQL死锁的错误?
2010-04-06 11:40
337 查看
什么是死锁?
==============
A deadlock occurs when two system server process IDs (SPIDs) are waiting for a resource and neither process can advance because the other process is preventing it from getting the resource. For example, Andrew holds a lock on table A and requests a lock on table B; Lindsay holds a lock on table B and requests a lock on table A.
什么东西可以引发死锁?
==============
Locks Worker threads Memory Parallel query execution-related resources Multiple Active Result Sets (MARS) resources.
如何排查死锁?
==============
1. 开启死锁日志输出(deadlock trace)
Trace flag 1204 reports deadlock information formatted by each node involved in the deadlock.
Trace flag 1222 formats deadlock information, first by processes and then by resources.
开启了上面的选项之后, SQL会输出死锁的细节信息到SQL Error Log中(默认位置Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG and ERRORLOG.n)
2. 开启SQL Profiler.
Start SQL profiler
On the Trace Properties dialog box, on the General tab, check Save to file and specify a path to save the trace
Click the Events tab, only add Locks\Lock:deadlock and Locks\Lock:deadlock chain
Click the Data columns tab, add DatabaseID, IndexID, ObjectID
可以通过下面的语句把DatabaseID和ObjectID换成DatabaseName和ObjectName
3. 使用下面的查询语句来检查那个进程被锁住了.
参考资料:
SQL: How to debug SQL deadlocks
http://www.dalun.com/blogs/10.13.2006.htm
SQL Server technical bulletin - How to resolve a deadlock
http://support.microsoft.com/?kbid=832524
Detecting and Ending Deadlocks
http://msdn.microsoft.com/en-us/library/ms178104.aspx
==============
A deadlock occurs when two system server process IDs (SPIDs) are waiting for a resource and neither process can advance because the other process is preventing it from getting the resource. For example, Andrew holds a lock on table A and requests a lock on table B; Lindsay holds a lock on table B and requests a lock on table A.
什么东西可以引发死锁?
==============
Locks Worker threads Memory Parallel query execution-related resources Multiple Active Result Sets (MARS) resources.
如何排查死锁?
==============
1. 开启死锁日志输出(deadlock trace)
DBCC TRACEON(1204,1222)
Trace flag 1204 reports deadlock information formatted by each node involved in the deadlock.
Trace flag 1222 formats deadlock information, first by processes and then by resources.
开启了上面的选项之后, SQL会输出死锁的细节信息到SQL Error Log中(默认位置Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG and ERRORLOG.n)
2. 开启SQL Profiler.
Start SQL profiler
On the Trace Properties dialog box, on the General tab, check Save to file and specify a path to save the trace
Click the Events tab, only add Locks\Lock:deadlock and Locks\Lock:deadlock chain
Click the Data columns tab, add DatabaseID, IndexID, ObjectID
可以通过下面的语句把DatabaseID和ObjectID换成DatabaseName和ObjectName
SELECT database_name(DatabaseID) SELECT object_name(ObjectID)
3. 使用下面的查询语句来检查那个进程被锁住了.
SELECT * FROM sys.sysprocesses WHERE blocked <> 0 从Blocked列中得到SPID DBCC inputbuffer (SPID) sp_who2 sp_lock2
参考资料:
SQL: How to debug SQL deadlocks
http://www.dalun.com/blogs/10.13.2006.htm
SQL Server technical bulletin - How to resolve a deadlock
http://support.microsoft.com/?kbid=832524
Detecting and Ending Deadlocks
http://msdn.microsoft.com/en-us/library/ms178104.aspx
相关文章推荐
- 如何排查SQL死锁的错误?
- 如何排查SQL死锁的错误?
- 如何使用SQLDiag工具来追踪死锁错误 -ZT
- 使用Oracle SQL Developer连接数据库时,提示“执行请求的操作时遇到错误:IO 错误: The Network Adapter could not establish”,不知如何处理
- 错误21002:[SQL-DMO]用户"xxx"已经存在,如何解决
- CSS如何排查错误?从哪入手
- SQl如何避免除零错误 小结
- 如何使错误日志更加方便排查问题
- PL/SQL Developer 使用错误的tnsnames.ora,如何修改?
- VS2010 + Sql Server Express 2008 R2 如何安装(兼谈错误:The database 'XXX' cannot be opened because it is version 661. This server support
- SQL Server里如何处理死锁 (转)
- GS凭证接口常用错误排查sql
- 卸载postpersql后重装不了,出现错误,如何解决
- JVM的线程状态及如何排查死锁原因
- 如何将sql 执行的错误消息 记录到本地文件中
- 如何排查EXC_BAD_ACCESS错误
- SQL2005 : 如何在SQL Server Profiler (事件查看器)中 跟踪查看死锁恢复
- 如何解决sql server2005的“不允许远程连接”错误.(how to resolve the error: "SQL Server does not allow remote connections" for SQL Server2005)
- SQL错误排查:子查询返回的值不止一个。子查询用作表达式时,这种情况是不允许的...
- 如何查看SQL Profiler? 如何查看SQL死锁?