跟踪标记:1204/1222

功能及用途:

捕获SQL Server死锁信息,并自动存放到错误日志(ERRORLOG)中。

举例:

USE tempdb
GO CREATE TABLE t1(id int)
INSERT t1 SELECT 1 CREATE TABLE t2(id int)
INSERT t2 SELECT 1
GO --开启1204/1222跟踪标记(任何查询窗口)
DBCC TRACEON(1204,-1)
--DBCC TRACEON(1205,3605,-1)
--DBCC TRACEON(1206,3605,-1) --DBCC TRACEON(1222,-1)
GO --查询窗口1
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN
UPDATE t1 SET id = 0 WHERE id=1
WAITFOR DELAY '00:00:05'
UPDATE t2 SET id = 0 WHERE id=1
--COMMIT TRAN
GO --查询窗口2
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED --死锁优先级为low,将被选择作为牺牲品
SET DEADLOCK_PRIORITY LOW BEGIN TRAN
UPDATE t2 SET id = 0 WHERE id=1
UPDATE t1 SET id = 0 WHERE id=1
--COMMIT TRAN
GO --关闭1204/1222跟踪标记(任何查询窗口)
DBCC TRACEOFF(1204,-1)
--DBCC TRACEOFF(1205,3605,-1)
--DBCC TRACEOFF(1206,3605,-1) --DBCC TRACEOFF(1222,-1)
GO --查看错误日志里的死锁信息
exec xp_readerrorlog 0,1 DROP TABLE t1,t2

跟踪标记1204 在错误日志中记录的死锁信息

DBCC TRACEON 1204, server process ID (SPID) 56. This is an informational message only; no user action is required.
Deadlock encountered .... Printing deadlock information
Wait-for graph
NULL
Node:1 RID: 2:1:624:0 CleanCnt:2 Mode:X Flags: 0x3
Grant List 1:
Owner:0x00000034DF20D840 Mode: X Flg:0x40 Ref:0 Life:02000000 SPID:56 ECID:0 XactLockInfo: 0x00000034DDE34440
SPID: 56 ECID: 0 Statement Type: UPDATE Line #: 9
Input Buf: Language Event:
--查询窗口1
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN
UPDATE t1 SET id = 0 WHERE id=1
WAITFOR DELAY '00:00:05'
UPDATE t2 SET id = 0 WHERE id=1
--COMMIT TRAN Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x00000034E122B2B8 Mode: U SPID:58 BatchID:0 ECID:0 TaskProxy:(0x00000034BB418870) Value:0xdf20d040 Cost:(5/224)
NULL
Node:2 RID: 2:3:944:0 CleanCnt:2 Mode:X Flags: 0x3
Grant List 1:
Owner:0x00000034DF20D980 Mode: X Flg:0x40 Ref:0 Life:02000000 SPID:58 ECID:0 XactLockInfo: 0x00000034E122B2F0
SPID: 58 ECID: 0 Statement Type: UPDATE Line #: 8
Input Buf: Language Event:
--查询窗口2
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SET DEADLOCK_PRIORITY LOW BEGIN TRAN
UPDATE t2 SET id = 0 WHERE id=1
UPDATE t1 SET id = 0 WHERE id=1
--COMMIT TRAN
Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x00000034DDE34408 Mode: U SPID:56 BatchID:0 ECID:0 TaskProxy:(0x00000034BB1DC870) Value:0xdf20ec00 Cost:(0/224)
NULL
Victim Resource Owner:
ResType:LockOwner Stype:'OR'Xdes:0x00000034E122B2B8 Mode: U SPID:58 BatchID:0 ECID:0 TaskProxy:(0x00000034BB418870) Value:0xdf20d040 Cost:(5/224)
DBCC TRACEOFF 1204, server process ID (SPID) 56. This is an informational message only; no user action is required.

跟踪标记1222 在错误日志中记录的死锁信息

