Trace Flag

Trace Flag能够影响Sql Server的行为,主要用于diagnose performance issue,官方解释是:

Trace flags are used to temporarily set specific server characteristics or to switch off a particular behavior. For example, if trace flag 3205 is set when an instance of SQL Server starts, hardware compression for tape drivers is disabled. Trace flags are frequently used to diagnose performance issues or to debug stored procedures or complex computer systems.

1,Trace Scope

每个Trace Flag影响的范围是不同的,有的Trace Flag只能作用于当前的connection,有的Trace Flag能够影响SQL Server Instance的所有Connection。

In SQL Server, there are two types of trace flags: session and global. Session trace flags are active for a connection and are visible only to that connection. Global trace flags are set at the server level and are visible to every connection on the server. Some flags can only be enabled as global, and some can be enabled at either global or session scope.

The following rules apply:

  • A global trace flag must be enabled globally. Otherwise, the trace flag has no effect. We recommend that you enable global trace flags at startup, by using the -T command line option.
  • If a trace flag has either global or session scope, it can be enabled with the appropriate scope. A trace flag that is enabled at the session level never affects another session, and the effect of the trace flag is lost when the SPID that opened the session logs out.

Scope: global only

1204    Returns the resources and types of locks participating in a deadlock and also the current command affected.

1222    Returns the resources and types of locks that are participating in a deadlock and also the current command affected, in an XML format that does not comply with any XSD schema.

2,Trace Flag的打开和关闭

使用 DBCC TRACEON打开Trace Flag, -1 参数表示Trace Flag的Scope是Global。如果不指定该值,默认值是 Session Scope。

DBCC TRACEON ( trace_flag_id[ ,...n ][ , -1 ] ) 

-1  Switches on the specified trace flags globally.

使用 DBCC TRACEOFF关闭Trace Flag, -1 参数表示Trace Flag的Scope是Global。如果不指定该值,默认值是 Session Scope。

DBCC TRACEOFF ( trace_flag_id[ ,...n ][ , -1 ] ) 

示例:

--enable the trace flag globally
DBCC TRACEON (2528, -1)

--disable the trace flag globally
DBCC TRACEOFF (2528, -1)

--This flag is switched on only for the current connection.
DBCC TRACEON (3205)

--This flag is switched off only for the current connection.
DBCC TRACEOFF (3205)

3,查看Trace Flag的status

Use the DBCC TRACESTATUS command to determine which trace flags are currently active.  -1 参数表示Trace Flag的Scope是Global。如果不指定该值,默认值是 Session Scope。

DBCC TRACESTATUS ( [ [ trace_flag_id [ ,...n ] ] [ , ] [ -1 ] ] ) 

-1   Displays the status of trace flags that are enabled globally. If -1 is specified without trace_flag_id, all the global trace flags that are enabled are displayed.

示例:

-- displays the status of all trace flags that are currently enabled globally
dbcc tracestatus(-1);

-- lists all the trace flags that are enabled for the current session.
dbcc tracestatus();

--displays whether trace flag 3205 is enabled globally
DBCC TRACESTATUS (3205, -1);

4,常用Trace Falg

4.1,1204和 1222常用于检测数据库死锁

--1204  Returns the resources and types of locks participating in a deadlock and also the current command affected.
--1222  Returns the resources and types of locks that are participating in a deadlock and also the current command affected, in an XML format.     

     --1204是以文本格式保存, --1222是以XML格式保存
--3605  输出的结果存放到SQL server ERROR LOG
--3604  将trace结果输出到屏幕

4.2,查看加锁过程

--1200  返回加锁的整个过程

DBCC TRACEON(1200,-1)

DBCC TRACEON(3604) 

DBCC TRACESTATUS

5,查看Trace文件中记录的内存

1222,1204 这两个Trace Flag的输出结果被默认存储到Error Log中,可以使用 系统sp: sys.sp_readerrorlog 查看Error Log

exec sys.sp_readerrorlog 0,1,N‘deadlock‘

参考doc:

Trace Flags (Transact-SQL)

时间: 2024-11-09 06:42:56

Trace Flag的相关文章

Enable a SQL Server Trace Flag Globally on Linux

