Wednesday, August 8, 2012

dm_exec_query_stats


SELECT TOP 20
        SUBSTRING(text, statement_start_offset/2 + 1,
             (CASE WHEN statement_end_offset = -1
                             THEN LEN(CONVERT(nvarchar(max), text)) * 2
                             ELSE statement_end_offset
                END - statement_start_offset)/2) as SQL,
        db_name(query_text.DBID) as DBNAME, Object_name(query_text.objectid,query_text.DBID) as OBJECTNAME
        ,
        cast(c.query_plan as XML) as queryplan, cast(c.query_plan as XML).exist(
'declare default element namespace ''http://schemas.microsoft.com/sqlserver/2004/07/showplan'';
/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/QueryPlan//MissingIndexes') HASMISSINGINDEX,
        --s.min_worker_time / 1000 AS min_cpu_time_ms,
        --s.max_worker_time / 1000 AS max_cpu_time_ms,
        ((s.total_worker_time * 1.00)/ s.execution_count)/ 1000.00 AS avg_cpu_time_ms,
       
       
        --s.total_elapsed_time / 1000.00 AS total_execution_time_ms,
        --s.min_elapsed_time / 1000.00 AS min_execution_time_ms,
        --s.max_elapsed_time / 1000.00 AS max_execution_time_ms,
          ((s.total_elapsed_time)/ s.execution_count)/ 1000.00 AS avg_execution_time_ms,
       
        --s.total_logical_reads,
        --s.min_logical_reads,
        --s.max_logical_reads,
         ((s.total_logical_reads * 1.00)/ s.execution_count) AS avg_logical_reads,
        
        --s.total_logical_writes,
        --s.min_logical_writes,
        --s.max_logical_writes,
        ((s.total_logical_writes * 1.00)/ s.execution_count) AS avg_logical_writes,
       
       
        --s.total_physical_reads,
        --s.min_physical_reads,
        --s.max_physical_reads,
        ((s.total_physical_reads * 1.00)/ s.execution_count) AS avg_physical_reads,
       
       s.query_hash,
        s.query_plan_hash,
                s.execution_count,
        -- Convert the DMV's ambiguous DATETIME to DATETIMEOFFSET
        TODATETIMEOFFSET (s.creation_time, DATENAME (TZoffset, SYSDATETIMEOFFSET())) AS creation_time,
        s.plan_generation_num,
        TODATETIMEOFFSET (s.last_execution_time, DATENAME (TZoffset, SYSDATETIMEOFFSET())) AS last_execution_time
        --s.total_worker_time / 1000 AS total_cpu_time_ms,
FROM sys.dm_exec_query_stats s
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS query_text
    cross apply sys.dm_exec_text_query_plan(plan_handle, statement_start_offset, statement_end_offset) c
    --where Object_name(query_text.objectid,query_text.DBID) = 'p_Folders_sel_All_Active_with_Hierarchy'
ORDER BY total_worker_time DESC
--ORDER By (s.total_elapsed_time/ s.execution_count) DESC

No comments: