SQL Server Profiler – a tutorial

Posted: 2011.01.07 in SQL Server 2005, SQL Server 2008

During all kinds of database interaction there are times when you want to see what is actually being sent to the database, the actual queries that are executed by the database engine. Some times you have 3rd party apps that do all kinds of different stuff under the hood, or maybe you have inherited a website from some developer that made a serious spaghetti of everything. The only way of knowing what is really going on in the database is to use SQL Server Profiler.

Now, although profiler is a simple yet *very* powerful tool, it does take some tweaking to make it work they way you want it to. It comes with some default templates but in my experience they are not nearly good enough to be useful in a working environment. There are a whole bunch of objects/events that can be monitored but what I’m gonna do is show you how to make a trace template that will provide the information you need most of the time.

Here is the order of things you need to do:

  1. First of all you need to open up Profiler. You can either find it in the Tools menu in SQL Server Management Studio, or you can find it on the Windows Start Menu under Microsoft SQL Server -> Performance Tools.
  2. When Profiler opens it will ask you to connect to the server you want to profile. Click “Cancel” in this window, no connection is needed to create a Profiler Template like we are doing now.
  3. You should now have a blank Profiler window and it’s time to create the new template. Go to File -> Templates and then click “New Template”
  4. The General window of the Template generator pops up and you need to fill out the selected server type for the template and a new template name. I usually go for “MyTemplate” (quite original right :) as the name  as I usually only have one custom template in there. You should also check “Base the new template on an existing one” and choose “TSQL_Duration” as your existing template. Finally check the last checkbox to make this the default template for new traces. Do NOT click Save!
  5. Now you need to click “Event Selection” which is the second tab in the open window and for people not familiar with Profiler this is where the tricky part starts. After all, how do you know which events you need to choose if you’ve never used it before??
  6. Since we chose to base the new template on an existing one (TSQL_duration), the events selection window will be preconfigured with some events. Be aware that the two events that are chosen will only take in to account those queries/procedures that have actually finished. Check the checkbox in the lower right part of the window that says “Show all columns”. A bunch of checkboxes will appear next to the two preconfigured events.
  7. Now you need to do the following:
    • Remove the checkbox for the column BinaryData for the event RPC:Completed.

    Then make sure that the following checkboxes are checked for BOTH events:

    • Duration (the number of milliseconds it took to run the query)
    • TextData (the actual sql query text/stored procedure call that is sent to the database, including parameters)
    • SPID (the system process ID of the current connection)
    • ApplicationName (the name of the application the query/procedure originates from, could sometimes be empty)
    • DatabaseName (the database name the query is sent to)
    • HostName (the server name of the server sending the query)
    • LoginName (the username of the authenticated user that opened the connection)
    • StartTime (the timestamp when the query was started)
  8. Ok, we’ve come a long way now! What we need to do now is to set some Column Filters so our traces wont be cluttered with mumbo-jumbo that is not relevant to what we are doing:
    • Uncheck the “Show all columns”
    • Click “Column Filters”
  9. Setting the correct Column Filters is essential to getting good trace output and if you don’t do it right your trace might get flooded a lot of stuff that will make it harder for you to find what you’re looking for. And the only column filter we will set for the *template* is a SPID filter of Greater than or equal to 50. All SPIDs that have a number less than 50 are reserved for internal processes and are usually not of any value to us. Also make sure that the “Exclude rows that do not contain values” is checked. Click OK to save your column filters. When at a later stage you are  using this template, it will be useful to add additional filters. Remember that this is only a template, so when your’e running a trace using this template you can change all the events and columns and filters to your liking. What I usually do though is to create filters on the TextData (the sql query/procedure that is being exeuted) or the DatabaseName. Filters are ususlly specified as LIKE with a value of i.e “%CREATE TABLE%” (without any quotation marks) or %MyDatabaseName% and always remember to check the “Exclude rows that do not contain values” when making filters.
  10. Now you need to click “Organize Columns”. And I have always found Profiler to be a little weird when it comes to this organizing, because Profiler actually sorts the output of the trace according to the order you specify here. This in itself might not be so weird but the fact that it is impossible to sort the output of a trace after it has started makes this step really important! And it took me quite a while to figure it out actually :) And to me at least it’s vital that the trace is always sorted by StartTime. If you sort by StartTime it has no real importance what the order of the other columns are so these can be put in whichever order you prefer. After your finished organizing your windows should look more or less like this. The important thing is that the StartTime is on top!
  11. Once you have done all this you are set to go, and all you need to do now is to click “Save”. Your template is done!
  12. To test the newly created template you need to start a new trace by going to File -> New Trace. You need to log on to the server you want to trace, and once you have done that the new trace window will open. If you set the template you just created to be the default template it will automatically be selected for you, if not you will have to select it manually in the “Use the template” dropdown.
  13. Click Run and all new trace events that satisfy your filters will now appear in the order of when they were started :) By selecting one or more lines (use the Ctrl-key to select several)  the actual t-sql that was sent to the database will be displayed in the details window. If you select several lines, they will be separated by the GO statement automatically. 

Happy Tracing!

About these ads
Comments
  1. Avinash Kumar says:

    Excellent document….

  2. Shamil says:

    Thank you very much!
    Great explanation: clear and detailed.

  3. Dhavapriya says:

    Nice Ariticle

  4. hamsa says:

    Thank you.. its nice article for beginners..

  5. Yatwei says:

    Thanks, man. This article has enlightened me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s