今天就跟大家聊聊有关sql server跟踪数据库的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
select * FROM ::fn_trace_getinfo(NULL) WHERE property=5 AND traceid>1
--设置阻塞阀值
use master
GO
sp_configure 'show advanced options',1
RECONFIGURE WITH override
GO
sp_configure 'blocked process threshold',1
RECONFIGURE WITH override
GO
-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
declare @DateTime datetime
--设置跟踪结束时间
set @DateTime = '2017-03-31 12:01:00.000'
--预留30G空间
set @maxfilesize = 1000
-- 2表示环滚文件 存储目录需要设置
exec @rc = sp_trace_create @TraceID output, 2, N'd:\sqltrace', @maxfilesize, @Datetime,30
if (@rc != 0) goto error
-- 设置跟踪事件
declare @on bit
set @on = 1
--跟踪阻塞
exec sp_trace_setevent @TraceID, 137, 3, @on
exec sp_trace_setevent @TraceID, 137, 15, @on
exec sp_trace_setevent @TraceID, 137, 1, @on
exec sp_trace_setevent @TraceID, 137, 13, @on
--跟踪死锁
exec sp_trace_setevent @TraceID, 148, 11, @on
exec sp_trace_setevent @TraceID, 148, 12, @on
exec sp_trace_setevent @TraceID, 148, 14, @on
exec sp_trace_setevent @TraceID, 148, 1, @on
--跟踪存储过程调用
exec sp_trace_setevent @TraceID, 10, 15, @on --EndTime
exec sp_trace_setevent @TraceID, 10, 16, @on --Reads
exec sp_trace_setevent @TraceID, 10, 1, @on --TextData
exec sp_trace_setevent @TraceID, 10, 3, @on --DatabaseID
exec sp_trace_setevent @TraceID, 10, 17, @on --Writes
exec sp_trace_setevent @TraceID, 10, 10, @on --ApplicationName
exec sp_trace_setevent @TraceID, 10, 18, @on --CPU
exec sp_trace_setevent @TraceID, 10, 12, @on --SPID
exec sp_trace_setevent @TraceID, 10, 14, @on --StartTime
--跟踪T-SQL调用
exec sp_trace_setevent @TraceID, 12, 15, @on --EndTime
exec sp_trace_setevent @TraceID, 12, 16, @on --Reads
exec sp_trace_setevent @TraceID, 12, 1, @on --TextData
exec sp_trace_setevent @TraceID, 12, 17, @on --Writes
exec sp_trace_setevent @TraceID, 12, 10, @on --ApplicationName
exec sp_trace_setevent @TraceID, 12, 14, @on --StartTime
exec sp_trace_setevent @TraceID, 12, 18, @on --CPU
exec sp_trace_setevent @TraceID, 12, 12, @on --SPID
exec sp_trace_setevent @TraceID, 12, 3, @on --DatabaseID
-- 设置过滤
declare @intfilter int
declare @bigintfilter bigint
--过滤非SQL跟踪
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler'
--过滤读大于等于1000才被跟踪
set @bigintfilter = 1000
exec sp_trace_setfilter @TraceID, 16, 0, 4, @bigintfilter
--启动跟踪
exec sp_trace_setstatus @TraceID, 1
-- 返回TraceID
select TraceID=@TraceID
goto finish
error:
select ErrorCode=@rc
finish:
go
看完上述内容,你们对sql server跟踪数据库的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。