Home
Blog
About
Database administration
Operating systems
Development
Links
Following require login:
ScratchPad
Normally use SQL Profiler GUI, but it's a bit of a pain.
Some links about scheduling tracing in SQL Server
http://www.mssqltips.com/tip.asp?tip=1715
http://www.novicksoftware.com/Articles/scripting-traces-for-performance-monitoring-on-sql-server-page3.htm
http://vyaskn.tripod.com/server_side_tracing_in_sql_server.htm
Of these, the first is straightforward and the last is a fairly comprehensive wrapper around the trace functions and includes data for all the event and column codes.
Using server tracing for performance evaluation
This describes the use of sys.traces: http://www.sqlservercentral.com/articles/Stairway+Series/72465/
1> select * FROM ::fn_trace_getinfo(default) 2> go traceid property value ------- -------- ----- 1 1 2 1 2 G:\Trace\20091023\trace_0344 1 3 5000 1 4 2009-10-23 03:49:03.497000000 1 5 1
Where property values are:
1 | Trace options |
2 | Trace file name |
3 | Max trace file size in MB |
4 | Trace stop time (NULL=run indefinitely) |
5 | Trace status, 0=stopped, 1=running |
Each event can have one or more columns of data associated with it, these columns represent the values that are ultimately output (e.g. in SQL Profiler). Event and column defintions can be found here
1> select * FROM ::fn_trace_geteventinfo(1) 2> go eventid columnid ------- -------- 11 1 Textdata 11 2 BinaryData 11 3 DatabaseID 11 8 Hostname 11 9 Client process ID 11 10 Application ID 11 11 Login name 11 12 SPID 11 14 Start time 11 21 Event sub class 11 26 Server name 11 35 Database name ... 1> select distinct eventid FROM ::fn_trace_geteventinfo(1) 2> go eventid ------- 11 RPC start 13 SQL batch start 14 Audit login 15 Audit logout 17 Existing connection - detects activity from before trace started 53 Cursor Open 70 Cursor prepare 71 Prepare SQL 72 Exec prepared SQL 74 Cursor execute 77 Cursor unprepare 78 Cursor close 100 RPC output parameter
Column definitions are here
1> select * FROM ::fn_trace_getfilterinfo(1) 2> go columnid logical_operator comparison_operator value -------- ---------------- ------------------- ----- 3 0 0 17 3 1 0 16 10 0 7 SQL Profiler
A trace is created by executing sp_trace_create, e.g.
declare @TraceID int declare @maxfilesize bigint -- The maximum trace file size in megabytes set @maxfilesize = 5 exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL if (@rc != 0) goto error goto finish error: select ErrorCode=@rc :finish go
Note that traces created this way are initially stopped and do not have any events or filters associated with them.
Use sp_trace_setevent to add events and columns, e.g.
-- 11 = RPC start exec sp_trace_setevent @TraceID, 11, 1, @on -- Textdata exec sp_trace_setevent @TraceID, 11, 2, @on -- Binary data exec sp_trace_setevent @TraceID, 11, 3, @on -- Database ID exec sp_trace_setevent @TraceID, 11, 8, @on -- Hostname exec sp_trace_setevent @TraceID, 11, 9, @on -- Client ID exec sp_trace_setevent @TraceID, 11, 10, @on -- Application ID exec sp_trace_setevent @TraceID, 11, 11, @on -- login name exec sp_trace_setevent @TraceID, 11, 12, @on -- SPID exec sp_trace_setevent @TraceID, 11, 14, @on -- Start time exec sp_trace_setevent @TraceID, 11, 21, @on -- Event sub class exec sp_trace_setevent @TraceID, 11, 26, @on -- Server exec sp_trace_setevent @TraceID, 11, 35, @on -- Database name
Use sp_trace_setfilter to add filters, e.g.
-- Database ID = 16 exec sp_trace_setfilter @TraceID, 3, 1, 0, 16 -- Exclude SQL Profiler exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Profiler%'
Use sp_trace_setstatus to start, stop and close the trace, e.g.
exec sp_trace_setstatus @traceid=1, @status=1 -- start trace exec sp_trace_setstatus @traceid=1, @status=0 -- stop/pause trace exec sp_trace_setstatus @traceid=1, @status=2 -- close and delete trace