sql server trace 是一个轻量级的追踪工具,对追踪数据库的行为很有用,因此,sql server内置一个trace(default trace)。
1,sql server 内置Default Trace,Default Trace的TraceID是1,默认是开启的,可以通过sys.configurations 进行查看
select * from sys.configurations where configuration_id=1568
字段 value=1,表示Default Trace是开启的。
2,如果default trace没有开启,可以利用如下语句进行开启,或者关闭等操作。
--开启Default Trace sp_configure ‘show advanced options‘ , 1 ; GO RECONFIGURE; GO sp_configure ‘default trace enabled‘ , 1 ; GO RECONFIGURE; GO --关闭Default Trace sp_configure ‘default trace enabled‘ , 0 ; GO RECONFIGURE; GO sp_configure ‘show advanced options‘ , 0 ; GO RECONFIGURE; GO
3,查看Default Trace的trace文件的存放路径,default trace的id是1
select * from sys.traces where id=1
Trace status: 0 = stopped ,1 = running
max_size:Maximum trace file size limit in megabytes (MB). This value is null when the trace is a rowset trace.
max_files:Maximum number of rollover files. This value is null if the Max number is not set.
系统默认提供5个跟踪文件,每一个文件的最大size默认是20MB,SQL Server负责维护这5个文件,当实例重启的时候或者到达文件最大值的时候,sql server生成新的文件,将最早的跟踪文件删除,依次滚动(rollover)更新。
4,查看trace 文件的内容
sys.fn_trace_gettable ( ‘filename‘ , number_files )
Be aware that the fn_trace_gettable function will not load rollover files (when this option is specified by using the number_files argument) where the original trace file name ends with an underscore and a numeric value. (This does not apply to the underscore and number that are automatically appended when a file rolls over.) As a workaround, you can rename the trace files to remove the underscores in the original file name. For example, if the original file is named Trace_Oct_5.trc and the rollover file is named Trace_Oct_5_1.trc, you can rename the files to TraceOct5.trc and TraceOct5_1.trc.
filename 能够从sys.traces 中获取。
select * from sys.fn_trace_gettable(N‘C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\log_4.trc‘,default)
5,查看default trace 追踪的Event 和 EventColumn
fn_trace_geteventinfo ( trace_id )
该函数返回 EventID 和 EventColumnID
select tgei.eventid,tgei.columnid from sys.fn_trace_geteventinfo(1) tgei
查看default trace 追踪的Event详细信息,包括 Catagory,EventName 和 Column
select categ.name as CategoryName, te.trace_event_id as EventID, te.name as EventName, tc.trace_column_id as EventColumnID, tc.name as ColumnName, tc.type_name as ColumnType from sys.fn_trace_geteventinfo(1) as gei inner join sys.trace_columns tc on gei.columnid=tc.trace_column_id inner join sys.trace_events te on gei.eventid=te.trace_event_id inner join sys.trace_categories categ on te.category_id=categ.category_id order by CategoryName,EventID,EventColumnID
6,查看Event和其Category,Category用于组织Event。在SQL Server 2012中,共有21个Category,180个Event,每个Event属于唯一的一个Category。
select tc.name as CategoryName, te.trace_event_id as EventID, te.name as EventName from sys.trace_categories tc inner join sys.trace_events te on tc.category_id=te.category_id order by CategoryName
7,查看Event的Column,在SQL Server 2012中,共有66个Column,并不是每个Event都能绑定所有66个column,每个Event能够绑定的Column是固定的。
The sys.trace_event_bindings catalog view contains a list of all possible usage combinations of events and columns. For each event listed in the trace_event_id column, all available columns are listed in the trace_column_id column. Not all available columns are populated each time a given event occurs.
select te.trace_event_id as EventID, te.name as EventName, tc.trace_column_id as EventColumnID, tc.name as ColumnName, tc.type_name as ColumnType from sys.trace_event_bindings teb inner join sys.trace_columns tc on teb.trace_column_id=tc.trace_column_id inner join sys.trace_events te on teb.trace_event_id=te.trace_event_id order by EventID,EventColumnID
参考doc:
Server-wide Configuration Catalog Views (Transact-SQL)