IT tutorials
 
Database
 

SQL Server 2008 R2 : Query Analysis - SSMS Client Statistics, Using the SET SHOWPLAN Options

12/5/2012 5:46:06 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

1. SSMS Client Statistics

You can use SSMS to get some additional information related to the client-side performance of the query by toggling the Include Client Statistics option in the Query menu. When turned on, the Client Statistics tab is added to the Results panel. This tab displays useful performance statistics in a tabular format that is related to how much work the client had to do to submit the query and process the results, including statistics about the network packets and elapsed time of the query.

SSMS keeps track of the statistics for previous executions within a session so that you can compare the statistics between different query executions. It also keeps track of the overall average statistics across all executions. Figure 1 shows an example of the client statistics displayed after three separate query executions.

Figure 1. SSMS client statistics.

The first line in the Client Statistics tab displays the actual time the query was executed. The Time Statistics values are specified in number of milliseconds. Some of the most useful pieces of information include the number of rows returned by SELECT statements, total client processing time, total execution time, and number of bytes sent and received across the network.

The Average column contains the cumulative average since the Include Client Statistics option was enabled. Turning the option off and back on clears out all the historical statistics and resets the averages. Alternatively, you can also reset the client statistics by selecting the Reset Client Statistics option from the Query menu.

One of the most helpful features of the client statistics is the arrow indicators provided for the different executions, which makes it easy to identify which values increased, decreased, or stayed this same. This feature makes it easy to compare the runtime statistics between different queries or different executions of the same query.

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:

SET SHOWPLAN_TEXT ON
GO

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:

SET SHOWPLAN_TEXT OFF
GO

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 NameDescription
StmtTextThe 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.)
StmtIdThe number of the statement in the current batch.
NodeIdThe ID of the node in the current query.
ParentThe node ID of the parent operator for the current operator.
PhysicalOpPhysical operator description for the current node.
LogicalOpLogical operator description for the current node.
ArgumentSupplemental information about the operation being performed.
DefinedValuesA 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.
EstimateRowsEstimated number of rows of output produced by the operator.
EstimateIOEstimated I/O cost for the operator.
EstimateCPUEstimated CPU cost for the operator.
AvgRowSizeEstimated average row size (in bytes) of the row being passed through the operator.
TotalSubtreeCostEstimated (cumulative) cost of this operation and all child operations.
OutputListA comma-separated list of columns being projected by the current operation.
WarningsA comma-separated list of warning messages relating to the current operation (for example, missing statistics).
TypeThe type of node (either PLAN_ROW or the type of T-SQL statement).
ParallelWhether the operator is running in parallel (1) or not (0).
EstimateExecutionsEstimated 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.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us