发布时间:2020-11-30编辑:佚名阅读(2678)
1、查看CPU占用量最高的会话及SQL语句
SELECT spid, cmd, cpu, physical_io, memusage, (SELECT TOP (1) [text] FROM::fn_get_sql(sql_handle)) sql_text FROM master..sysprocesses ORDER BY cpu DESC, physical_io DESC;
2、查看缓存重用次数少,内存占用大的SQL语句
SELECT TOP(100) p.usecounts, p.objtype, p.size_in_bytes, sql.text FROM sys.dm_exec_cached_plans p OUTER APPLY sys.dm_exec_sql_text(p.plan_handle) sql ORDER BY p.usecounts, p.size_in_bytes DESC;
3、执行最慢的SQL语句
SELECT (qs.total_elapsed_time / qs.execution_count) / 1000 N'平均时间ms', qs.total_elapsed_time / 1000 N'总花费时间ms', qs.total_worker_time / 1000 N'所用的CPU总时间ms', qs.total_physical_reads N'物理读取总次数', qs.total_logical_reads / qs.execution_count N'每次逻辑读次数', qs.total_logical_reads N'逻辑读取总次数', qs.total_logical_writes N'逻辑写入总次数', qs.execution_count N'执行次数', SUBSTRING(st.text, (qs.statement_start_offset / 2) + 1, ((CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2) + 1) N'执行语句', qs.creation_time N'语句编译时间', qs.last_execution_time N'上次执行时间' FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st WHERE SUBSTRING(st.text, (qs.statement_start_offset / 2) + 1, ((CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)ELSE qs.statement_end_offset END - qs.statement_start_offset) / 2) + 1)NOT LIKE 'tch%' ORDER BY qs.total_elapsed_time / qs.execution_count DESC;
4、总耗CPU最多的前个SQL
SELECT TOP (20) qs.total_worker_time / 1000 AS [总消耗CPU 时间(ms)], qs.execution_count [运行次数], qs.total_worker_time / qs.execution_count / 1000 AS [平均消耗CPU 时间(ms)], qs.last_execution_time AS [最后一次执行时间], qs.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 [完整语法], qt.dbid, dbname = DB_NAME(qt.dbid), qt.objectid, 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 qs.execution_count > 1 ORDER BY qs.total_worker_time DESC;
5、平均耗CPU最多的前个SQL
SELECT TOP (20) qs.total_worker_time / 1000 AS [总消耗CPU 时间(ms)], qs.execution_count [运行次数], qs.total_worker_time / qs.execution_count / 1000 AS [平均消耗CPU 时间(ms)], qs.last_execution_time AS [最后一次执行时间], qs.min_worker_time / 1000 AS [最小执行时间(ms)], qs.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 [完整语法], qt.dbid, dbname = DB_NAME(qt.dbid), qt.objectid, 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 qs.execution_count > 1 ORDER BY (qs.total_worker_time / qs.execution_count / 1000) DESC;
关键字: Sql Server 资源 占用 情况 查看
0人
0人
0人
0人