Previous | Table of Contents | Next |
The showplan option allows you to review the optimization plan the server intends to use for your query.
set showplan on go
Once you have done that, you will have changed the characteristics of your session. Now, whenever SQL Server executes a query, it returns the optimization plan along with the results set.
Tip: You need to set the showplan option on in a separate batch before executing the query. Consider this example:/* WARNING: Dont do this. It wont work! */ set showplan on select count(*) from sales where title_id like A% set showplan offEarlier we discussed the sequence of steps each query undergoes. That sequence is actually performed at the batch level, so first the batch is parsed; then the batch is optimized, compiled, and executed. That means that no execution takes place until the entire batch is optimized. At optimization time, when showplan would be relevant, it has not been set. When it is finally set during execution, there is no optimization to report on. Try it!
Heres the optimization plan returned by the server when we executed the sample query:
STEP 1 The type of query is SELECT Scalar Aggregate FROM TABLE sales Nested iteration Using Clustered Index STEP 2 The type of query is SELECT
First of all, the query took two steps because all queries including aggregates (count, max) take two steps, one to build the aggregate, the second to scan it and return the aggregate data. Were interested in STEP 1. Well look at it in detail.
The type of query is SELECT
The server always reports the type of query. This is particularly useful when you submit a long batch consisting of several queries. SQL Server reports the optimization plan for each statement in turn. This element will help you isolate the command you are interested in.
Scalar Aggregate
This particular query is a scalar aggregate (an aggregate without a GROUP BY clause). If a GROUP BY clause had been included, it would be a vector aggregate.
FROM TABLE sales
When a query includes multiple tables, SQL Server reports the names of the tables in the order in which they are accessed. Join order turns out to be very important to performance, especially in large queries. In Day 21 you find out how you can help the performance of the server by looking closely at join order and multitable joins.
Nested iteration
This is the most common method of walking a table. The other method you are likely to see is GETSORTED, which occurs when the output needs to be sorted. Nested iteration is faster.
Using Clustered Index
In this last line is the real point of this exercise. SQL Server used the clustered index to resolve the query.
Tip: Heres another tip about showplan. Often you will combine it with noexec in order to review an optimization plan without needing to wait for the query to execute. This is most common when you are testing queries for a large table or with a large result set. To set the noexec option, typeset noexec onIts very important that you get the sequence correct; the noexec keyword will shut off all query execution for your session, so you want to execute it after showplan:
set showplan on set noexec onOnce you have executed this, the server will report the optimization of every query you send, but the only statement SQL Server will execute is
set noexec offOne more thing: Never run any system stored procedures from a connection where you have set the showplan option. You will be stunned at the amount of information it returns. Instead, when using showplan to analyze queries, open another query session and use that session to execute stored procedures.
Previous | Table of Contents | Next |