• SQL查询运行时间,IO,CPU 前几位的语句


    ;with long_queries as
    (
        select top 20 
            query_hash, 
            sum(total_elapsed_time) elapsed_time
        from sys.dm_exec_query_stats 
        where query_hash <> 0x0
        group by query_hash
        order by sum(total_elapsed_time) desc
    )
    select @@servername as server_name,
        db_name(st.dbid) as database_name,
        object_name(ST.objectid, ST.dbid) as [object_name],
        qs.query_hash,
        qs.total_elapsed_time AS 总运行时间,
        qs.execution_count AS 执行计算,
        cast(total_elapsed_time / (execution_count + 0.0) as money) as 平均持续时间毫秒,
        elapsed_time as 查询的总运行时间,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE 
                WHEN QS.statement_end_offset = -1  THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as sql_text,
        qp.query_plan
    from sys.dm_exec_query_stats qs
    join long_queries lq
        on lq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    order by lq.elapsed_time desc,
        lq.query_hash,
        qs.total_elapsed_time desc
    option (recompile)
    ;with high_cpu_queries as
    (
        select top 20 
            query_hash, 
            sum(total_worker_time) cpuTime
        from sys.dm_exec_query_stats 
        where query_hash <> 0x0
        group by query_hash
        order by sum(total_worker_time) desc
    )
    select @@servername as server_name,
        db_name(st.dbid) as database_name,
        object_name(ST.objectid, ST.dbid) as [object_name],
        qs.query_hash,
        qs.total_worker_time as cpu_time,
        qs.execution_count,
        cast(total_worker_time / (execution_count + 0.0) as money) as average_CPU_in_microseconds,
        cpuTime as total_cpu_for_query,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE 
                WHEN QS.statement_end_offset = -1  THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as sql_text,
        qp.query_plan
    from sys.dm_exec_query_stats qs
    join high_cpu_queries hcq
        on hcq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    order by hcq.cpuTime desc,
        hcq.query_hash,
        qs.total_worker_time desc
    option (recompile)
    ;with frequent_queries as
    (
        select top 20 
            query_hash, 
            sum(execution_count) executions
        from sys.dm_exec_query_stats 
        where query_hash <> 0x0
        group by query_hash
        order by sum(execution_count) desc
    )
    select @@servername as server_name,
        db_name(st.dbid) as database_name,
        object_name(ST.objectid, ST.dbid) as [object_name],
        qs.query_hash,
        qs.execution_count,
        executions as total_executions_for_query,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE 
                WHEN QS.statement_end_offset = -1  THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as sql_text,
        qp.query_plan
    from sys.dm_exec_query_stats qs
    join frequent_queries fq
        on fq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    order by fq.executions desc,
        fq.query_hash,
        qs.execution_count desc
    option (recompile)
    ;with high_io_queries as
    (
        select top 20 
            query_hash, 
            sum(total_logical_reads + total_logical_writes) io
        from sys.dm_exec_query_stats 
        where query_hash <> 0x0
        group by query_hash
        order by sum(total_logical_reads + total_logical_writes) desc
    )
    select @@servername as servername,
        db_name(st.dbid) as database_name,
        object_name(ST.objectid, ST.dbid) as [object_name],
        qs.query_hash,
        qs.total_logical_reads + total_logical_writes as total_io,
        qs.execution_count,
        cast((total_logical_reads + total_logical_writes) / (execution_count + 0.0) as money) as average_io,
        io as total_io_for_query,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE 
                WHEN QS.statement_end_offset = -1  THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as sql_text,
        qp.query_plan
    from sys.dm_exec_query_stats qs
    join high_io_queries fq
        on fq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    order by fq.io desc,
        fq.query_hash,
        qs.total_logical_reads + total_logical_writes desc
    option (recompile)
  • 相关阅读:
    014Linux几种虚拟网络比较
    013Docker几种存储驱动比较(转发)
    005文件系统压测工具iozone
    016SNAT和DNAT区别
    012docker四种网络模式区别
    001CPU个数/物理核数/逻辑核数/vCPU数之间的区别
    009Ubuntu关闭掉终端或jet公司烦人的bell音
    Java 微信小程序imgSecCheck接口示例-校验一张图片是否含有违法违规内容
    提升NginxTLS/SSL HTTPS 性能的7条优化建议
    MySQL复合索引探究
  • 原文地址:https://www.cnblogs.com/weifeng123/p/15571164.html
Copyright © 2020-2023  润新知