![]() |
|
|
By: Greg Robidoux | Read Comments | Print Greg is the President of Edgewood Solutions and a co-founder of MSSQLTips.com. Related Tips: More |
|
Problem
When troubleshooting a SQL Server performance problem, one of the tools to use is Profiler. This tool allows you to collect metrics on statements that are run on your SQL Server for analysis and troubleshooting. The problem with using Profiler is that it is a client tool and unless it is run on the server itself the connection may be lost and your trace stops. This usually happens right before the problem your trying to troubleshoot occurs and you don't end up collecting that valuable information you need.
Solution
One alternative to using Profiler is to run a Server Side Trace. This process runs on the server and collects trace statistics pretty much the same way that you do using Profiler, but the process involves running a T-SQL script to start, run and stop the trace vs. using the Profiler GUI.
The server side trace can be modified to collect any event that the trace process can capture, but for this example we are just looking at SQL:StmtCompleted events which occur when a T-SQL statement has completed. For a complete list of events click here.
| EventNumber | Event | Description |
| 45 | SQL:StmtCompleted | Occurs when the Transact-SQL statement has completed. |
In addition to collecting information on certain events, you can also specify what data to collect. In this example we are collecting the statements or TextData, the SPID, Duration etc... For a complete list of columns click here.
| ColumnNumber | Column | Description |
| 1 | TextData | Text value dependent on the event class that is captured in the trace. |
| 12 | SPID | Server Process ID assigned by SQL Server to the process associated with the client. |
| 13 | Duration | Amount of elapsed time (in milliseconds) taken by the event. This data column is not populated by the Hash Warning event. |
| 14 | StartTime | Time at which the event started, when available. |
| 15 | EndTime | Time at which the event ended. This column is not populated for starting event classes, such as SQL:BatchStarting or SP:Starting. It is also not populated by the Hash Warning event. |
| 16 | Reads | Number of logical disk reads performed by the server on behalf of the event. This column is not populated by the Lock:Released event. |
| 17 | Writes | Number of physical disk writes performed by the server on behalf of the event. |
To create the trace for these events and columns the command would look as follows:
/***********************************************/ |
There are basically four components to this to get this running:
To add additional events and columns you would just include additional sp_trace_setevent commands such as the following to collect event 10 RPC:Completed for the same columns that we were collecting above.
EXEC sp_trace_setevent @TraceID, 10, 1, @on |
To start, stop and delete a trace you use the following commands.
| Task | Command | Notes |
| To find traceid | SELECT * FROM :: fn_trace_getinfo(default) | This will give you a list of all of the traces that are running on the server. |
| To start a trace | sp_trace_setstatus traceid, 1 | TraceId would be the value of the trace |
| To stop a trace | sp_trace_setstatus traceid, 0 | TraceId would be the value of the trace |
| To close and delete a trace | sp_trace_setstatus traceid,0 sp_trace_setstatus traceid, 2 |
To delete you need to stop the trace first and then you can delete the trace. This will close out the trace file that is written. |
Once the data has been collected you can load the data into a trace table and then run queries against the trace file. Following are some commands that can be used to load the trace data into a trace table.
| Task | Command | Notes |
| To load a trace | --Load into a new table |
|
| To query the table | SELECT * |
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |