统计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,rate = total_worker_time * 100 / @total_run_time,execute_count from @tmptb order by id asc

统计SQL语句耗时百分比

时间: 2024-11-25 07:49:01

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

统计sql语句执行效率

--统计sql语句执行效率SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N'总花费时间ms' ,total_worker_time/1000 N'所用的CPU总时间ms' ,total_physical_reads N'物理读取总次数' ,total_logical_reads/execution_count N'每次逻辑读次数' ,total_logical_reads

sql语句求百分比

此sql语句包括了两个聚合函数做除法求百分比,并保留两位小数,直接输出字符串形式的百分比.以及对case when在聚合函数的应用. SELECT ss.SS_NAME,SS_ID, COUNT(ea.EA_ID) AS EACounts,--回单交换单个数 COUNT(eb.EB_ID) AS EBCounts,--交换单个数 COUNT(ps.PS_ID) AS PSCounts,--签收单个数 COUNT(SW_ID) AS SWCounts,--运单数个数 COUNT(CASE WHEN

sqlserver 统计sql语句大全收藏

SQL统计大全收藏,主要是一些实现统计功能常用的代码,希望对需要的朋友有所帮助. 1.计算每个人的总成绩并排名 select name,sum(score) as allscore from stuscore group by name order by allscore 2.计算每个人的总成绩并排名 select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,( select stuid,sum(score) as allscor

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

学生成绩表(stuscore): 姓名:name 课程:subject 分数:score 学号:stuid 张三 数学 89 1 张三 语文 80 1 张三 英语 70 1 李四 数学 90 2 李四 语文 70 2 李四 英语 80 2 创建表 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[stuscore]( [name] [varchar](50) COLLATE

oracle 资源统计SQL语句

SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)", TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",

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

SELECT GoodID,sum(if(Level = 1, 1, 0)) as Better,sum(if(Level = 0, 1, 0)) as Nomal,sum(if(Level = -1, 1, 0)) as Bad from evaluates GROUP BY GoodID;

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

SELECT TOP 5 total_worker_time , last_worker_time , max_worker_time , min_worker_time , SUBSTRING(st.text, ( qs.statement_start_offset / 2 ) + 1, ( ( CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.sta

Effective MySQL之SQL语句最优化

推荐本SQL优化的书<Effective MySQL之SQL语句最优化>. 主要讲解:如何去分析SQL的性能.索引的原理.如何创建合适的索引.如何去分析线上系统的性能瓶颈. 另外还介绍了几个辅助工具: mysqldumpslow 来分析慢查询日志: Google开源的mysql-slow-query-log-parser 分析慢查询日志: 应用程序中使用MySQL Proxy来收集SQL语句.QEP.查询执行时间: 开源Maatkit检查数据库中的重复索引: Google的MySQL补丁,引入

oracle之 v$sql_monitor 监视正在运行的SQL语句的统计信息

11g中引入了新的动态性能视图V$SQL_MONITOR,该视图用以显示Oracle监视的SQL语句信息.SQL监视会对那些并行执行或者消耗5秒以上cpu时间或I/O时间的SQL语句自动启动,同时在V$SQL_MONITOR视图中产生一条记录.当SQL语句正在执行,V$SQL_MONITOR视图中的统计信息将被实时刷新,频率为每秒1次.SQL语句执行完成后,监视信息将不会被立即删除,Oracle会保证相关记录保存一分钟(由参数_sqlmon_recycle_time所控制,默认为60s),最终这