码迷,mamicode.com
首页 > 数据库 > 详细

通过sql的DMV查看数据库使用状态

时间:2019-01-28 19:19:25      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:read   action   object   offset   char   cal   cas   reads   end   

--数据库隔离级别 读未提交
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

--查找每次执行时引发I/O最多的前10位的查询
SELECT TOP 10
        total_logical_reads / execution_count avg_logical_reads ,
        total_logical_writes / execution_count avg_logical_writes ,
        total_physical_reads / execution_count avg_physical_reads ,
        execution_count ,
        ( SELECT    SUBSTRING(text, statement_start_offset / 2 + 1,
                              CASE WHEN statement_end_offset = -1
                                   THEN LEN(CONVERT(NVARCHAR(MAX), text)) * 2
                                   ELSE statement_end_offset
                              END - statement_start_offset / 2)
          FROM      sys.dm_exec_sql_text(sql_handle)
        ) query_text ,
        plan_handle
FROM    sys.dm_exec_query_stats
ORDER BY total_logical_reads + total_logical_writes DESC;

--查找查询消耗时间最长的前20个查询语句
SELECT TOP 20
        CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2)) [Total Elapsed Duration(s)] ,
        qs.execution_count ,
        ( SUBSTRING(text, statement_start_offset / 2 + 1,
                    CASE WHEN statement_end_offset = -1
                         THEN LEN(CONVERT(NVARCHAR(MAX), text)) * 2
                         ELSE statement_end_offset
                    END - statement_start_offset / 2) ) query_text ,
        qp.query_plan
FROM    sys.dm_exec_query_stats qs
        CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
        CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_elapsed_time DESC;

--缓存计划所占用的 CPU 总使用率(带 SQL 文本)
SELECT  total_cpu_time ,
        total_execution_count ,
        number_of_statements ,
        s2.text
FROM    ( SELECT TOP 20
                    SUM(qs.total_worker_time) AS total_cpu_time ,
                    SUM(qs.execution_count) AS total_execution_count ,
                    COUNT(*) AS number_of_statements ,
                    qs.sql_handle --,
          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;
        
--CPU 平均占用率最高的前 20 个 SQL 语句
SELECT TOP 20
        total_worker_time / execution_count AS [Avg CPU Time] ,
        ( SELECT    SUBSTRING(text, statement_start_offset / 2,
                              ( CASE WHEN statement_end_offset = -1
                                     THEN LEN(CONVERT(NVARCHAR(MAX), text))
                                          * 2
                                     ELSE statement_end_offset
                                END - statement_start_offset ) / 2)
          FROM      sys.dm_exec_sql_text(sql_handle)
        ) AS query_text ,
        *
FROM    sys.dm_exec_query_stats
ORDER BY [Avg CPU Time] DESC;

--哪个查询占用了最多的 CPU 累计使用率
SELECT  highest_cpu_queries.plan_handle ,
        highest_cpu_queries.total_worker_time ,
        q.dbid ,
        q.objectid ,
        q.number ,
        q.encrypted ,
        q.[text]
FROM    ( SELECT TOP 20
                    qs.plan_handle ,
                    qs.total_worker_time
          FROM      sys.dm_exec_query_stats qs
          ORDER BY  qs.total_worker_time DESC
        ) AS highest_cpu_queries
        CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS q
ORDER BY highest_cpu_queries.total_worker_time DESC;

 

通过sql的DMV查看数据库使用状态

标签:read   action   object   offset   char   cal   cas   reads   end   

原文地址:https://www.cnblogs.com/zhyue93/p/sql_state.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!