统计SQL语句耗时百分比
2014-08-07 19:48
190 查看
-- 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,rate = total_worker_time * 100 / @total_run_time,execute_count from @tmptb order by id asc
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,rate = total_worker_time * 100 / @total_run_time,execute_count from @tmptb order by id asc
相关文章推荐
- 一条SQL语句统计百分比
- group by分组统计SQL语句
- 考勤统计情况SQL语句
- 分组统计的sql语句
- 统计数据方面的Transact-SQL查询语句
- SQL语句笔记,增加一个字段,统计该表内的数据信息
- sql语句技巧:按月统计数据
- 一个SQL语句实现的统计功能
- 关于统计的一个sql问题,使用动态sql语句实现。
- sql语句中如何查询百分比
- sql 查寻 耗时统计
- SQL语句统计
- SQL语句技巧:按月统计数据
- 用sql语句统计生成(统计项目,值)对
- sql分组统计语句
- 关于数据库统计查询的sql语句
- 分类数量统计SQL语句
- SQL语句技巧:按月统计数据
- 学生各门课程成绩统计SQL语句大全
- 关于统计的一个sql问题,使用动态sql语句实现。