sql 从statspack获取dbtime(秒)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 从statspack获取dbtime(秒)相关的知识,希望对你有一定的参考价值。

select r.*, round(dbtime / elapsed, 2) * 100 rel_dbtime_pct from (
  select t.instid, round((e.value - b.value)/1000000,1) dbtime, 
         extract(day from elapsed_interval) * 86400 + extract(hour from elapsed_interval) * 1440
         + extract(minute from elapsed_interval) * 60 + extract(second from elapsed_interval) as elapsed
    from (select dbid, instance_number instid, min(snap_id) begin_snap, max(snap_id) end_snap, 
                 max(end_interval_time)-min(begin_interval_time) elapsed_interval
            from dba_hist_snapshot group by dbid, instance_number) t
   cross join dba_hist_stat_name sn        
    join dba_hist_sys_time_model e on e.snap_id = t.end_snap and e.dbid = t.dbid and e.instance_number = t.instid and e.stat_id = sn.stat_id
    join dba_hist_sys_time_model b on b.snap_id = t.begin_snap and b.dbid = t.dbid and b.instance_number = t.instid and b.stat_id = sn.stat_id
   where sn.stat_name = 'DB time'
) r;
select r.*, round(dbtime / elapsed, 2) * 100 rel_dbtime_pct from (
  select t.instid, round((e.value - b.value)/1000000,1) dbtime, elapsed
    from (select dbid, instance_number instid, min(snap_id) begin_snap, max(snap_id) end_snap, (max(snap_time)-min(snap_time))*24*60*60 elapsed
            from stats$snapshot group by dbid, instance_number) t
   cross join stats$time_model_statname sn        
    join stats$sys_time_model e on e.snap_id = t.end_snap and e.dbid = t.dbid and e.instance_number = t.instid and e.stat_id = sn.stat_id
    join stats$sys_time_model b on b.snap_id = t.begin_snap and b.dbid = t.dbid and b.instance_number = t.instid and b.stat_id = sn.stat_id
   where sn.stat_name = 'DB time'
) r;
-- Average DB Time over the past week

select instance_number, avg(dbtime_min) avg_dbtime_min
  from (
select a.instance_number, begin_interval_time, end_interval_time,
       round((a.value-lag(a.value) over (order by a.instance_number, a.snap_id ))/1000000/60,2) dbtime_min
  from dba_hist_sys_time_model a, dba_hist_snapshot b
 where a.snap_id = b.snap_id and a.instance_number = b.instance_number 
   and a.stat_name = 'DB time'
)
where begin_interval_time between trunc(sysdate - 7) and trunc(sysdate) and dbtime_min > 0
group by instance_number;

-- DB time per hour over the past week

set pages 100
select d.name, instance_number, to_char(begin_interval_time, 'HH24'), avg(dbtime_min) avg_dbtime_min
  from (
select a.instance_number, begin_interval_time, end_interval_time,
	   round((a.value-lag(a.value) over (order by a.instance_number, a.snap_id ))/1000000/60,2) dbtime_min
  from dba_hist_sys_time_model a, dba_hist_snapshot b
 where a.snap_id = b.snap_id and a.instance_number = b.instance_number 
   and a.stat_name = 'DB time'
), v$database d
where begin_interval_time between trunc(sysdate - 7) and trunc(sysdate) and dbtime_min > 0
group by d.name, instance_number, to_char(begin_interval_time, 'HH24')
order by 2, 3;

-- DB time per day over the past week

select d.name, instance_number, to_char(begin_interval_time, 'D'), avg(dbtime_min) avg_dbtime_min
  from (
select a.instance_number, begin_interval_time, end_interval_time,
	   round((a.value-lag(a.value) over (order by a.instance_number, a.snap_id ))/1000000/60,2) dbtime_min
  from dba_hist_sys_time_model a, dba_hist_snapshot b
 where a.snap_id = b.snap_id and a.instance_number = b.instance_number 
   and a.stat_name = 'DB time'
), v$database d
where begin_interval_time between trunc(sysdate - 7) and trunc(sysdate) and dbtime_min > 0
group by d.name, instance_number, to_char(begin_interval_time, 'D')
order by 2, 3;

以上是关于sql 从statspack获取dbtime(秒)的主要内容,如果未能解决你的问题,请参考以下文章

获取历史执行计划:AWR/StatsPack SQL 报告

sql statspack.sql的日程安排

sql 安装statspack工具和shedules

SSIS MYSQL 到 SQL 数据类型

如何读懂statspack报告

无法从 SQL Server 中的 CURRENT_TIMESTAMP 获取秒数