统计SQL语句耗时百分比
-- 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语句耗时百分比的更多相关文章
- 统计sql语句执行效率
--统计sql语句执行效率SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N ...
- sql语句求百分比
此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比.以及对case when在聚合函数的应用. SELECT ss.SS_NAME,SS_ID, COUNT(ea ...
- 查询sql语句耗时的方法!
declare @times datetimeset @times=getdate()--要查询的sql语句select [注册数花费时间(毫秒)]=datediff(ms,@times,getdat ...
- 学生各门课程成绩统计SQL语句大全
学生成绩表(stuscore): 姓名:name 课程:subject 分数:score 学号:stuid 张三 数学 89 1 张三 语文 80 1 张三 英语 70 1 李四 数学 90 2 李四 ...
- 查询SQLServer 服务器,执行过的SQL语句耗时!
SELECT creation_time N'语句编译时间',last_execution_time N'上次执行时间',total_physical_reads N'物理读取总次数',total_l ...
- mysql 分组统计SQL语句
1.按照特定字段: 2.在某一日期范围内: 3.按日.按月统计: 4.动态传入数据库表名称. select <if test="dateType=="d"" ...
- SQL语句:查看排名前五的SQL语句耗时情况
total_worker_time , last_worker_time , max_worker_time , min_worker_time , ) , ( ( CASE statement_en ...
- MySQL查询某个字段为某值的次数统计SQL语句
SELECT GoodID,sum(if(Level = 1, 1, 0)) as Better,sum(if(Level = 0, 1, 0)) as Nomal,sum(if(Level = -1 ...
- oracle查看执行最慢与查询次数最多的sql语句及其执行速度很慢的问题分析
oracle查看执行最慢与查询次数最多的sql语句 注:本文来源 于<oracle查看执行最慢与查询次数最多的sql语句> 前言 在ORACLE数据库应用调优中,一个SQL的执行次数/频率 ...
随机推荐
- python 递归
学习python,正好用一个例子练习一下递归. 参考文档: http://www.runoob.com/python/python-exercise-example18.html 题目:求s=a+aa ...
- Swift闭包
把Swift中的 block 常见的声明和写法作一个总结.以免后续忘了,好查阅. // // blockDemo.swift // swiftDemo // // Created by appl ...
- RFS_javascript的使用
1. RFS对javascript的调用 (1)调用javascript语句 (2)调用javascript函数 2. IE8不支持javascript的 getElementsByClassName ...
- TCP 状态图网摘
from unkonwn 1.CLOSED:起始点,在超时或者连接关闭时候进入此状态. 2.LISTEN:svr端在等待连接过来时候的状态,svr端为此要调用socket, bind,listen函数 ...
- struts标签小记
1.<s:iterator>标签的 奇偶数行使用不同样式 <s:iterator id="list" value="#request.listq&qu ...
- maven中下载jar包源码和javadoc
1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...
- jquery获取自身元素的html
在开发过程中,jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML" ...
- 企业Openvpn环境部署
企业Openvpn环境部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任 ...
- .net开发之我见,or实现 最简 优化法。knock out type convert 与我之简化orm的实现原理及实现细则,最简化开发法
现在的.net or构架,大家认同的各种大大小小,ef,subsonic,nhibernate,甚至小一些的petapoco这种,但用过的人我想他们考虑的是比较多. 小一些的Petapoco也有几千行 ...
- CentOS 7 内核更新后删除旧内核
0.当前 # uname -sr Linux -.el7.x86_64 1.搜索查询 # rpm -q kernel kernel--.el7.x86_64 kernel--.el7.x86_64 k ...