码迷,mamicode.com
首页 > 其他好文 > 详细

Trace Flag

时间:2016-07-31 17:42:51      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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,Ndeadlock

 

参考doc:

Trace Flags (Transact-SQL)

Trace Flag

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4650727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!