在SQL SERVER 中,可以使用SQL SERVER Profiler来捕获数据库操作,但是使用Profiler会对损耗服务器性能,一种解决办法是远程调用,另外一种解决办法就是使用TSQL来创建跟踪。
1.使用SQL SERVER Profiler来选择跟踪事件和相关配置
2.启动该跟踪,将该跟踪导出为TSQL脚本
/****************************************************/ /* Created by: SQL Server 2008 R2 Profiler */ /* Date: 10/25/2012 04:11:05 PM */ /****************************************************/ -- Create a Queue declare @rc int declare @TraceID int declare @maxfilesize bigint set @maxfilesize = 5 -- Please replace the text InsertFileNameHere, with an appropriate -- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension -- will be appended to the filename automatically. If you are writing from -- remote server to local drive, please use UNC path and make sure server has -- write access to your network share exec @rc = sp_trace_create @TraceID output, 0, N'I:\Test\TraceDemo1.trc', @maxfilesize, NULL if (@rc != 0) goto error -- Client side File and Table cannot be scripted -- Set the events declare @on bit set @on = 1 exec sp_trace_setevent @TraceID, 10, 15, @on exec sp_trace_setevent @TraceID, 10, 16, @on exec sp_trace_setevent @TraceID, 10, 1, @on exec sp_trace_setevent @TraceID, 10, 9, @on exec sp_trace_setevent @TraceID, 10, 17, @on exec sp_trace_setevent @TraceID, 10, 2, @on exec sp_trace_setevent @TraceID, 10, 10, @on exec sp_trace_setevent @TraceID, 10, 18, @on exec sp_trace_setevent @TraceID, 10, 11, @on exec sp_trace_setevent @TraceID, 10, 12, @on exec sp_trace_setevent @TraceID, 10, 13, @on exec sp_trace_setevent @TraceID, 10, 6, @on exec sp_trace_setevent @TraceID, 10, 14, @on exec sp_trace_setevent @TraceID, 12, 15, @on exec sp_trace_setevent @TraceID, 12, 16, @on exec sp_trace_setevent @TraceID, 12, 1, @on exec sp_trace_setevent @TraceID, 12, 9, @on exec sp_trace_setevent @TraceID, 12, 17, @on exec sp_trace_setevent @TraceID, 12, 6, @on exec sp_trace_setevent @TraceID, 12, 10, @on exec sp_trace_setevent @TraceID, 12, 14, @on exec sp_trace_setevent @TraceID, 12, 18, @on exec sp_trace_setevent @TraceID, 12, 11, @on exec sp_trace_setevent @TraceID, 12, 12, @on exec sp_trace_setevent @TraceID, 12, 13, @on -- Set the Filters declare @intfilter int declare @bigintfilter bigint exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 303ed42a-19ae-43b3-a749-ad3bc85cb244' -- Set the trace status to start exec sp_trace_setstatus @TraceID, 1 -- display trace id for future references select TraceID=@TraceID goto finish error: select ErrorCode=@rc finish: go
3.修改脚本中文件路径
exec @rc = sp_trace_create @TraceID output, 0, N'I:\Test\TraceDemo1.trc', @maxfilesize, NULL if (@rc != 0) goto error
4.跟踪启动后,查看现有跟踪状态
SELECT * FROM master.sys.fn_trace_getinfo(NULL) T WHERE T.traceid<>1
5.暂停跟踪
EXEC master.sys.sp_Trace_SetStatus <TraceId>,0
6.关闭并删除跟踪
EXEC master.sys.sp_Trace_SetStatus <TraceId>,2
7.将跟踪文件中数据导入到Table中
SELECT * INTO DB1.dbo.Trace20121025 FROM master.sys.fn_trace_gettable('I:\Test\TraceDemo1.trc.trc',default)
8.对数据进行分析
Note:
1.数据库默认跟踪编号为1.
2.数据库跟踪结果可以配合Performace Monitor产生的跟踪结果结合在SQL SERVER Profiler中展示。