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.

Dissecting the <strong>SQL</strong> text<br />

83<br />

Chapter 3: Query Plan Metadata<br />

In Chapter 2, we saw how to use the sql_handle from sys.dm_exec_requests to<br />

return the <strong>SQL</strong> text for the batch, from sys.dm_exec_sql_text, and then to dissect<br />

the text using the "statement offset" columns provided by the former, to get at the text of<br />

just the currently executing query.<br />

We'll have to do the same thing when using sys.dm_exec_query_stats, in order to<br />

extract query statistics about individual statements <strong>with</strong>in a cached batch/procedure.<br />

Let's take a look at the sql_handle, plan_handle and associated <strong>SQL</strong> text, returned<br />

for each query in our stored procedure, as shown in Listing 3.3.<br />

SELECT deqs.plan_handle ,<br />

deqs.sql_handle ,<br />

execText.text<br />

FROM sys.dm_exec_query_stats deqs<br />

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

WHERE execText.text LIKE 'CREATE PROCEDURE ShowQueryText%'<br />

Listing 3.3: Viewing the sql_handle and plan_handle.<br />

As you can see, each row has the same sql_handle, same plan_handle and the same<br />

<strong>SQL</strong> text (referring to the whole procedure). I'm only showing two of the rows here:<br />

plan_handle sql_handle text<br />

-------------- ------------- -----------------<br />

0x050002003... 0x03000200... CREATE PROCEDURE<br />

ShowQueryText as…<br />

0x050002003... 0x03000200... CREATE PROCEDURE<br />

ShowQueryText as…

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

Saved successfully!

Ooh no, something went wrong!