SQL SERVER 占用资源高的SQL语句

SQL SERVER 占用资源高的SQL语句
原创曹振华 最后发布于2019-12-03 19:45:05 阅读数 16  收藏
展开
--SQL SERVER 占用资源高的SQL语句:
--查询占用cpu高的前 50 个 SQL 语句
SELECT total_cpu_time,[total_physical_Reads], total_execution_count, number_of_statements, s2.text
      --(SELECT SUBSTRING(s2.text, statement_start_offset / 2, ((CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(NVARCHAR(MAX), s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2) ) AS query_text
FROM 
      (SELECT TOP 50 
            SUM(qs.total_worker_time) AS total_cpu_time, 
            SUM(total_physical_reads) AS [total_physical_Reads], 
            SUM(qs.execution_count) AS total_execution_count,
            COUNT(*) AS  number_of_statements, 
            qs.sql_handle --,
            --MIN(statement_start_offset) AS statement_start_offset, 
            --MAX(statement_end_offset) AS statement_end_offset
      FROM 
            sys.dm_exec_query_stats AS qs
      GROUP BY qs.sql_handle
      ORDER BY SUM(qs.total_worker_time) DESC) AS stats
      CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle) AS s2
--------------------------------------------------------------------
 
--查询物理IO高的前 50 个 SQL 语句
SELECT total_cpu_time, total_physical_Reads , total_execution_count, number_of_statements, s2.text
      --(SELECT SUBSTRING(s2.text, statement_start_offset / 2, ((CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(NVARCHAR(MAX), s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2) ) AS query_text
FROM 
      (SELECT TOP 50 
            SUM(qs.total_worker_time) AS total_cpu_time, 
            SUM(total_physical_reads) AS [total_physical_Reads], 
            SUM(qs.execution_count) AS total_execution_count,
            COUNT(*) AS  number_of_statements, 
            qs.sql_handle --,
            --MIN(statement_start_offset) AS statement_start_offset, 
            --MAX(statement_end_offset) AS statement_end_offset
      FROM 
            sys.dm_exec_query_stats AS qs
      GROUP BY qs.sql_handle
      ORDER BY SUM(qs.total_physical_Reads) DESC) AS stats
      CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle) AS s2
--------------------------------------------------------------------
 
--查看实时数据库占用资源
SELECT st.text, qp.query_plan, rq.* 
FROM sys.dm_exec_requests RQ CROSS APPLY sys.dm_exec_sql_text(rq.sql_handle) as st 
CROSS APPLY sys.dm_exec_query_plan(rq.plan_handle) as qp order by RQ.CPU_time desc
 
————————————————
版权声明:本文为CSDN博主「曹振华」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chadcao/article/details/103375316

posted @ 2020-01-15 08:42  grj001  阅读(254)  评论(0编辑  收藏  举报