Introduction to SQL Server Profiler

How often do you find yourself with the need to know what statements are occurring on your SQL Server?  Well, if you're like me the answer is "ALL THE TIME".  Constantly Im asked about processes running on the server if things are slow.  Even more importantly, I like to capture data from the system to use for trending purposes and to proactively diagnose performance problems before someone comes and tells me about it.  Luckily, SQL Server comes with tools to capture such data, and to save it for later use.  The tool is SQL Server Profiler, and it is wonderful for setting up trace files to save data so that you can report on the happenings on the server.

To start a new SQL Server Profiler session, you can click on the Tools menu and select it from the downdown (so long as you're not using SQL Server Express edition - Profiler does not come with Express).

Selecting Profiler from the menu brings up the SQL Server Profiler application.  At this point, you'll want to create a New Trace.  This can be done from the File menu, or by pressing Control + N.

 

A Bit about Traces
A trace is a SQL Server object that captures information regarding certain events that occur on the database instance.  The information traces gather can be as simple as catching stored procedure executions to as complex as catching deadlock graphs.  Whats great about them is that you can select certain columns of data as well as filter for certain events.  These traces are absolutely imperative for any SQL Server DBA to use, and are extremely handy for diagnosing problems on the server, such as performance problems.  SQL Server Profiler is a graphical user interface (GUI) for creating a grouping of traces.  It makes it super easy to define them, alter them, and save them.  However, if you want to define your own custom trace files through TSQL, I show you how in this article.

To run a new trace, you'll first need to connect to an instance of SQL Server.  If you're using 2005 or 2008 a screen similar to the one below should appear.  You can type your instance name (your server name if it is the default instance) or select your instance from the dropdown box.

 

Once I am connected to my server, I can define the Events, Columns and Filters I want to use in my trace.  SQL Profiler comes with a handy set of predefined trace templates that you can use to capture common data.  For the most part, these are great if you have a limited set of needs for your trace file. However, if you want more specific information you're going to need to define your own trace or alter some of the predefined templates. 

In the trace properties screen I have the option to save the trace data to a file or to a table, or both.  If you want to save the data you capture, here is a good place to do it.

 

Clicking on the Events Selection tab I can set filters for my trace events.  For example, in the screenshot below I am setting criteria to only capture events where the event is intitiated by the LoginName "thedude".  More realistic filters may include looking for logical Reads > 1000 or a duration of a query longer than 2 seconds.

 

In the same screen I can decide which columns of data are important to me.  If I am capturing information regarding statements running, stored proc execution, etc then I want to know such informaiton as the name of the procedure, statement running, duration, reads, writes, etc.  I can use this information to find those procedures which may be resource hogs.

 

I can also easily organize the order in which the columns appear.  I can also group them, which I appreciate doing after I've captured the trace as it allows me to group the durations and reads of statements to easily identify longer running statements.

 

Below is a sample output of what statements were captured on my system while I was running the trace file.  Notice the amount of detail that is showed through the trace.  I am easily able to capture the Reads, CPU, SQL Statement, and Duration of the statement just through the use of the trace file.

 

Once you have your trace defined the way you want it, you have a number of options as to how you want to save it.  You can save the results from the trace to a File (flat or XML) or a Table.  I like to use the Table option since it allows me to view the data in a format to which I am accustomed to using, and so I can easily query it how I want.  What is also great about the Save option is that I can save the definition of the trace file so that I can use it later.  From there I can view the TSQL which comprises the trace so that I can save it later.  I use this method when I want to quickly define a trace, and then create a stored procedure that runs on system start up to start the trace.  Doing so make sure I can automatically capture events without having to go through the manual interaction to do so.

 

Conclusion
Today I took a broad overview of what SQL Server Profiler is, what it does, and what you can use it for.  I hope it is a great place to get started opening the window to the world on your SQL Server.  However, care must be taken when using the tool.  If you try to capture too many events then you're risking slowing down your server because there is overhead in capturing the events.  In a later writing, I plan to dig a little more deeply into these issues, and how you can use Profiler to diagnose some common performance problems on your server.

 


Posted 5/27/2009 8:12 PM by TimChapman

Comments

Feodor Georgiev wrote re: Introduction to SQL Server Profiler
on 6/11/2009 5:53 AM

Yes, profiler is a great tool. However, consider that server side traces are generally more flexible and give better over all performance. Also, keep in mind that with SST and Profiler, you can specify how many files, what size of files and what start time and end time to be used as parameters for the trace. If you use those values in a reasonable manner, you don't have to sit next to the server or worry that the disk will run out of space.

TimChapman wrote re: Introduction to SQL Server Profiler
on 6/11/2009 11:53 AM

I totally agree.  That is why I had mentioned in the article that Profiler is great for generating the code you'd need to create the server side trace...and then just have it start when the SQL Service starts.   It beats the heck out of trying to figure out what events to pass to the trace procs, etc.

Feodor Georgiev wrote re: Introduction to SQL Server Profiler
on 6/12/2009 2:35 AM

One more thing: I have seen soo many people doing this "the wrong way", that I have to mention this:

Events by themselves mean almost nothing; it is the CONTEXT which brings the entire picture together.

For an example, sometimes I would find myself running 2 traces: one has only the essence of the event related to the problem I am trying to solve (lets say - locking), and the other one would be a detailed trace for the context of the problem (rpc, etc).

I have seen way too many people staring at the monitor for hours, trying to see a magic instance of an event, and they would browse through thousands of rows in the profiler. What I do is to run the 2 traces above, then I would go drink some coffee for 15 minutes, then I would come back and relate the 2 traces. If there is something interesting in the first one, I go in the detailed one and search for the time it occured and I look at the context...

TimChapman wrote re: Introduction to SQL Server Profiler
on 6/12/2009 7:28 AM

I agree w/ that too....

Feodor Georgiev wrote re: Introduction to SQL Server Profiler
on 6/13/2009 12:37 AM

And the last, but not least, it is very important to know the ability to relate the results from the perfmon and profiler. This is a subject for a whole new blog post, however, it would be good to let the idea floating: you can get a result from perfmon for whatever counters you are interested in, then you can have a trace file for the same period, and you can relate both in profiler, where you will be able to see the graphical representation of the system resources with the trace, and you can explore the peaks and lows directly connecting them to the sql trace.

TimChapman wrote re: Introduction to SQL Server Profiler
on 6/14/2009 2:15 PM

Hi Sibir1us,

If you're interested in writing an article on how to relate events from Perfmon to trace files, let me know and I'd be more than happy to post your article on here.

Tim

Copyright SQL Server Nation 2010
Powered by Community Server (Non-Commercial Edition), by Telligent Systems