Turn from: 19498327 How do I capture and record deadlocks that occur in SQL Server?
How SQL Server makes the error log record deadlock
February 19, 2014 18:25:55Hits: 1313
We know that you can use the Profiler tool that comes with SQL Server to track the deadlock information. But this way there is a big one, that is, the consumption is very large. According to a major foreign God test, Profiler can even account for the total server bandwidth of 35%, so in a busy system, using Profiler is obviously not a good idea, below I introduce two ways to consume less. The second is the least expensive and can be used in the busiest system. The first is the most flexible and can meet a wide range of applications.
Method One: Leverage SQL Server Agent (alert+job)
The steps are as follows:
1. First use the following command to enable the related trace flag.
[C-sharp] View plaincopy
- DBCC TRACEON (3605,1204,1222,-1)
Description
3605 outputs the results of the DBCC to the error log.
1204 returns the resource and type of the lock that participates in the deadlock, and the current command that is affected.
1222 returns the resources and types of locks that participate in deadlocks, and the affected current commands that use XML formats that do not conform to any XSD schema (more than 1204, SQL 2005 and more are available).
1 opens the specified trace flag in a global fashion.
The trace flag scope above is global, which will continue to work during SQL Server operation until SQL Server restarts.
If you want to ensure that SQL Server automatically turns on these flags after a restart, you can use the/T startup option in the SQL Server service startup option to specify that the trace flag is set to on during startup. (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, you can already see it in the error log, but it is not intuitive (mixed with other information). (SQL Server log, management---SQL Server instance, SSMS)
2. Build the table and store the deadlock record
[C-sharp] View plaincopy
- Use [Cole]--cole is my sample database, you can modify it according to the actual situation.
- GO
- CREATE TABLE Deadlocklog (
- ID int IDENTITY (1, 1) not NULL,
- Logdate DATETIME,
- ProcessInfo VARCHAR (10),
- ErrorText VARCHAR (MAX)
- )
- GO
3. Create a job
Create a new job (assuming the name is Deadlockjob), create a new step in step, write a step name, and the database is "Cole" (see 2. Build table), enter the following statement in the command field:
[C-sharp] View plaincopy
- --Create a new temporary table
- 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 (ten), C varchar (MAX))
- --Inserting the current log record into a temporary table
- INSERT into #ErrorLog EXEC master.dbo.sp_readerrorlog
- --inserting deadlock information into the user table
- Insert Deadlocklog
- Select a, B, c
- From #ErrorLog
- Where ID >= (select MAX (ID) from #ErrorLog where C like '%deadlock encountered% ')
- DROP TABLE #ErrorLog
4. Create a new alert
In the new alert form, on the General tab, make the following settings:
Name: Can be according to the actual self-naming, here I use Deadlockalert
Type: Select "SQL Server performance condition alert"
Object: Sqlserver:locks
Counter: Number of Deadlocks/sec
Example: _total
The counter triggers an alert when the following conditions are met: higher than
Value: 0
After Setup is complete, it should look like the following:
In the Response tab, select Execute Job and select the job we created in step 3 (that is, deadlockjob)
So far, we've done all the steps, so you can check the Deadlocklog table at any time to show the deadlock information.
Method Two: Take advantage of server-side tracking.
The specific implementation steps are as follows:
1. Write the following script and execute
[C-sharp] View plaincopy
- --Defining parameters
- DECLARE @rc int
- DECLARE @TraceID int
- DECLARE @maxfilesize bigint
- Set @maxfilesize = 5
- --Initialization tracking
- exec @rc = sp_trace_create @TraceID output, 0, N' E:/dblog/deadlockdetect ', @maxfilesize, NULL
- --The E:/dblog/deadlockdetect here is the file name (which can be modified by itself), and SQL automatically appends the. trc extension
- if (@rc! = 0) goto error
- --Set trace events
- DECLARE @on bit
- Set @on = 1
- --148 in the following statement refers to the Locks:deadlock graph event (see sys.trace_events) and 12 to the SPID column (see SYS.TRACE_COLUMNS)
- exec sp_trace_setevent @TraceID, 148, @on
- exec sp_trace_setevent @TraceID, 148, one, @on
- exec sp_trace_setevent @TraceID, 148, 4, @on
- exec sp_trace_setevent @TraceID, 148, @on
- exec sp_trace_setevent @TraceID, 148, @on
- exec sp_trace_setevent @TraceID, 148, @on
- exec sp_trace_setevent @TraceID, 148, 1, @on
- --Start tracking
- exec sp_trace_setstatus @TraceID, 1
- --record the Tracking ID for later use
- Select traceid = @TraceID
- Goto Finish
- Error
- Select [Email protected]
- Finish
- Go
Once the above statement is run, a record is automatically inserted into the file e:/dblog/deadlockdetect.trc whenever a deadlock event occurs in SQL Server.
2. Pausing and stopping server-side tracking
To pause the server-side trace above, run the following statement:
EXEC sp_trace_setstatus 1, 0--The first parameter represents Traceid, which is the output parameter in step 1. The second parameter indicates that the state is changed to 0, which is a pause
To stop the above server-side trace, run the following statement:
EXEC sp_trace_setstatus 1, 2--the first parameter represents Traceid, which is the output parameter in step 1. The second parameter indicates that the state is changed to 2, that is, stop
3. View the contents of the trace file
There are two ways to view the trace file (E:/DBLOG/DEADLOCKDETECT.TRC) generated above:
1). Execute T-SQL command
[C-sharp] View plaincopy
- SELECT * from fn_trace_gettable (' e:/dblog/deadlockdetect.trc ', 1)
The TextData column in the result returns the details of the deadlock in the form of XML.
2). Open in SQL Server Profiler.
Go to Profiler, open trace file, select E:/dblog/deadlockdetect.trc, and you can see the deadlock information in graphical form.
(4.7) How do I capture and record deadlocks that occur in SQL Server?