17.07.2015 Views

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

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 3: Text and XML <strong>Execution</strong> <strong>Plans</strong> for Basic QueriesWe'll use the same execution plan that we evaluated as a text plan. Then, I'll show youhow to retrieve estimated plans from the plan cache and retrieve information out of themdirectly, using XQuery, which is a much more useful exercise.We can issue the SHOWPLAN_XML <strong>com</strong>mand in order to start capturing an estimatedexecution plan in the XML format (remember that any statements that follow the<strong>com</strong>mand will not be executed). We can then execute the required statement and thenimmediately deactivate SHOWPLAN_XML so that <strong>SQL</strong> <strong>Server</strong> will execute any subsequentstatements we issue.SET SHOWPLAN_XML ON;GOSELECT c.CustomerID ,a.City ,s.Name ,st.NameFROM Sales.Customer AS cJOIN Sales.Store AS s ON c.StoreID = s.BusinessEntityIDJOIN Sales.SalesTerritory AS st ON c.TerritoryId = st.TerritoryIDJOIN Person.BusinessEntityAddress AS beaON c.CustomerID = bea.BusinessEntityIDJOIN Person.Address AS a ON bea.AddressID = a.AddressIDJOIN Person.StateProvince AS spON a.StateProvinceID = sp.StateProvinceIDWHERE st.Name = 'Northeast'AND sp.Name = 'New York';GOSET SHOWPLAN_XML OFF;GOListing 3.9Being able to wrap individual statements in this manner is a great way to capture anexecution plan for an isolated statement within the larger set of statements that can makeup many of the more <strong>com</strong>plex queries. When you run the query above, you won't seeresults, but rather a link.128

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

Saved successfully!

Ooh no, something went wrong!