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.

109<br />

Chapter 3: Query Plan Metadata<br />

Depending on what columns we include, and which column we order by, we can<br />

discover which cached stored procedures are the most expensive from several different<br />

perspectives. In this case, we are interested in finding out which stored procedures are<br />

generating the most total logical reads (which relates to memory pressure). This query is<br />

especially useful if there are signs of memory pressure, such as a persistently low page life<br />

expectancy and/or persistent values above zero for memory grants pending. This query is<br />

filtered by the current database, but we can change it to be instance-wide by removing the<br />

WHERE clause.<br />

Simply by selecting the total_physical_reads column, instead of total_logical_<br />

reads in this query, we can perform the same analysis from the perspective of physical<br />

reads, which relates to read, disk I/O pressure. Lots of stored procedures <strong>with</strong> high total<br />

physical reads or high average physical reads, could indicate severe memory pressure,<br />

causing <strong>SQL</strong> <strong>Server</strong> to go to the disk I/O subsystem for data. It could also indicate lots of<br />

missing indexes or "bad" queries (<strong>with</strong> no WHERE clauses, for example) that are causing<br />

lots of clustered index or table scans on large tables.<br />

Be aware, though, that there are a couple of caveats <strong>with</strong> these queries. The big one is<br />

that you need to pay close attention to the cached_time column as you compare rows<br />

in the result set. If you have stored procedures that have been cached for different periods<br />

of time, this will skew the results. One easy, but perhaps controversial, solution to this<br />

problem is to periodically clear your procedure cache, by running DBCC FREEPROCCACHE<br />

<strong>with</strong> a <strong>SQL</strong> Agent job, as previously discussed.<br />

The second caveat is that only cached stored procedures will show up in these queries.<br />

If you are using WITH RECOMPILE or OPTION(RECOMPILE), which is usually not a good<br />

idea anyway, then those plans won't be cached.

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

Saved successfully!

Ooh no, something went wrong!