DBCC TRACEON 1222, server process ID (SPID) 56. This is an informational message only; no user action is required.
deadlock-list
deadlock victim=process34d71fc4e8
process-list
process id=process34d71fc4e8 taskpriority=5 logused=224 waitresource=RID: 2:1:624:0 waittime=3012 ownerId=1281992 transactionname=user_transaction lasttranstarted=2016-10-12T10:11:24.513 XDES=0x34dde34408 lockMode=U schedulerid=2 kpid=5736 status=suspended spid=58 sbid=0 ecid=0 priority=-5 trancount=2 lastbatchstarted=2016-10-12T10:11:24.513 lastbatchcompleted=2016-10-12T10:11:12.760 lastattention=2016-10-12T10:10:46.323 clientapp=Microsoft SQL Server Management Studio - Query hostname=CHRZHANG hostpid=6924 loginname=PCLC0\chrzhang isolationlevel=read committed (2) xactid=1281992 currentdb=2 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
executionStack
frame procname=adhoc line=9 stmtstart=38 stmtend=116 sqlhandle=0x020000006f1dc606af4ee82be297cae142d4eca1b1b26d840000000000000000000000000000000000000000
unknown
frame procname=adhoc line=9 stmtstart=324 stmtend=384 sqlhandle=0x02000000c9c0f33adb0fe790eb6b0e4c7175f9f4b5931a970000000000000000000000000000000000000000
unknown
inputbuf
--查询窗口2
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SET DEADLOCK_PRIORITY LOW
BEGIN TRAN
UPDATE t2 SET id = 0 WHERE id=1
UPDATE t1 SET id = 0 WHERE id=1
--COMMIT TRAN
process id=process34ddc6a4e8 taskpriority=0 logused=224 waitresource=RID: 2:3:944:0 waittime=1071 ownerId=1281979 transactionname=user_transaction lasttranstarted=2016-10-12T10:11:21.440 XDES=0x34e122b2b8 lockMode=U schedulerid=2 kpid=968 status=suspended spid=56 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2016-10-12T10:11:21.440 lastbatchcompleted=2016-10-12T10:11:04.287 lastattention=1900-01-01T00:00:00.287 clientapp=Microsoft SQL Server Management Studio - Query hostname=CHRZHANG hostpid=6924 loginname=PCLC0\chrzhang isolationlevel=read committed (2) xactid=1281979 currentdb=2 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
executionStack
frame procname=adhoc line=8 stmtstart=38 stmtend=116 sqlhandle=0x0200000083cd4832d993ca3b2dcaae9f9cc70a25e718dbe90000000000000000000000000000000000000000
unknown
frame procname=adhoc line=8 stmtstart=326 stmtend=386 sqlhandle=0x02000000ff6a84274c6888c86c481ae84300231e8f5fb8000000000000000000000000000000000000000000
unknown
inputbuf
--查询窗口1
SET LOCK_TIMEOUT -1
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
UPDATE t1 SET id = 0 WHERE id=1
WAITFOR DELAY '00:00:05'
UPDATE t2 SET id = 0 WHERE id=1
--COMMIT TRAN
resource-list
ridlock fileid=1 pageid=624 dbid=2 objectname=tempdb.dbo.t1 id=lock34d8ea3200 mode=X associatedObjectId=2017612634171244544
owner-list
owner id=process34ddc6a4e8 mode=X
waiter-list
waiter id=process34d71fc4e8 mode=U requestType=wait
ridlock fileid=3 pageid=944 dbid=2 objectname=tempdb.dbo.t2 id=lock34d8ea3780 mode=X associatedObjectId=2089670228250132480
owner-list
owner id=process34d71fc4e8 mode=X
waiter-list
waiter id=process34ddc6a4e8 mode=U requestType=wait
DBCC TRACEOFF 1222, server process ID (SPID) 56. This is an informational message only; no user action is required.

小结:

(1) 不需要加跟踪标记3605,跟踪标记1204,1222捕获的死锁信息便会被写到错误日志;

(2) 跟踪标记 1222以XML样式返回死锁信息,相比跟踪标记1204,返回的信息也更为丰富;

(3) 无文档记载的跟踪标记1205,1206,据说可以用来丰富1204捕获死锁的信息,在SQL Server 2016下简单测试同时开启1204,1205,1206,和单独开启1204并没发现有什么不同,个人猜测是随着版本更替,跟踪标记捕获的死锁信息已经被整合,直接用1204或1222即可;

(4) 从SQL Server 2008开始,引进了扩展事件(Extended Events),也可以用来捕获死锁信息。

参考:

Tracing a SQL Server Deadlock

https://www.mssqltips.com/sqlservertutorial/252/tracing-a-sql-server-deadlock/

Trace Flags - SQL Server Wiki - SQL Server - Toad World

https://www.toadworld.com/platforms/sql-server/w/wiki/9790.trace-flags

Using SQL Server 2008 Extended Events

https://technet.microsoft.com/en-us/library/dd822788%28v=sql.100%29.aspx?f=255&MSPPError=-2147217396

