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 8: Advanced Topics<br />

If you rerun Listing 8.9, you'll see that the execution plan is the same as that shown in<br />

Figure 8.10. However, the query stored in the Select operator is not the same. It now<br />

looks as shown in Listing 8.11.<br />

SELECT 42 AS TheAnswer ,<br />

em.EmailAddress ,<br />

e.BirthDate ,<br />

a.City<br />

FROM Person.Person AS p<br />

JOIN HumanResources.Employee e<br />

ON p.BusinessEntityID = e.BusinessEntityID<br />

JOIN Person.BusinessEntityAddress AS bea<br />

ON p.BusinessEntityID = bea.BusinessEntityID<br />

JOIN Person.Address a ON bea.AddressID = a.AddressID<br />

JOIN Person.StateProvince AS sp<br />

ON a.StateProvinceID = sp.StateProvinceID<br />

JOIN Person.EmailAddress AS em<br />

ON e.BusinessEntityID = em.BusinessEntityID<br />

WHERE em.EmailAddress LIKE 'david%'<br />

AND sp.StateProvinceCode = @0<br />

Listing 8.11<br />

Instead of the two-character string we supplied in the original query definition, the<br />

parameter, @0, is used in the comparison to the StateProvinceCode field. This could<br />

seriously affect performance, either positively or negatively. However, this does increase<br />

the likelihood that, if this query is called again with a different two- or three-character<br />

state code, the plan will be reused.<br />

Before proceeding, be sure to reset the parameterization of the AdventureWorks2008R2<br />

database.<br />

ALTER DATABASE AdventureWorks2008R2<br />

SET PARAMETERIZATION SIMPLE<br />

GO<br />

Listing 8.12<br />

309

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

Saved successfully!

Ooh no, something went wrong!