Tip
Unlike
the graphical execution plans, SSMS does not provide a way to save the
client statistics. Fortunately, the statistics are displayed using a
standard grid control. You can right-click the client statistics and
choose Select All. Then you right-click and select Copy. You can then
paste the information into a spreadsheet program such as Excel, which
allows you to save the information or perform further statistical
analysis on it.
2. Using the SET SHOWPLAN Options
In addition to the graphical execution plans available in SSMS, SQL Server 2008 provides three SET SHOWPLAN options to display the execution plan information in a text or XML format. These options are SET SHOWPLAN_TEXT, SET SHOWPLAN_ALL, and SET SHOWPLAN_XML.
When one of these options is enabled, SQL Server returns the execution
plan generated for the query, but no results are returned because the
query is not executed. It’s similar to the Display Estimated Execution
Plan option in SSMS.
You can turn on the textual execution plan output in a couple of ways. One way is to issue the SET SHOWPLAN_TEXT ON, SET SHOWPLAN_ALL ON, or SET SHOWPLAN_XML ON
command directly in the SSMS query window. These commands must be
executed in a separate batch by themselves before running a query.
Tip
Before enabling SHOWPLAN_TEXT or SHOWPLAN_ALL options in a Query Editor session in SSMS, be sure to disable the Include Actual Execution Plan option; otherwise, the SHOWPLAN options will have no effect.
SHOWPLAN_TEXT
Typing the following command in an SSMS query window turns on the SHOWPLAN_TEXT option:
Setting this option causes
the textual showplan output to be displayed in the results panel but
does not execute the query. You can also enable the SHOWPLAN_TEXT
option by choosing the Query Options item from the Query menu. In the
Query Options dialog, you click the Advanced item and check the SET SHOWPLAN_TEXT option.
The SHOWPLAN_TEXT option displays a textual representation of the execution plan. Listing 1 shows an example for a simple inner join query.
Tip
When you are displaying the SHOWPLAN_TEXT information in SSMS, it is usually easiest to view if you configure SSMS to return results to text rather than as a grid.
Listing 1. An Example of SHOWPLAN_TEXT Output
set showplan_text on
go
select st.stor_name, ord_date, qty
from stores st join sales_noclust s on st.stor_id = s.stor_id
where st.stor_id between 'B100' and 'B199'
go
StmtText
-------------------------------------------------------------------------------
select st.stor_name, ord_date, qty
from stores st join sales_noclust s on st.stor_id = s.stor_id
where st.stor_id between 'B100' and 'B199'
(1 row(s) affected)
StmtText
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-------------------------
|--Nested Loops(Inner Join, OUTER REFERENCES:([Bmk1002], [Expr1006]) WITH
UNORDERED PREFETCH)
|--Nested Loops(Inner Join, OUTER REFERENCES:([st].[stor_id]))
| |--Clustered Index
Seek(OBJECT:([bigpubs2008].[dbo].[stores].[UPK_storeid]
AS [st]), SEEK:([st].[stor_id] >= 'B100' AND [st].[stor_id] <= 'B199')
ORDERED FORWARD)
| |--Index Seek(OBJECT:([bigpubs2008].[dbo].[sales_noclust].[idx1] AS
[s]), SEEK:([s].[stor_id]=[bigpubs2008].[dbo].[stores].[stor_id] as
[st].[stor_id]), WHERE:([bigpubs2008].[dbo].[sales_noclust].[stor_id] as
[s].[stor_id]>='B100' AND [bigpubs
|--RID Lookup(OBJECT:([bigpubs2008].[dbo].[sales_noclust] AS [s]),
SEEK:([Bmk1002]=[Bmk1002]) LOOKUP ORDERED FORWARD)
(5 row(s) affected)
|
The output is read from
right to left, similarly to the graphical execution plan. Each line
represents a physical/logical operator. The text displayed matches the
logical and physical operator names displayed in the graphical execution
plan. If you can read the graphical query plan, you should have no
trouble reading the SHOWPLAN_TEXT output.
In the example in Listing 1, SQL Server performs a clustered index seek on the stores table, using the UPK_storeid index, and a nonclustered index seek on sales_noclust, using index idx1. The inputs are combined using a nested loop join. Finally, a RID lookup is performed to retrieve the ord_date and qty information from the sales_noclust table.
When the SHOWPLAN_TEXT option is set to ON, execution plan information about all subsequent SQL Server 2008 statements is returned until the option is set to OFF.
Also, all subsequent commands are optimized but not executed. To turn
off the textual showplan output and allow execution of commands again,
type the following command:
Tip
To switch from one SET SHOWPLAN option to another, remember that no commands are executed until the SET SHOWPLAN option is turned off. This includes setting the SET SHOWPLAN options. For example, to switch from SHOWPLAN_TEXT to either SHOWPLAN_ALL or SHOWPLAN_XML, you have to turn off SHOWPLAN_TEXT first with the SET SHOWPLAN_TEXT OFF command.
SHOWPLAN_ALL
The SHOWPLAN_ALL option displays the same textual execution plan information as the SHOWPLAN_TEXT
option, and it also provides additional columns of output for each row
of textual showplan output. These columns provide much of the same
information available in the graphical execution ToolTips. Table 1 describes the information provided in the data columns returned by the SHOWPLAN_ALL option.
Table 1. Data Columns Returned by SHOWPLAN_ALL
Column Name | Description |
---|
StmtText | The
text of the T-SQL statement and also each of the physical operators in
the execution plan. (It may optionally also contain the logical
operators.) |
StmtId | The number of the statement in the current batch. |
NodeId | The ID of the node in the current query. |
Parent | The node ID of the parent operator for the current operator. |
PhysicalOp | Physical operator description for the current node. |
LogicalOp | Logical operator description for the current node. |
Argument | Supplemental information about the operation being performed. |
DefinedValues | A
comma-separated list of values introduced by this operator. These may
be either computed expressions present in the current query or internal
values introduced by the query processor to be able to process this
query. |
EstimateRows | Estimated number of rows of output produced by the operator. |
EstimateIO | Estimated I/O cost for the operator. |
EstimateCPU | Estimated CPU cost for the operator. |
AvgRowSize | Estimated average row size (in bytes) of the row being passed through the operator. |
TotalSubtreeCost | Estimated (cumulative) cost of this operation and all child operations. |
OutputList | A comma-separated list of columns being projected by the current operation. |
Warnings | A comma-separated list of warning messages relating to the current operation (for example, missing statistics). |
Type | The type of node (either PLAN_ROW or the type of T-SQL statement). |
Parallel | Whether the operator is running in parallel (1) or not (0). |
EstimateExecutions | Estimated number of times this operator will be executed while running the current query. |
Tip
When you are displaying the SHOWPLAN_ALL information in SSMS, it is usually easiest to view if you configure SSMS to return results to grid rather than as text.
SHOWPLAN_XML
When SET SHOWPLAN_XML is set to ON,
SQL Server does not execute the query but returns execution information
for each T-SQL batch as an XML document. The execution plan information
for each T-SQL batch is contained in a single XML document. Each XML
document contains the text of the statements in the batch, followed by
the details of the execution steps and operators. The document includes
the estimated costs, numbers of rows, indexes used, join order, and
types of operators performed.
The SHOWPLAN_XML
option generates the same XML output as the Show Estimated Execution
Plan option in SSMS. In essence, you are looking at the same
information, just without the pretty pictures. As a matter of fact, you
can save the output from the SHOWPLAN_XML
option to a file and open it back into SSMS as a SQL plan file. The
recommended approach is to configure the query window to return results
to a grid. If you return the results as text or to a file, the maximum
output size for a character column in SSMS
is 8,192 bytes. If the XML document exceeds this length, it is
truncated and does not load correctly. In the grid results, the maximum
size of XML data is 2MB.
After you run the query and
generate the grid results, you can right-click on the result row and
choose the Save Results As option to specify the file to save the
results to. If all goes well, you end up with a .sqlplan file that you can then load back into SSMS for further analysis at a later date.
Note
The document containing the XML schema for the SET SHOWPLAN_XML output is available in the same directory as the SQL Server installation, which by default is C:\Program Files\Microsoft SQL Server\100\Tools\Binn\schemas\sqlserver\2004\07\showplan\showplanxml.xsd.