1.SQL Prifiler:捕捉事件类型为SP和T-SQL的事件(Starting、Stmtcompleted、Recompile、Completed、CacheInsert、CacheHit、CacheMiss)可以找到重新编译的原因。
2.SQLServer的系统用于查看分析执行计划的DMV,如:
from sys.dm_exec_cached_plans cp
cross apply sys.dm_exec_text(cp.plan_handle) st order by cp.usecounts desc
--列:st.text:查询中生成查询计划的sql文本。
cp.usecounts:重用计划查询的次数。
cp.size_in_bytes:查询计划中使用的字节数。
cp.cacheobjtype:缓存对象的类型。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 20
CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2))
AS [Total Duration (s)]
, CAST(qs.total_worker_time * 100.0 / qs.total_elapsed_time
AS DECIMAL(28, 2)) AS [% CPU]
, CAST((qs.total_elapsed_time - qs.total_worker_time)* 100.0 /
qs.total_elapsed_time AS DECIMAL(28, 2)) AS [% Waiting]
, qs.execution_count
, CAST(qs.total_elapsed_time / 1000000.0 / qs.execution_count
AS DECIMAL(28, 2)) AS [Average Duration (s)]
, SUBSTRING (qt.text,(qs.statement_start_offset/2) + 1,
((CASE WHEN qs.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2) + 1) AS [Individual Query
, qt.text AS [Parent Query]
, DB_NAME(qt.dbid) AS DatabaseName
, qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE qs.total_elapsed_time > 0
ORDER BY qs.total_elapsed_time DESC
3.虚拟表