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…
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.
Once you have created your new function, you are ready to run the query.
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.
- Click on this link and a new XML document will be opened up.
- Save this as a new document with the extension “.sqlplan”
- 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.
January 23rd, 2007 at 12:53 am
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.
July 23rd, 2007 at 11:55 am
Steve…
Cool! Its really cool….
December 13th, 2007 at 9:31 pm
[…] http://www.proteanit.com/b/2007/01/22/a-useful-script-to-analyse-current-activity-on-your-box/ […]
June 4th, 2008 at 12:41 pm
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?
June 4th, 2008 at 2:17 pm
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