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

Posted zhyue93

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过sql的DMV查看数据库使用状态相关的知识,希望对你有一定的参考价值。

--数据库隔离级别 读未提交
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查看数据库使用状态的主要内容,如果未能解决你的问题,请参考以下文章

sql server使用DMV排查系统异常

SQL Server ->> 与SQL Server服务配置相关的DMV

sql 获取数据库文件大小的SQL DMV

DMV Usage Caution

sql 获取用户会话的总CPU时间和总内存使用量。 DMV

sql SQL列表的DMV。