30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Query Plan Metadata<br />

( ( CASE WHEN deqs.statement_end_offset = -1<br />

THEN DATALENGTH(execText.text)<br />

ELSE deqs.statement_end_offset<br />

END ) - deqs.statement_start_offset ) / 2)<br />

END AS queryText<br />

FROM sys.dm_exec_query_stats deqs<br />

CROSS APPLY sys.dm_exec_text_query_plan(deqs.plan_handle,<br />

deqs.statement_start_offset,<br />

deqs.statement_end_offset)<br />

AS detqp<br />

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

CROSS APPLY sys.dm_exec_sql_text(deqs.plan_handle) AS execText<br />

WHERE deqp.objectid = OBJECT_ID('ShowQueryText', 'p') ;<br />

Listing 3.5: Returning the plan using sys.dm_exec_text_query_plan.<br />

This time, for each row returned, we get the individual plan for each query, as well as the<br />

batch plan, as shown in Figure 3.3.<br />

Figure 3.3: Seeing the individual query plans.<br />

Cached Query Plan Statistics<br />

In the previous section, we explained how to use the sys.dm_exec_query_plan<br />

function and sys.dm_exec_query_stats DMV to get the text of the plan for a given<br />

batch. In this section, we'll start retrieving some meatier information about the query<br />

plans that have been used to execute queries on the server.<br />

86

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

Saved successfully!

Ooh no, something went wrong!