https://www.mssqltips.com/sql-server-tip-category/226/sql-server-on-linux// Microsoft has recently released SQL Server to run on Linux servers. Sometimes we need to enable a trace flag globally on SQL Server and in this tip I will demonstrate how to

sql server auto increment - trace flag 272

从 sql 2012 开始, 微软为了让 insert 时 auto increment 快一些,做了一个 cache 的机制. 这个机制虽然好,但是也有麻烦的情况,如果你的 sql 突然 restart 了, 那么这个 cache 就流失了 这回导致你的 sql auto increment Id 突然跳 1000, 比如从 45,46...1047. 如果这个你很在意这个号码的顺序. 那么你可以关掉这个 cache 机制. refer : http://www.dfarber.com/com

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

跟踪标记:3604 功能: 输出DBCC命令返回结果到查询窗口(通常是SSMS窗口),类似print命令的显示效果: 用途: 常用于获取DBCC IND, DBCC PAGE命令的输出结果,因为这2个命令默认不输出任何结果. 举例: USE master GO --开启跟踪标记 3604 DBCC TRACEON(3604) --查看页:(1,1,10) dbid 1(master), file id 1, page 10 DBCC PAGE(1,1,10) --关闭跟踪标记 3604 DBCC

HOW TO CHECK FOR ACTIVE TRACE FLAGS ON MICROSOFT SQL SERVER

http://crashmag.net/how-to-check-for-active-trace-flags-on-microsoft-sql-server You check for active trace flags by running the following query. They may be global or they may be session based. Checking for active trace flags DBCC TRACESTATUS The out

EBS获取并发程序Trace File

http://blog.itpub.net/16832682/viewspace-1249765/ 最近因为项目上出现了PL/SQL性能的问题,因此需要对已经开发好的并发程序进行调优的工作.调优有个很重要的步骤就是获取并发程序的trace file,从而才能知道是哪一段SQL出现了问题.下面就针对如何获取并发程序的trace file做个归纳:1.在并发程序注册页面启动跟踪功能:2.提交并发,待并发跑完之后获取trace ID,可以用如下代码来完成: SELECT 'Request id: '

EBS 抓trace 文件

如果要对FORM的操作做TRACE操作,可以使用 帮助->诊断->跟踪 中启用跟踪功能来实现. 但是如果要实现对并发请求的trace,需要在 系统管理员->并发->方案->定义 里找到对应的并发请求,并勾选”启用跟踪”项.然后提交这个并发请求,系统就会生成并发请求的trace文件.一个复杂的并发请求,会生成一个很大的trace文件,所以在做完trace后,记得勾除并发请求的“启用跟踪”项. 用如下的SQL来实现查找并发请求的TRACE文件: SELECT 'Request i

(转)SQL Server 性能调优(cpu)

摘自:http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html 研究cpu压力工具 perfom SQL跟踪 性能视图 cpu相关的wait event Signal wait time SOS_SCHEDULER_YIELD等待 CXPACKET等待 CMEMTHREAD等待 调度队列 cpu密集型查询 高CPU使用率的创建几种状况 miss index 统计数据丢失 非SARG谓词 隐式类型转化 参数探测器 ad ho

SQL Server数据库空间管理 (1)

数据库经常遇到的问题: 1).数据库文件空间用尽  2).日志文件不停增长 3).数据库文件无法收缩  4).自动增长和自动收缩 本系列就以上面的4个问题入手分析并总结数据库空间的管理方法.   1.文件的分配方式以及文件空间检查方法 首先,你在你的数据库中运行sp_spaceused;之后会看到当前数据库的使用空间信息:这个命令也就会将大概的数据库空间信息给你展现出来,并不能查看每个数据文件和日志文件的使用情况:其中对于数据库tempdb来说里面存储的临时数据对象,这个命令是统计不到的.我们知

SQL Server ->> Enable Instant File Initialization(开启文件及时初始化)

我们在安装SQL Server的时候会在指定SQL Server各个服务的运行启动账户的账户的时候看到底下有一个选项写着“Grant Perform Volume Maintenance Task privilege.....”.准确的说,这个东西不是SQL Server的东西,“Perform Volume Maintenance Task”权限属于Windows的安全策略(Security Policy)范畴,这里其实只是通过一个UI界面改了本地的Windows安全策略. 那“Perform