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.

101<br />

Chapter 3: Query Plan Metadata<br />

Each plan has 1-to-N queries and in this section we're going to explore how to get<br />

detailed information about the individual queries in the batch or object. To get these<br />

query statistics, be it for a standalone query or a query <strong>with</strong>in a larger stored procedure<br />

or prepared statement, we use the sys.dm_exec_query_stats DMV.<br />

The sys.dm_exec_query_stats DMV will return one row per query that is executed<br />

<strong>with</strong>in a batch or stored procedure, and provides columns such as total_worker_time<br />

(CPU), total_physical_reads, total_logical_reads, which can give you a very<br />

useful overview of the system resources that your queries are eating up.<br />

Pre-<strong>SQL</strong> <strong>Server</strong> 2005, the only way to get this sort of information was through Profiler.<br />

However, Profiler only lets you collect the information as the queries occur, not after the<br />

event. Now, the next time you hear about a problem second-hand, while walking down<br />

the hallway, which is too often how users report problems, you'll be able to interrogate<br />

the sys.dm_exec_query_stats DMV and find out what happened. This is an<br />

exciting step forward for most DBAs, especially given that resorting to Profiler after the<br />

event often entailed capturing traces over long periods of time, in order to reobserve the<br />

problem, and this could be really costly in terms of server resources and programmer<br />

sanity, since doing the matching of query text after the fact is very difficult.<br />

Overview of sys.dm_exec_query_stats<br />

The sys.dm_exec_query_stats DMV contains quite a few columns that are<br />

incremented counters, and provide information about how many times a query has been<br />

executed and the resources that were used. It is described in Books Online as follows:<br />

Returns aggregate performance statistics for cached query plans. The view contains one row per query<br />

statement <strong>with</strong>in the cached plan, and the lifetime of the rows is tied to the plan itself. When a plan is<br />

removed from the cache, the corresponding rows are eliminated from this view.

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

Saved successfully!

Ooh no, something went wrong!