【监控笔记】【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 Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\blackbox.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'
参考文件
其他参阅:联机丛书--事件
相关参考:深入了解跟踪
分类:
sql server 监控
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2018-05-28 (1.3)DML增强功能-Apply、pivot、unpivot、for xml path行列转换