A useful script to analyse current queries on your box…

Ever been stuck trying to figure out what people are doing on your server, but unable to tell from DBCC InputBuffer what statements the users are running? This is one of the most useful scripts I’ve come accross and helps enormously when trying to analyse what is happening on your SQL Server.

It is often the case that users will query your database with tools like MS Access or Business Objects which are quite difficult to track. Well this useful tool helps to solve that problem… 

SQL Server 2005 Administrator\'s Pocket Consultant (Pro-Administrator\'s Pocket Consultant) 

Take for example an Access select query, when you trace the activity with the profiler what you will see is many individual parameterized sp_prepare, sp_exec…() statements running against the box. Even when you execute the ubiquitous sp_who2, you still do not get the info you really want. Sure we know somebody is executing something against database x, but then we hit a wall… What are they selecting?; what are the predicates? how are they joining? and most importantly, how is my server dealing with it?

Step in the function “statement_level_query_plan()”. This function allows you to see any currently executing queries on your box and whats more gives you an XMLShowplan schema so that you can see how the box is handling the request.

This is a truly magnificent function for any DBA who wants complete visibility of what users are running against their server…

Firstly you’ll need to install the function on your box.

This is the code courtesy of Microsoft.

Once you have created your new function, you are ready to run the query.

Get XML execution plans for currently running queries.

So - what to do with this resultset? Well as you scroll along the resultset you will see a column [statement_plan] and in it there will be hyperlinks to the execution plan in its XML format.

Clicking on the hyperlink

  1. Click on this link and a new XML document will be opened up.
  2. Save this as a new document with the extension “.sqlplan”
  3. Close and then re-open. A familiar execution plan will be visible with the SELECT statement at the top.

Once you have this tool you can harrass those people using their 3rd party tools to interrogate your database, and impress upon them what their poorly written query is doing to your system…

Happy days.

 

5 Responses to “A useful script to analyse current queries on your box…”

  1. James Says:

    You can also get the plan information by running a trace and capturing the XML ShowPlan events.
    However this does mean that you have to specifically set up a trace and run it, where as running the query above can be done at the drop of a hat.

  2. Steve Says:

    Steve…

    Cool! Its really cool….

  3. Find out whats running on SQL Server 2005 « SQL Server 2005 Performance Tuning Says:

    […] http://www.proteanit.com/b/2007/01/22/a-useful-script-to-analyse-current-activity-on-your-box/ […]

  4. Mark H Says:

    When I try an see this script it is in one giant line and it will not go. I tried braging this down where I thought the breaks should be and it still does not run for me. What am I doing wrong?

  5. Frank Says:

    Hi Mark,

    Which script are you trying to see? Do you mean the hyperlink in the results grid, the script used to create the function or the one that selects the running queries?

    When you click on the hyperlink in the results grid, a new file should open with the generated xml output. You then need to save this to a file with the extension .sqlplan. When you open it again in SSMS you should see it open as an execution plan.

    Let me know if you’re still struggling with it.

    HTH
    Frank

Leave a Reply