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.

Figure 5.23<br />

Chapter 5: Controlling <strong>Execution</strong> <strong>Plans</strong> with Hints<br />

With a full knowledge of your system that you have from examining the indexes and<br />

statistics on the tables, you know that the plan for the second query should be completely<br />

different because the value passed (288) is much more selective and a useful index exists<br />

on that column. So, you modify the queries using the RECOMPILE hint. In this instance<br />

(Listing 5.25) I'm adding it to both queries so that you can see that the performance gain<br />

in the second query is due to the RECOMPILE and the subsequent improved execution<br />

plan, while the same RECOMPILE on the first query leads to the original plan.<br />

DECLARE @PersonId INT = 277;<br />

SELECT soh.SalesOrderNumber ,<br />

soh.OrderDate ,<br />

soh.SubTotal ,<br />

soh.TotalDue<br />

FROM Sales.SalesOrderHeader soh<br />

WHERE soh.SalesPersonID = @PersonId<br />

OPTION ( RECOMPILE );<br />

SET @PersonId = 288;<br />

SELECT soh.SalesOrderNumber ,<br />

soh.OrderDate ,<br />

soh.SubTotal ,<br />

soh.TotalDue<br />

FROM Sales.SalesOrderHeader soh<br />

WHERE soh.SalesPersonID = @PersonId<br />

OPTION ( RECOMPILE );<br />

Listing 5.25<br />

207

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

Saved successfully!

Ooh no, something went wrong!