30.06.2013 Views

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2: Graphical <strong>Execution</strong> <strong>Plans</strong> for Basic Queries<br />

properties about the index that can be useful in understanding how the operator works<br />

and what it is doing. Some of the properties are self-explanatory, but others might bear<br />

some information. The Estimated I/O Cost and Estimated CPU Cost are measures<br />

assigned by the optimizer, and each operator's cost contributes to the overall cost of the<br />

plan. I'll detail various points on these properties and others that are interesting for each<br />

execution plan as we hit that plan and that operator. Not all the operators will be useful<br />

in figuring out each execution plan. For example, rebinds and rewinds are only important<br />

when dealing with Nested Loops joins, but there are none of those in this plan, so those<br />

values are useless to you.<br />

Indexes in <strong>SQL</strong> <strong>Server</strong> are stored in a balanced-tree, or a b-tree (a series of nodes that<br />

point to a parent). A clustered index not only stores the key structure, like a regular index,<br />

but also sorts and stores the data at the lowest level of the index, known as the leaf,<br />

which is the reason why there can be only one clustered index per table. This means that<br />

a Clustered Index Scan is very similar in concept to a Table Scan. The entire index, or a<br />

large percentage of it, is being traversed, row by row, in order to retrieve the data needed<br />

by the query.<br />

An Index Scan often occurs, as in this case, when an index exists but the optimizer determines<br />

that there are so many rows to return that it is quicker to simply scan all the values<br />

in the index rather than use the keys provided by that index. In other situations, a scan is<br />

necessary because the index is not selective enough for the optimizer to be sure of finding<br />

the values it needs without scanning a large percentage of the index. That can also occur<br />

when the statistics for that index are out of date and showing incorrect information. You<br />

can also have situations where a query applies functions to columns, which means the<br />

optimizer can't determine what the value for that column could be so it has to scan the<br />

entire index to find it.<br />

An obvious question to ask, if you see an Index Scan in your execution plan, is whether<br />

you are returning more rows than is necessary. If the number of rows returned is higher<br />

than you expect, that's a strong indication that you need to fine-tune the WHERE clause<br />

of your query so that only those rows that are actually needed are returned. Returning<br />

unnecessary rows wastes <strong>SQL</strong> <strong>Server</strong> resources and hurts overall performance.<br />

67

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!