Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jan 24

Written by: Steve Gray
1/24/2011 9:09 AM  RssIcon

Here’s a quick starter on Dynamic Management Views that is available in SQL 2008. I got this from an article by Greg Larsen over at Red-Gate. If you are reading this and don’t subscribe to their Simple-Talk newsletter… you should. The full article is here

Anyway, I didn’t want to risk loosing these two queries, they’re too good.

 

This one looks at the data available in sys.dm_exec_procedure_stats

Code Snippet
  1. SELECT CASE WHEN database_id = 32767 then 'Resource' ELSE DB_NAME(database_id)END AS DBName
  2.         ,OBJECT_SCHEMA_NAME(object_id,database_id) AS [SCHEMA_NAME]
  3.         ,OBJECT_NAME(object_id,database_id)AS [OBJECT_NAME]
  4.         ,*
  5.     FROM sys.dm_exec_procedure_stats

 

This one compares execution times

Code Snippet
  1. SELECT CASE WHEN database_id = 32767 then 'Resource' ELSE DB_NAME(database_id)END AS DBName
  2.         ,OBJECT_SCHEMA_NAME(object_id,database_id) AS [SCHEMA_NAME]
  3.         ,OBJECT_NAME(object_id,database_id)AS [OBJECT_NAME]
  4.         ,cached_time
  5.         ,last_execution_time
  6.         ,execution_count
  7.         ,total_worker_time / execution_count AS AVG_CPU
  8.         ,total_elapsed_time / execution_count AS AVG_ELAPSED
  9.         ,total_logical_reads / execution_count AS AVG_LOGICAL_READS
  10.         ,total_logical_writes / execution_count AS AVG_LOGICAL_WRITES
  11.         ,total_physical_reads / execution_count AS AVG_PHYSICAL_READS
  12.     FROM sys.dm_exec_procedure_stats
  13.     ORDER BY AVG_LOGICAL_READS DESC

 

This one is similar to the last but uses sys.dm_exec_query_stats and sys.dm_exec_cached_plans; these are available in SQL 2005

Code Snippet
  1. SELECT CASE WHEN dbid = 32767 then 'Resource' ELSE DB_NAME(dbid)END AS DBName
  2.         ,OBJECT_SCHEMA_NAME(objectid,dbid) AS [SCHEMA_NAME]
  3.         ,OBJECT_NAME(objectid,dbid)AS [OBJECT_NAME]
  4.         ,MAX(qs.creation_time) AS 'cache_time'
  5.         ,max(last_execution_time) AS 'last_execution_time'
  6.         ,sum(usecounts) AS [execution_count]
  7.         ,SUM(total_worker_time) / SUM(usecounts) AS AVG_CPU
  8.         ,SUM(total_elapsed_time) / SUM(usecounts) AS AVG_ELAPSED
  9.         ,SUM(total_logical_reads) / SUM(usecounts) AS AVG_LOGICAL_READS
  10.         ,SUM(total_logical_writes) / SUM(usecounts) AS AVG_LOGICAL_WRITES
  11.         ,SUM(total_physical_reads) / SUM(usecounts)AS AVG_PHYSICAL_READS
  12.     FROM sys.dm_exec_query_stats qs
  13.         join sys.dm_exec_cached_plans cp on qs.plan_handle = cp.plan_handle
  14.         CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle)
  15.     WHERE objtype = 'Proc'
  16.         AND text
  17.         NOT LIKE '%CREATE FUNC%'
  18.     GROUP BY cp.plan_handle,DBID,objectid

Tags:
Categories:
Location: Blogs Parent Separator DEVSHED Blogs Child Separator SQL
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us