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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1: <strong>Execution</strong> Plan Basics<br />

Why the actual and estimated execution plans<br />

might differ<br />

Generally, the estimated and actual execution plans will be the same, in terms of the<br />

operations used. However, circumstances can arise that can cause differences between<br />

the estimated and actual execution plans.<br />

When statistics are stale<br />

The main cause of a difference between the estimated and actual plans is differences<br />

between the statistics and the actual data. This generally occurs over time, as data is<br />

added and deleted. This causes the key values that define the index to change, or their<br />

distribution (how many of what type) to change. The automatic update of statistics that<br />

occurs, assuming it's on, only samples a subset of the data in order to reduce the cost<br />

of the operation. This means that, over time, the statistics can become a less and less<br />

accurate reflection of the actual data. Not only can this cause differences between the<br />

plans, but you can get bad execution plans because the statistical data is not up to date. 5<br />

When the estimated plan is invalid<br />

In some instances, the estimated plan won't work at all. For example, try generating an<br />

estimated plan for the simple bit of code in Listing 1.20, and you will get the error shown<br />

in Listing 1.21.<br />

The optimizer, which is what generates estimated execution plans, doesn't execute<br />

T-<strong>SQL</strong>. It does run the statements through the algebrizer, the process outlined earlier that<br />

is responsible for verifying the names of database objects.<br />

5 An example demonstrating how a drastic change in the data can affect the execution plan is given in the Statistics and indexes<br />

section of Chapter 4.<br />

59

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

Saved successfully!

Ooh no, something went wrong!