sql server 查询性能最差的sql语句

时间:2022-06-21 11:16:43

查询性能最差的sql语句:
SELECT TOP 10 TEXT AS 'SQL Statement'
    ,last_execution_time AS 'Last Execution Time'
    ,(total_logical_reads + total_physical_reads + total_logical_writes) / execution_count AS [Average IO]
    ,(total_worker_time / execution_count) / 1000000.0 AS [Average CPU Time (sec)]
    ,(total_elapsed_time / execution_count) / 1000000.0 AS [Average Elapsed Time (sec)]
    ,execution_count AS "Execution Count"
    ,qp.query_plan AS "Query Plan"
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY total_elapsed_time / execution_count DESC

  延申说明:

DBCC DROPCLEANBUFFERS :从缓冲池中删除所有缓冲区

DBCC FREEPROCCACHE :从执行计划缓冲区删除所有缓存的执行计划

另外还有清理表索引也会影响 sys.dm_exec_query_stats 表的统计,

所以当同一个sql语句经过一段时间后反而 执行的次数变少了,可以从以上三点进行排查