SQL执行最多的查询?

时间:2010-05-14 10:37:27

标签: sql

我在SQL Server 2008中有一个数据库,并且有很多机器对它进行查询。我知道有一个SQL Server探查器,但我不太清楚如何使用它。

有没有办法知道在数据库中执行哪些最常见的查询?通过分析器与否,无所谓。

非常感谢你!

1 个答案:

答案 0 :(得分:23)

     SELECT *
       FROM sys.dm_exec_query_stats AS a
CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) AS b 
   ORDER BY execution_count DESC

这将告诉您执行查询的次数以及要执行的最长查询。

不确定这是否方便,但这个SQL会给你最慢的100个查询:

    SELECT TOP 100
            [Object_Name] = object_name(st.objectid),
            creation_time, 
            last_execution_time, 
            total_cpu_time = total_worker_time / 1000, 
            avg_cpu_time = (total_worker_time / execution_count) / 1000,
            min_cpu_time = min_worker_time / 1000,
            max_cpu_time = max_worker_time / 1000,
            last_cpu_time = last_worker_time / 1000,
            total_time_elapsed = total_elapsed_time / 1000 , 
            avg_time_elapsed = (total_elapsed_time / execution_count) / 1000, 
            min_time_elapsed = min_elapsed_time / 1000, 
            max_time_elapsed = max_elapsed_time / 1000, 
            avg_physical_reads = total_physical_reads / execution_count,
            avg_logical_reads = total_logical_reads / execution_count,
            execution_count, 
            SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
                  (
                        (
                              CASE statement_end_offset
                                    WHEN -1 THEN DATALENGTH(st.text)
                                    ELSE qs.statement_end_offset
                              END 
                              - qs.statement_start_offset
                        ) /2
                  ) + 1
            ) as statement_text
       FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st 
   ORDER BY total_worker_time / execution_count DESC