-- sql语句耗时百分比

declare @tmptb table(id int,name varchar(50),total_worker_time bigint,rate varchar(50),execute_count bigint)

;with cte1 as
(
select a.*,t.*
from sys.dm_exec_query_stats a
cross apply sys.dm_exec_sql_text(a.plan_handle) t
where t.dbid >= 5 
)
,cte2 as
(
select 
t.dbid,db_name(t.dbid) as dbname, 
a.total_worker_time,a.avg_time_ms,a.execution_count,a.cache_count,
replace(replace(t.text,CHAR(10),' '),CHAR(13),' ') as sql_text
from 
(
select 
plan_handle,
sum(total_worker_time) / 1000 as total_worker_time , 
sum(execution_count) as execution_count , 
count(1) as cache_count,
(sum(total_worker_time) / sum(execution_count) ) / 1000 as avg_time_ms
from cte1 
where dbid >= 5
group by plan_handle 
) a 
cross apply sys.dm_exec_sql_text(a.plan_handle) t
)
,cte3 as
(
select 
group_id = 
case 
when avg_time_ms < 50 then 1
when avg_time_ms >= 50 and avg_time_ms <= 200 then 2
when avg_time_ms >= 200 and avg_time_ms <= 500 then 3
when avg_time_ms >= 500 and avg_time_ms <= 1000 then 4
when avg_time_ms >= 1000 and avg_time_ms <= 3000 then 5
when avg_time_ms > 3000 then 6
else 7
end,
group_name = 
case 
when avg_time_ms < 50 then '小于50毫秒'
when avg_time_ms >= 50 and avg_time_ms <= 200 then '50~200毫秒'
when avg_time_ms >= 200 and avg_time_ms <= 500 then '200~500毫秒'
when avg_time_ms >= 500 and avg_time_ms <= 1000 then '500~1000毫秒'
when avg_time_ms >= 1000 and avg_time_ms <= 3000 then '1~3秒'
when avg_time_ms > 3000 then '大于3秒'
else 'unknown'
end,
-- sum(total_worker_time) as total_run_time ,
*
from cte2 
)

insert into @tmptb(id,name,total_worker_time,execute_count)
select 
group_id, group_name,sum(total_worker_time) as total_worker_time,sum(execution_count) as execute_count
from cte3 
group by group_id,group_name

declare @total_run_time bigint
select @total_run_time = sum(total_worker_time) from @tmptb

select id,name,total_worker_time as '总时间','比率' = total_worker_time * 100 / @total_run_time,execute_count as '执行次数' from @tmptb order by id asc

统计SQL语句耗时百分比的更多相关文章

  1. 统计sql语句执行效率

    --统计sql语句执行效率SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N ...

  2. sql语句求百分比

    此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比.以及对case when在聚合函数的应用. SELECT ss.SS_NAME,SS_ID, COUNT(ea ...

  3. 查询sql语句耗时的方法!

    declare @times datetimeset @times=getdate()--要查询的sql语句select [注册数花费时间(毫秒)]=datediff(ms,@times,getdat ...

  4. 学生各门课程成绩统计SQL语句大全

    学生成绩表(stuscore): 姓名:name 课程:subject 分数:score 学号:stuid 张三 数学 89 1 张三 语文 80 1 张三 英语 70 1 李四 数学 90 2 李四 ...

  5. 查询SQLServer 服务器,执行过的SQL语句耗时!

    SELECT creation_time N'语句编译时间',last_execution_time N'上次执行时间',total_physical_reads N'物理读取总次数',total_l ...

  6. mysql 分组统计SQL语句

    1.按照特定字段: 2.在某一日期范围内: 3.按日.按月统计: 4.动态传入数据库表名称. select <if test="dateType=="d"" ...

  7. SQL语句:查看排名前五的SQL语句耗时情况

    total_worker_time , last_worker_time , max_worker_time , min_worker_time , ) , ( ( CASE statement_en ...

  8. MySQL查询某个字段为某值的次数统计SQL语句

    SELECT GoodID,sum(if(Level = 1, 1, 0)) as Better,sum(if(Level = 0, 1, 0)) as Nomal,sum(if(Level = -1 ...

  9. oracle查看执行最慢与查询次数最多的sql语句及其执行速度很慢的问题分析

    oracle查看执行最慢与查询次数最多的sql语句 注:本文来源 于<oracle查看执行最慢与查询次数最多的sql语句> 前言 在ORACLE数据库应用调优中,一个SQL的执行次数/频率 ...

随机推荐

  1. emmc boot1 boot2 partition

    使用mfg tool烧写android5.1的镜像之后,再使用旧版的mfg tool烧写linux或者android镜像,都不能正常启动,而且运行的uboot还是android5.1版本的uboot. ...

  2. 3. 如何封装查询条件与查询结果到map中

    public Map<String, Object> queryOrderStatus(String orderNo) { // 查询到的结果与查询的条件一一对应,封装到map中! Str ...

  3. pages 元素(ASP.NET 设置架构)web.config 详解

    pages 元素(ASP.NET 设置架构)    buffer="[True|False]"   enableEventValidation="[True|False] ...

  4. iOS: 讯飞语音的使用

    一.介绍: 讯飞语音做的相当不错,容错率达到90%多,如果需要做语音方面的功能,它绝对是一个不错的选择.讯飞语音的功能很多:语音听写.语音识别.语音合成等,但我们最常用的还是语音听写.讯飞语音中包含界 ...

  5. python_操作oracle数据库

    1. cx_Oracle Python 连接Oracle 数据库,需要使用cx_Oracle 包. 该包的下载地址:http://cx-Oracle.sourceforge.net/ 下载的时候,注意 ...

  6. 在Matlab中编译c/c++代码需要安装mex

    >> mex -setup Welcome to mex -setup. This utility will help you set up a default compiler. For ...

  7. jsp上传excel文件并导入数据库

    1,excel文件的上传 需要借助jar包:commons-fileupload-1.2.1.jar以及commons-io-1.3.2.jar 前端的html文件 <form id=" ...

  8. SQL exist

    EXISTS = IN,意思相同不过语法上有点点区别,好像使用IN效率要差点,应该是不会执行索引的原因SELECT ID,NAME FROM A WHERE ID IN (SELECT AID FRO ...

  9. SmartGit as SVN Bridge

    This page provides an introduction to SmartGit from an SVN users perspective and shows how the SVN w ...

  10. JQuery实现页面企业广告图片切换和新闻列表滚动效果

    最近用到一个页面上图片左右切换和新闻列表滚动呈现的效果,整理如下: 前段代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...