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 5: Controlling <strong>Execution</strong> <strong>Plans</strong> with Hints<br />

SELECT *<br />

FROM Person.vStateProvinceCountryRegion;<br />

Listing 5.26<br />

Figure 5.25 shows the resulting execution plan.<br />

Figure 5.25<br />

An indexed view is effectively a clustered index, so this execution plan makes<br />

perfect sense since the data needed to satisfy the query is available in the<br />

materialized view. Things change, as we see in Figure 5.26, if we add the query<br />

hint, OPTION (EXPAND VIEWS).<br />

Figure 5.26<br />

Now we're no longer scanning the indexed view. Within the optimizer, the view has<br />

been expanded into its definition so we see the Clustered Index Scan against the<br />

Person.CountryRegion and Person.StateProvince tables. These are then joined<br />

using a Merge Join, after the data in the StateProvince stream is run through a Sort<br />

operation. The first query ran in about 214ms, but the second ran in about 342ms, so we're<br />

talking a substantial decrease in performance to use the hint in this situation.<br />

211

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

Saved successfully!

Ooh no, something went wrong!