统计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的执行次数/频率 ...
随机推荐
- iOS:使用Github托管自己本地的项目代码方式三(命令行方式: Terminal Line)
使用终端命令行将本地项目代码上传到github上进行托管 对于IOS开发者来说,Github的使用是必须要掌握的一种技能,而把项目由本地上传到Github有多种方式 1.开发工具Xcode配置Git, ...
- Android Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
今天项目发布时遇到了这个问题,在低版本设备上面死活发布不上去,还有打包也打不成功,折腾了好长一段时间,网上大部分给出的 解决方案都是说 在工程的混淆配置文件 proguard-rules.pro 中加 ...
- proguard使用
Proguard用于混淆java代 码,使代码变为由难懂的,无规律的字符命名的各种方法和类,保护自己的劳动成果.个人认为proguard混淆纯java项目比较理想,比如j2me的 MIDLET,如 ...
- Redis一些基本的操作
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- 使用Html来避免写复杂的app代码,跨平台
http://www.jianshu.com/p/c375ac056149 http://www.php.net.cn/app/
- curl获得http响应码 302 和绑定host
shell curl 取得HTTP返回的状态 获取状态码 curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com 获取时间 curl ...
- 微信成为开发者C#代码
第一句话都会这么去写:程序猿就是苦逼,除了开发还要会写博文!今天咱就和大家探讨下如何让自己成为开发者!那么怎么才能成为开发者呢? 首先给大家一个微信的截图,看到这个截图,是不是有想去尝试的冲动?
- 使用duplicate target database ... from active database复制数据库
使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...
- jQuery中的siblings
所谓siblings,英文翻译就是兄弟节点.那么故名思意,就是拿到某元素的兄弟节点(不包括自己). <html> <head> <script type="te ...
- Java基础之访问文件与目录——创建目录(CreatingDirectories)
控制台程序,使用两种方法来创建目录. import java.nio.file.*; import java.io.IOException; public class CreatingDirector ...