2010-05-14 171 views
5

我在SQL Server 2008中有一個數據庫,並且有很多機器正在對它進行查詢。我知道有一個SQL Server分析器,但我不太清楚如何使用它。SQL最執行的查詢?

有沒有什麼辦法知道在數據庫中執行的最常見的查詢是什麼?通過探查器與否,這並不重要。

非常感謝您提前!

回答

20
 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 
+0

這些都是生活儲蓄。可以立即查看哪些查詢無法緩存。 – JakobMillah 2017-05-15 11:07:10