在上3篇文章裏,咱們討論了列出反映服務器當前狀態的不一樣查詢。html
這篇文章咱們看下從計劃緩存裏列出執行狀態。sql
1 /***************************************************************************************** 2 List heavy query based on CPU/IO. Change the order by clause appropriately 3 ******************************************************************************************/ 4 SELECT TOP 20 5 DB_NAME(qt.dbid) AS DatabaseName 6 ,DATEDIFF(MI,creation_time,GETDATE()) AS [Age of the Plan(Minutes)] 7 ,last_execution_time AS [Last Execution Time] 8 ,qs.execution_count AS [Total Execution Count] 9 ,CAST((qs.total_elapsed_time) / 1000000.0 AS DECIMAL(28,2)) [Total Elapsed Time(s)] 10 ,CAST((qs.total_elapsed_time ) / 1000000.0/ qs.execution_count AS DECIMAL(28, 2)) AS [Average Execution time(s)] 11 ,CAST((qs.total_worker_time) / 1000000.0 AS DECIMAL(28,2)) AS [Total CPU time (s)] 12 ,CAST(qs.total_worker_time * 100.0 / qs.total_elapsed_time AS DECIMAL(28,2)) AS [% CPU] 13 ,CAST((qs.total_elapsed_time - qs.total_worker_time)* 100.0 /qs.total_elapsed_time AS DECIMAL(28, 2)) AS [% Waiting] 14 ,CAST((qs.total_worker_time) / 1000000.0/ qs.execution_count AS DECIMAL(28, 2)) AS [CPU time average (s)] 15 ,CAST((qs.total_physical_reads) / qs.execution_count AS DECIMAL(28, 2)) AS [Avg Physical Read] 16 ,CAST((qs.total_logical_reads) / qs.execution_count AS DECIMAL(28, 2)) AS [Avg Logical Reads] 17 ,CAST((qs.total_logical_writes) / qs.execution_count AS DECIMAL(28, 2)) AS [Avg Logical Writes] 18 ,max_physical_reads 19 ,max_logical_reads 20 ,max_logical_writes 21 , SUBSTRING (qt.TEXT,(qs.statement_start_offset/2) + 1,((CASE WHEN qs.statement_end_offset = -1 22 THEN LEN(CONVERT(NVARCHAR(MAX), qt.TEXT)) * 2 23 ELSE qs.statement_end_offset 24 END - qs.statement_start_offset)/2) + 1) AS [Individual Query] 25 , qt.TEXT AS [Batch Statement] 26 , qp.query_plan 27 FROM SYS.DM_EXEC_QUERY_STATS qs 28 CROSS APPLY SYS.DM_EXEC_SQL_TEXT(qs.sql_handle) AS qt 29 CROSS APPLY SYS.DM_EXEC_QUERY_PLAN(qs.plan_handle) qp 30 WHERE qs.total_elapsed_time > 0 31 ORDER BY 32 [Total CPU time (s)] 33 --[Avg Physical Read] 34 --[Avg Logical Reads] 35 --[Avg Logical Writes] 36 --[Total Elapsed Time(s)] 37 --[Total Execution Count] 38 DESC
輸出結果的每列說明介紹以下:數據庫
通常咱們能夠分析前5條記錄(經過修改排序規則)的具體語句信息。大多數狀況,咱們會發現問題出如今臨時表的濫用,distinct語句,遊標,不合適的錶鏈接條件,不合適的索引等等。其餘常常發生的問題是,存儲過程對數據庫的大量調用(CPU消耗和執行時間都很小)。這個須要和開發人員反饋,修改下具體的實現方式。若是數據常常被調用,能夠在程序裏使用緩存方法避免與服務器的屢次交互。有些對數據庫的調用只是檢查結果數據是否有改變。有些對數據庫的調用是爲檢查數據庫表裏是否有新記錄,且必須立刻處理的。爲了完成這些操做,程序會在1秒內屢次查詢表來找出未處理的記錄。這個能夠經過程序的異步調用來往表裏插入數據來解決,或能夠使用.net框架裏的sqlDependency來解決。(sqlDependency提供了這樣一種能力:當被監測的數據庫中的數據發生變化時,SqlDependency會自動觸發OnChange事件來通知應用程序,從而達到讓系統自動更新數據(或緩存)的目的。)緩存