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 3: Text and XML <strong>Execution</strong> <strong>Plans</strong> for Basic Queries<br />

The simple example in Listing 3.18 returns a list of operators within queries currently<br />

stored in the plan cache. It illustrates how we can construct queries against the plan<br />

cache, using the DMVs, but I would still hesitate before running this query on a<br />

production system if that system was already under stress. Exercise caution using these<br />

XML queries.<br />

SELECT TOP 3<br />

RelOp.op.value('declare default element namespace "http://schemas.<br />

microsoft.com/sqlserver/2004/07/showplan";<br />

@PhysicalOp', 'varchar(50)') AS PhysicalOp,<br />

dest.text,<br />

deqs.execution_count,<br />

RelOp.op.value('declare default element namespace "http://schemas.<br />

microsoft.com/sqlserver/2004/07/showplan";<br />

@EstimatedTotalSubtreeCost', 'float') AS EstimatedCost<br />

FROM sys.dm_exec_query_stats AS deqs<br />

CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest<br />

CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp<br />

CROSS APPLY deqp.query_plan.nodes('declare default element namespace<br />

"http://schemas.microsoft.com/sqlserver/2004/07/showplan";<br />

//RelOp') RelOp (op)<br />

ORDER BY deqs.execution_count DESC<br />

Listing 3.18<br />

This query returns the first three operators from the most frequently called query in<br />

the plan cache. The basic construct is easy enough to follow. In the FROM clause, three<br />

DMVs are referenced, sys.dm_exec_query_stats, sys.dm_exec_sql_text, and<br />

sys.dm_exec_query_plan, along with a reference to the query_plan column<br />

within sys.dm_exec_query_plan and to the .nodes of the XML. Apart from that,<br />

it's all XQuery. The results from my system look as shown in Figure 3.10.<br />

136

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

Saved successfully!

Ooh no, something went wrong!