MSSQL慢查询查询与统计

Posted DBArtist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MSSQL慢查询查询与统计相关的知识,希望对你有一定的参考价值。

 

查询MSSQL慢查询:

SELECT TOP 20 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


 SELECT TOP 20 
 total_worker_time/1000 AS [总消耗CPU 时间(ms)],
 execution_count [运行次数],
 qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)],
 last_execution_time AS [最后一次执行时间],
 max_worker_time /1000 AS [最大执行时间(ms)],
 SUBSTRING(qt.text,qs.statement_start_offset/2+1,(CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text)  ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [使用CPU的语法], 
 qt.text [完整语法],
dbname=db_name(qt.dbid),
object_name(qt.objectid,qt.dbid) ObjectName
FROM sys.dm_exec_query_stats qs WITH(nolock)
CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE execution_count>1
ORDER BY total_worker_time DESC

  

以上是关于MSSQL慢查询查询与统计的主要内容,如果未能解决你的问题,请参考以下文章

mssql数据库在磁盘阵列运行慢

Microsoft SQL Server 代码片段收集

MSSQL·查询T-SQL执行记录

MSSQL·查询存储过程中的关键字

MSSQL 查询表空间

sql慢查询工具(配置代码)