Execution Plan Basics - Simple Talk
Execution Plan Basics - Simple Talk
These processes are run for each and every query submitted
to the system. While there are lots of different actions
occurring simultaneously within SQL Server, we’re going to
focus on the processes around T-SQL. They break down
roughly into two stages:
Query Parsing
When you pass a T-SQL query to the SQL Server system, the
rst place it goes to is the relational engine. [1]
Query Execution
Once the execution plan is generated, the action switches to
the storage engine, where the query is actually executed,
according to the plan.
Next is the plan that represents the output from the actual
query execution. This type of plan is known, funnily enough,
as the Actual execution plan. It shows what actually
happened when the query executed.
Each plan is stored once, unless the cost of the plan lets the
optimizer know that a parallel execution might result in
better performance (more on parallelism in Chapter 8). If the
optimizer sees parallelism as an option, then a second plan
is created and stored with a different set of operations to
support parallelism. In this instance, one query gets two
plans.
DBCC FREEPROCCACHE
SELECT [cp].[refcounts]
, [cp].[usecounts]
, [cp].[objtype]
, [st].[dbid]
, [st].[objectid]
, [st].[text]
, [qp].[query_plan]
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text ( cp.plan_h
CROSS APPLY sys.dm_exec_query_plan ( cp.plan
With this query we can see the SQL called and the XML plan
generated by the execution of that SQL. You can use the
XML directly or open it as a graphical execution plan.
SELECT *
FROM TempTable ;
Graphical Plans
Text Plans
XML Plans
The one you choose will depend on the level of detail you
want to see, and on the individual DBA’s preferences and
methods.
Graphical Plans
These are quick and easy to read but the detailed data for
the plan is masked. Both Estimated and Actual execution
plans can be viewed in graphical format.
Text Plans
These are a bit harder to read, but more information is
immediately available. There are three text plan formats:
XML Plans
XML plans present the most complete set of data available
on a plan, all on display in the structured XML format. There
are two varieties of XML plan:
Getting Started
Execution plans are there to assist you in writing e cient T-
SQL code, troubleshooting existing T-SQL behavior or
monitoring and reporting on your systems. How you use
them and view them is up to you, but rst you need to
understand the information contained within the plans and
how to interpret it. One of the best ways to learn about
execution plans is to see them in action, so let’s get started.
Sample Code
Throughout the following text, I’ll be supplying T-SQL code
that you’re encouraged to run for yourself. All of the source
code is freely downloadable from the Simple Talk
Publishingwebsite (http://www.simpletalkpublishing.com).
http://www.codeplex.com/MSFTDBProdSamples
The plans you see may not precisely re ect the plans
generated for the book. Depending on how old a given copy
of AdventureWorks may be, the statistics could be different,
the indexes may be different, the structure and data may be
different. So please be aware that you won’t always see the
same thing if you run the examples.
SELECT *
FROM [dbo].[DatabaseLog];
I tend to click the icon more often than not but, either way,
we see our very rst Estimated execution plan, as in Figure
1.
Figure 1
We’ll explain what this plan means shortly, but rst, let’s
capture the Actual execution plan.
Type Control-M.
ToolTips
Each of the icons and the arrows has, associated with it, a
pop-up window called a ToolTip, which you can access by
hovering your mouse pointer over the icon.
Figure 3
Figure 4
After that, we see the estimated costs for I/O, CPU, Operator
and Subtree. The Subtree is simply the section of the
execution tree that we have looked at so far, working right to
left again, and top to bottom. All estimations are based on
the statistics available on the columns and indexes in any
table.
The I/O Cost and CPU cost are not actual operators, but
rather the cost numbers assigned by the Query Optimizer
during its calculations. These numbers are useful when
determining whether most of the cost is I/O-based (as in
this case), or if we’re putting a load on the CPU. A bigger
number means more processing in this area. Again, these
are not hard and absolute numbers, but rather pointers that
help to suggest where the actual cost in a given operation
may lie.
You’ll note that, in this case, the operator cost and the
subtree cost are the same, since the table scan is the only
operator. For more complex trees, with more operators,
you’ll see that the cost accumulates as the individual cost
for each operator is added to the total. You get the full cost
of the plan from the nal operation in the query plan, in this
case the Select operator.
Operator Properties
More information is available than that presented in the
ToolTips. Right-click any icon within a graphical execution
plan and select the “Properties” menu item to get a detailed
list of information about that operation. Figure 5 shows the
details from the original table scan.
Figure 5
SET SHOWPLAN_ALL ;
And:
GO
SELECT *
FROM [dbo].[DatabaseLog] ;
GO
SET SHOWPLAN_ALL OFF ;
GO
When you execute this query, the estimated plan is shown in
the results pane. Here is the rst column of the results:
Figure 6
SET SHOWPLAN_XML ON
...
SET SHOWPLAN_XML OFF
GO
SET SHOWPLAN_XML ON ;
GO
SELECT *
FROM [dbo] .[DatabaseLog] ;
SET SHOWPLAN_XML OFF ;
GO
Figure 7
The link is a pointer to an XML le located here:
The results, even for our simple query, are too large to output
here. I’ll go over them by reviewing various elements and
attributes. The full schema is available here:
http://schemas.microsoft.com/sqlserver/2004/
< StmtSimpleStatementText="SELECT *
FROM [dbo].[DatabaseLog];
" StatementId="1" StatementCompId="1" Statem
< StatementSetOptions QUOTED_IDENTIFIER="fal
< QueryPlan CachedPlanSize="9">
Not only is there more information than in the text plans, but
it’s also much more readily available and easier to read than
in either the text plans or the graphical plans (although the
ow through the graphical plans is much easier to read). For
example, a problematic column, like the De ned List
mentioned earlier, that is di cult to read, becomes the
OutputList element with a list of ColumnReference
elements, each containing a set of attributes to describe
that column:
<OutputList>
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
< ColumnReference Database="[AdventureWorks]
</ OutputList>
This makes XML not only easier to read, but much more
readily translated directly back to the original query.
< DefinedValues>
< DefinedValue>
< ColumnReference Database="[AdventureWorks]
</ DefinedValue>
< DefinedValue>
...<output cropped>........
SQL Server 2005 Pro ler is a powerful tool that allows you to
capture data about events, such as the execution of T-SQL or
a stored procedure, occurring within SQL Server. Pro ler
events can be tracked manually, through a GUI interface, or
traces can be de ned through T-SQL (or the GUI) and
automated to run at certain times and for certain periods.
RPC: Completed
SQL:BatchStarting
SQL:BatchCompleted
Figure 8
Figure 9
Figure 10
At this stage, you can also save the code for this particular
Showplan XML event to a separate le. Simply right-click on
the Showplan XML event you want to save, then select
“Extract Event Data.”
Figure 11
This brings up a dialog box where you can enter the path
and lename of the XML code you want to store. Instead of
storing the XML code with the typical XML extension, the
extension used is .SQLPlan. By using this extension, when
you double-click on the le from within Windows Explorer,
the XML code will open up in Management Studio in the
form of a graphical execution plan.
Summary
In this article we’ve approached how the optimizer and the
storage engine work together to bring data back to your
query. These operations are expressed in the estimated
execution plan and the actual execution plan. You were
given a number of options for obtaining either of these
plans, graphically, output as text, or as XML. Either the
graphical plans or the XML plans will give you all the data
you need, but it’s going to be up to you to decide which to
use and when based on the needs you’re addressing and
how you hope to address them.