【监控笔记】【1.3】监控事件系列——SQL Trace(黑盒跟踪 BlackBox Trace)

【1】它跟踪了哪些事件?

  (1.1)存储过程执行(SP:Strating)

  (1.2)T-SQL执行(SQL:BatchString)

  (1.3)错误和警告(Exception,Attention)

~~对于这些事件,下面的信息会被跟踪

  (1.1)执行的查询或错误信息。

  (1.2)执行的日志和时间

  (1.3)执行查询或存储过程的用户

  (1.4)事件发生的数据库

  (1.5)发送查询或导致错误的服务器或工作站

  (1.6)实施查询的应用程序名

【2】黑盒跟踪的作用

  sql server黑盒跟踪有用大量运行数据的记录。它记录了发送到sql server所有查询以及类似错误信息的有用记录,可以帮助诊断间歇性服务器崩溃,或者再CPU飙高之前发生了什么错误。

【3】定义与使用

declare @TraceId int
declare @maxFilesize bigint
declare @filecount int
set @filecount=3
set @maxFilesize =200

--sp_trace_create联机丛书
exec sp_trace_create @TraceId output, @options=8, @tracefile=null, --该跟踪无法指定文件路径参数 @maxfilesize=@maxFilesize, @stoptime=null, @filecount=@filecount exec sp_trace_setstatus @TraceId,1 --0为禁用,2为停止,1为启用
print @traceId
--select * from sys.traces --查看所有跟踪

【4】查阅与分析

--使用select * from sys.traces 来寻找新建的跟踪,或上门的输出,找到路径

select
databasename,objectName,hostname,textdata, applicationName,sessionLoginName,loginName,spid,Starttime,eventclass,t2.name from sys.fn_trace_gettable('\?D:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLDATAlackbox.trc',default) t1 join sys.trace_events t2 on t1.eventclass=t2.trace_event_id

 


【5】生产环境下的自启配置

use master;
go
create procedure StartBlackBoxTrace
as 
begin
    declare @TraceId int
    declare @maxFilesize bigint
    declare @filecount int
    set @filecount=3
    set @maxFilesize =200

    exec sp_trace_create
    @TraceId output,
    @options=8,
    @tracefile=null,
    @maxfilesize=@maxFilesize,
    @stoptime=null,
    @filecount=@filecount

    exec sp_trace_setstatus @traceId,1 --0为禁用,2为关闭,1为启用
end

go

--服务启动时自动启动 exec sp_procoption 'StartBlackBoxTrace','startup','on'

 参考文件

   其他参阅:联机丛书--事件

   相关参考:深入了解跟踪

原文地址:https://www.cnblogs.com/gered/p/10939845.html