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

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: Query Plan Metadata<br />

Investigating Expensive Cached<br />

Stored Procedures<br />

New to <strong>SQL</strong> <strong>Server</strong> 2008 is the sys.dm_exec_procedure_stats DMV that focuses<br />

only on stored procedures and will not require you to aggregate on sql_handle to get<br />

an overall view of your object's performance characteristics. Books Online describes it as:<br />

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

procedure, and the lifetime of the row is as long as the stored procedure remains cached. When a stored<br />

procedure is removed from the cache, the corresponding row is eliminated from this view. At that time, a<br />

<strong>Performance</strong> Statistics <strong>SQL</strong> trace event is raised similar to sys.dm_exec_query_stats.<br />

It is similar in nature to sys.dm_exec_query_stats, but <strong>with</strong> a few differences in the<br />

columns available. It only links to the plan of the procedure or object, not the individual<br />

statements in the procedure. This DMV allows you to discover a lot of very interesting<br />

and important performance information about your cached stored procedures.<br />

-- Top Cached SPs By Total Logical Reads (<strong>SQL</strong> 2008 only).<br />

-- Logical reads relate to memory pressure<br />

SELECT TOP ( 25 )<br />

p.name AS [SP Name] ,<br />

deps.total_logical_reads AS [TotalLogicalReads] ,<br />

deps.total_logical_reads / deps.execution_count AS [AvgLogicalReads] ,<br />

deps.execution_count ,<br />

ISNULL(deps.execution_count / DATEDIFF(Second, deps.cached_time,<br />

GETDATE()), 0) AS [Calls/Second] ,<br />

deps.total_elapsed_time ,<br />

deps.total_elapsed_time / deps.execution_count AS [avg_elapsed_time] ,<br />

deps.cached_time<br />

FROM sys.procedures AS p<br />

INNER JOIN sys.dm_exec_procedure_stats<br />

AS deps ON p.[object_id] = deps.[object_id]<br />

WHERE deps.database_id = DB_ID()<br />

ORDER BY deps.total_logical_reads DESC ;<br />

Listing 3.16: Investigating logical reads performed by cached stored procedures.<br />

108

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

Saved successfully!

Ooh no, something went wrong!