Capture and record the deadlock information of SQL


We know that you can use the Profiler tool that comes with SQL Server to track deadlock information. But this method has a big advantage, that is, it consumes a lot. According to a foreign god test, the profiler can even account for 35% of the total bandwidth of the server, so in a busy system, using the profiler is obviously not a good idea. Below I introduce two methods that consume less. The second one has the least consumption and can also be used in the busiest system. The first is the most flexible and can meet a variety of applications.

Method 1: The

specific steps of using SQL Server Agent (Alert + Job) are as follows:

1. First use the following command to enable the relevant trace flag.

SQL code
 
DBCC TRACEON (3605,1204,1222,-1)  


Description:
3605 outputs the DBCC results to the error log.
1204 Returns the resources and types of locks participating in the deadlock, as well as the current commands affected.
1222 Returns the resources and types of locks participating in the deadlock, as well as the affected current commands in XML format that does not conform to any XSD schema (further than 1204, available for SQL 2005 and above).
-1 Turn on the specified trace mark globally.

The scope of the above trace flags is global, that is, SQL Server will continue to play a role during the operation of SQL Server until SQL Server restarts.

If you want to ensure that SQL Server automatically turns on these flags after restarting, you can use the / T startup option in the SQL Server service startup options to specify that the trace flag is set to on during startup. (Located in SQL Server Configuration Manager-> SQL Server Service-> SQL Server-> Properties-> Advanced-> Startup Parameters)

After running the above statement, when a deadlock occurs in SQL Server, it can already be seen in the error log Arrived, but not intuitive enough (mixed with other information). (SSMS-> SQL Server instance-> Management-> SQL Server log)

2. Create a table to store the deadlock record

SQL code
 





USE [Cole]  --Cole是我的示例数据库,你可以根据实际情况修改。  
GO  
CREATE  TABLE  DeadLockLog (  
id  int  IDENTITY (1, 1)  NOT  NULL ,   
LogDate DATETIME,   
ProcessInfo  VARCHAR (10),   
ErrorText  VARCHAR ( MAX )  
GO  

 

3. Create a JOB to

create a new JOB (assuming the name is DeadLockJob), create a new step in the "step", write a step name, the database is "Cole" (see 2. Create a table), enter the following in the "command" field Statement:

SQL code

















--新建临时表  
IF OBJECT_ID( 'tempdb.dbo.#ErrorLog' IS  Not  Null  
DROP  TABLE  #ErrorLog  
 
CREATE  TABLE  #ErrorLog (Id  int  IDENTITY (1, 1)  NOT  NULL , a DATETIME, b  VARCHAR (10), c  VARCHAR ( MAX ))  
 
--将当前日志记录插入临时表  
INSERT  INTO  #ErrorLog  EXEC  master.dbo.sp_readerrorlog  
 
--将死锁信息插入用户表  
insert  DeadLockLog  
select  a, b, c   
from  #ErrorLog   
where  id >= ( select  MAX (id)  from  #ErrorLog  WHERE  Like  '%Deadlock encountered%'
  
DROP  TABLE  #ErrorLog  
  



4. New alarm

In the "General" tab of the "New alarm" form, make the following settings:

Name: You can name it according to the actual situation, here I use DeadLockAlert
type: select "SQL Server performance condition alarm"
object: SQLServer: Locks
Counter: Number of Deadlocks / sec
Example: _Total
counter triggers an alarm when the following conditions are met: Above
value: 0

After the setting is completed, it should be as shown in the following figure:

In the "Response" tab, select "Execute Job" and select the job we created in Step 3 (ie DeadlockJob).

Here, we have completed all the steps. In the future, you can query the DeadLockLog table at any time to display Deadlock information.

Method 2: Use server-side tracking.

The specific implementation steps are as follows:

1. Write the following script and execute

SQL code



































-- 定义参数  
declare  @rc  int  
declare  @TraceID  int  
declare  @maxfilesize  bigint  
set  @maxfilesize = 5   
 
-- 初始化跟踪  
exec  @rc = sp_trace_create @TraceID  output , 0, N 'e:\DbLog\deadlockdetect' , @maxfilesize,  NULL   
--此处的e:\dblog\deadlockdetect是文件名(可自行修改),SQL会自动在后面加上.trc的扩展名  
if (@rc != 0)  goto  error  
 
-- 设置跟踪事件  
declare  @ on  bit  
set  @ on  = 1  
--下述语句中的148指的是locks:deadlock graph事件(参见sys.trace_events),12指的是spid列(参见sys.trace_columns)  
exec  sp_trace_setevent @TraceID, 148, 12, @ on    
exec  sp_trace_setevent @TraceID, 148, 11, @ on  
exec  sp_trace_setevent @TraceID, 148, 4, @ on  
exec  sp_trace_setevent @TraceID, 148, 14, @ on  
exec  sp_trace_setevent @TraceID, 148, 26, @ on  
exec  sp_trace_setevent @TraceID, 148, 64, @ on  
exec  sp_trace_setevent @TraceID, 148, 1, @ on  
 
-- 启动跟踪  
exec  sp_trace_setstatus @TraceID, 1  
 
-- 记录下跟踪ID,以备后面使用  
select  TraceID = @TraceID  
goto  finish  
 
error:   
select  ErrorCode=@rc  
 
finish:   
go  
  


After running the above statement, whenever a deadlock event occurs in SQL Server, a record is automatically inserted into the file e: \ DbLog \ deadlockdetect.trc.

2. Pause and stop server-side tracking

If you want to pause the server-side tracking above, you can run the following statement:

SQL code

exec  sp_trace_setstatus 1, 0  --第一个参数表示TraceID,即步骤1中的输出参数。第二个参数表示将状态改为0,即暂停



If you want to stop the above server-side tracking, you can run the following statement:

SQL code

exec  sp_trace_setstatus 1, 2  --第一个参数表示TraceID,即步骤1中的输出参数。第二个参数表示将状态改为2,即停止



3. View the contents of the trace file

For the trace file (e: \ DbLog \ deadlockdetect.trc) generated above, you can view it in two ways:

1). Execute the t-sql command

SQL code

select  from  fn_trace_gettable( 'e:\DbLog\deadlockdetect.trc' ,1)  


 
The TextData column in the result returns detailed information about the deadlock in the form of XML.

2). Open in SQL Server Profiler.

Go to Profiler-> Open trace file-> select e: \ DbLog \ deadlockdetect.trc in turn, you can see the deadlock information displayed in graphical form.


Original text from: http://bbs.csdn.net/topics/350234619


Published 22 original articles · praised 7 · 100,000+ views

Guess you like

Origin blog.csdn.net/qyx0714/article/details/63678740