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 />

To get started, you have to have the query for which you're going to create a template. You<br />

will need to use sp_get_query_template again, in order to see the structure of the<br />

query, when parameterized. This will show you where <strong>SQL</strong> <strong>Server</strong> places the parameters.<br />

You can then create the template guide, as shown in Listing 8.17.<br />

EXEC sp_create_plan_guide @name = N'MyThirdPlanGuide',<br />

@stmt = N'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<br />

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 = ''WA'' ;', @type = N'TEMPLATE',<br />

@module_or_batch = NULL, @params = N'@0 VARCHAR(8000)',<br />

@hints = N'OPTION(PARAMETERIZATION FORCED)'<br />

Listing 8.17<br />

Plan guide administration<br />

To see a list of plan guides within the database, just SELECT from the dynamic<br />

management view, sys.plan_guides.<br />

SELECT *<br />

FROM sys.plan_guides<br />

Listing 8.18<br />

316

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

Saved successfully!

Ooh no, something went wrong!