Systems Engineering and RDBMS

Archive for May 14th, 2008

Capturing XML Plans using SQL Profiler

Posted by decipherinfosys on May 14, 2008

In response to the composite indexes post, one of the readers had asked how to go about capturing the XML plans using profiler so that he can evaluate the plans later on.

Firstly, let us state that please do not run SQL Profiler directly on the production server. It can generate a lot of load and it is always advisable to run it from a separate machine. Having said that, now let’s take a look at which bare minimum events and columns can we use in order to capture the XML plans for us in the production environment. We will go ahead and create a new profiler template for this. Go ahead and open up SQL Profiler and then select File\Templates\New Template. Name the new template as XML_PLAN_CAPTURE. Then, go to the Events tab and under the Performance section, select “Showplan XML”. This event by itself is good enough but we should always select these events as well to help with the analysis:

  1. SQL:BatchStarting (Under the TSQL section)
  2. SQL:BatchCOmpleted (UNder the TSQL section)
  3. RPC:Completed (Under the Stored Procedures Section)

You can then set up your filters and also organize columns. One thing that we always like to do is to Organize the columns such that the EventClass falls under the Groups so that we can group the data using the EventClass. Depending upon what you are trying to analyze, one can also do the grouping based on: DatabaseName, ApplicationName, StartTime, Spid and EventClass. That way, for a given database, a given application, in a sequential order of starttime, all the data for a given spid will be captured grouped by the different events. That can make analysis a bit simpler.

As far as filters go, one can choose the SPID to be more than 50 to eliminate the system events and under the application name should elimiate the Profiler (%Profiler%) Management Studio (%Management%) and other applications that you do not wish to trace the data from. In addition, one can add a LoginName if you have separate logins for connecting to the instance as well as the DatabaseName if you wish to trace for any particular database only. Addition filters are very specific to the situation like capturing only those statements which have a duration of more than say 3 seconds (do note that the duration is in milli-seconds so choose the number correctly).

Now, once we have it, save that as a template and it will be present for you the next time you want to use it. One can also export this out as a *.tdf file and then send it out to team members to use it for their purposes – they would just need to double click it in order to register it.

Let’s run that trace now…we ran these statements on our healthcare database:

use misys
go
select top 10 * from dbo.pat
go
select top 10 * from dbo.insurance
go
SET NOCOUNT ON

BEGIN TRY
BEGIN TRANSACTION
update dbo.insurance
set ins_address2 = ‘test’
where ins_number = 2
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH
GO

And here was the output:

One can then right click a specific XML execution plan and choose to extract the event data and save it with an extension of *.sqlplan to be able to then copy the execution plan in a graphical manner or just plain simple XML.

One thing that we forgot to mention before – when setting up a trace using this trace template or even when doing a trace for any XML event, you will see that there is a third tab at the top which states: “Events Extraction Settings”. Since we were tracing the XML events, we have the choice of saving the execution plans as a separate file with the extension of *.sqlplan. See below:

You can then run the trace and you will see that all the execution plans have now been captured in that output file. If you double click that file, it will open up in SQL Server Management Studio as a graphical execution plan file. See below:

So, using these simple steps, one can capture the XML showplans and then do analysis of what went wrong and which SQL statements need to be tuned and what were the runtime execution plans. One can schedule the SQL Profiler trace by using the server side stored procedures via a script – we will cover that in an upcoming post and will also see how one can schedule such scripts can capture the relevant data.

Posted in SQL Server | Leave a Comment »

Query to look at the execution plans in cache

Posted by decipherinfosys on May 14, 2008

Here is a simple query using DMVs and DMFs to look at the cached execution plans on an instance in SQL Server:

SELECT  [sdecp].[refcounts]
,[sdecp].[usecounts]
,[sdecp].[objtype]
,db_name([dest].[dbid]) as database_name
,object_schema_name([dest].[objectid], [dest].[dbid]) as [schema_name]
,object_name([dest].[objectid], [dest].[dbid]) as [object_name]
,[dest].[text]
,[deqp].[query_plan]
FROM    sys.dm_exec_cached_plans sdecp
CROSS APPLY sys.dm_exec_sql_text(sdecp.plan_handle) dest
CROSS APPLY sys.dm_exec_query_plan(sdecp.plan_handle) deqp
where [dest].[dbid] is not null
order by database_name, usecounts desc

Using this query, you can look at the SQL that was called and the plan (in XML) that was generated by the optimizer.  One can then either just open up the XML or open it as a graphical execution plan.  In order to open it up as a graphical plan, you can save it as <filename>.sqlplan and then open it up in SQL Server Management Studio and it will open up as a graphical plan.

Posted in SQL Server | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 81 other followers