sql server跟蹤資料庫

datapeng發表於2017-08-10

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
goto finish

error:
select

finish:
go

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29371470/viewspace-2143339/,如需轉載,請註明出處,否則將追究法律責任。

相關文章