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.
|
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
|
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:
|
--新建临时表
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
c
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
|
-- 定义参数
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:
|
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:
|
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
|
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