3. 跟踪标记 (Trace Flag) 1204, 1222 抓取死锁信息的更多相关文章

  1. 0. 跟踪标记 (Trace Flag) 简介

    一. 什么是跟踪标记 SQL Server 跟踪标记(Trace Flag),像是一个开关,可用来自定义SQL Server的某种行为或特性,在性能诊断,系统调试等方面较为常用.比如:开启1204或1 ...

  2. 2. 跟踪标记 (Trace Flag) 3604, 3605 输出DBCC命令结果

    跟踪标记:3604 功能: 输出DBCC命令返回结果到查询窗口(通常是SSMS窗口),类似print命令的显示效果: 用途: 常用于获取DBCC IND, DBCC PAGE命令的输出结果,因为这2个 ...

  3. 4. 跟踪标记 (Trace Flag) 610 对索引组织表(IOT)最小化日志

    跟踪标记:610 功能: 用批量导入操作(Bulk Import Operations)加载数据时,对于索引组织表(即有聚集索引的表) 最小化日志: 上图为simple/bulk-logged恢复模式 ...

  4. 5. 跟踪标记 (Trace Flag) 834, 845 对内存页行为的影响

    跟踪标记:834 功能: 在64位的windows环境下,为SQL Server开启这个跟踪标记,那么SQL Server 会使用大页(Large pages)为内存缓冲区(buffer pool)分 ...

  5. 1. 跟踪标记 (Trace Flag) 1117, 1118 文件增长及空间分配方式

    跟踪标记:1117 功能: 默认,同一个文件组下的多个文件,如果某个文件没有可用空间,且设置了自动增长,则该文件自动增长,其他文件大小保持不变: 开启后,同一文件组下的多个文件,如果某个文件没有可用空 ...

  6. (5.2.2)配置服务器参数——dbcc跟踪标记(trace)

    关键字:跟踪标记,跟踪 [1]常规dbcc命令 dbcc help('?') --查看dbcc 所有命令,常规下只有32个常用的dbcc TRACEON(2588) --指定了2588标记的话,你就可 ...

  7. PHP快速抓取快递信息

    <?php header("Content-type:text/html;charset=utf-8"); /** * Express.class.php 快递查询类 * @ ...

  8. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(3): 抓取amazon.com价格

    通过上一篇随笔的处理,我们已经拿到了书的书名和ISBN码.(网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息 ...

  9. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息及ISBN码

    这一篇首先从allitebooks.com里抓取书籍列表的书籍信息和每本书对应的ISBN码. 一.分析需求和网站结构 allitebooks.com这个网站的结构很简单,分页+书籍列表+书籍详情页. ...

随机推荐

  1. wap尝试调取app(网易新闻为例)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 开发小技巧1——Logger

    开发小技巧1——Logger 在项目中加入静态Logger类,用于捕获并记录程序的进度.错误信息:   public static class Logger { public static void ...

  3. Ionic开发之如何修改Cordova插件

    由于大多数的Cordova plugin都是国外的,一些提示信息通常都是英文, 这就需要我们自己修改成中文信息. 除此之外,还有可能插件的样式,皮肤等和整体项目的风格不一致,这个时候也是要修改的. 可 ...

  4. [转] 如何选择正确的Hadoop版本

    Gartner:如何选择正确的Hadoop版本 这份报告的全名是<How to Choose the Right Apache Hadoop Distribution>.主要介绍了企业如何 ...

  5. ASPxPopupControl出现前一次弹框页面解决方法

    设置关闭事件 <ClientSideEvents  CloseUp="CloseUp" /> function CloseUp(s, e) {    s.SetCont ...

  6. 使用Python制作一个简单的刷博器

    呵呵,不得不佩服Python的强大,寥寥几句代码就能做一个简单的刷博器. import webbrowser as web import time import os count=0 while co ...

  7. dynamic结合匿名类型 匿名对象传参

    首先说明下,我一般很少用dynamic关键字(类)的,因为毕竟是由反射实现的,所以对于性能方面还是吃亏不少(注:由于心里没底,查了一些资料得知,dynamic实质上好像不是由反射实现的,其性能也比直接 ...

  8. 关于PLSQL启动用时较长的问题解决

    问题: 打开登陆界面缓慢. 解决: 1.删除控制面板中的打印机 2.将打印机改为手动并停止启动状态 .

  9. 【转】关于JTA,XA,ACID

    对于我们这种初学者,可能会使用spring带给我们的@Transactional,可能了解JTA,可能会使用jotm.atomikos,又会遇到一些名词XA,支持XA的数据库驱动等等诸多问题,然后就会 ...

  10. Spring Boot的快速创建

    一.利用向导快速搭建Spring Boot应用 创建一个controller package com.hoje.springboot.Controller; import org.springfram ...