oracle 10g awr 報告中內容所對應的sql

tianya_2011發表於2017-03-07
工作中喜歡用awr報告,但之前也只是知道里面內容的來源主要對應dba_hist_下面的檢視,
最近幾天透過使用Fuyuncat(Wei Huang)的解密工具,把awr報告中內容實現的sql整理了出來,分享給大家.
如大家喜歡,後續會再把ash的整理出來.
---------------------------------dbid,name,instance_name,instance_name,version,rac,host_name
select dbid,name,instance_name,instance_name,version,parallel rac,host_name from v$database,v$instance;
--------------------------下面語句的開始結束時間要自己根據情況改一下.
SELECT b.snap_id,b.dbid,b.instance_number,
      B.END_INTERVAL_TIME ,
      e.snap_id,e.dbid,e.instance_number,
      E.END_INTERVAL_TIME ,
      EXTRACT(DAY FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 1440
       + EXTRACT(HOUR FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 60
       + EXTRACT(MINUTE FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME)
       + EXTRACT(SECOND FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) / 60 STAT_ELAMIN,
      EXTRACT(DAY FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 86400
       + EXTRACT(HOUR FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 3600
       + EXTRACT(MINUTE FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 60
       + EXTRACT(SECOND FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) STAT_ELAPSED
     FROM DBA_HIST_SNAPSHOT B, DBA_HIST_SNAPSHOT E
     where to_date('2014-08-14 14:00:04','yyyy-mm-dd hh24:mi:ss') between b.begin_interval_time and b.end_interval_time
     and to_date('2014-08-14 15:00:00','yyyy-mm-dd hh24:mi:ss') between e.begin_interval_time and e.end_interval_time;
-----------以下注釋的變數值來自於上條語句,現有值只是例子
--b_snap_id=53905
--e_snap_id=53906
--b_dbid=1792101802
--e_dbid=1792101802
--b_INSTANCE_NUMBER=1
--e_INSTANCE_NUMBER=1
--B_END_INTERVAL_TIME=to_date('2014-08-14 14:00:04','yyyy-mm-dd hh24:mi:ss')
--E_END_INTERVAL_TIME=to_date('2014-08-14 15:00:32','yyyy-mm-dd hh24:mi:ss')
--STAT_ELAMIN=60.4615333333333
--ela=STAT_ELAPSED=3627.692
---------------------------DB time,DB CPU
SELECT stat_name,sum(case when snap_id=:e_snap_id then VALUE else -value end)/1000000
  FROM DBA_HIST_SYS_TIME_MODEL
   WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
  AND DBID            = :b_dbid
  AND INSTANCE_NUMBER = 1  
  and stat_name in ('DB time','DB CPU')
  group by stat_name;
-----------以下注釋的變數值來自於上條語句,現有值只是例子  
--db_time=DB time=14268.180268
--db_cpu=DB CPU=7422.706771
------------------------後面語句中要用到的值
        SELECT stat_name,sum(case when snap_id=:e_snap_id then VALUE else -value end)
        FROM DBA_HIST_SYSSTAT
       WHERE SNAP_ID         in (:b_snap_id,:e_snap_id)
         AND DBID            = :b_dbid
         AND INSTANCE_NUMBER = 1
         and stat_name in ('user rollbacks','user commits','session logical reads','physical reads',
         'execute count','parse count (total)')
         group by stat_name;
-----------以下注釋的變數值來自於上條語句,現有值只是例子
--tran=user rollbacks+user commits=51274
--gets=session logical reads=563716259
--phyr=physical reads=56635028         
--exe=execute count=396277
--prse=parse count (total)=374631   
---------------------------------------------------------Cache Sizes
-------------------------------------- Std Block Size,Shared Pool Size
    SELECT snap_id,PARAMETER_NAME,value
          FROM DBA_HIST_PARAMETER
          WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
            AND DBID            = :b_dbid
            AND INSTANCE_NUMBER = 1
            and parameter_name in ('db_block_size','__shared_pool_size');
-----------以下注釋的變數值來自於上條語句,現有值只是例子
--b_spm=__shared_pool_size=369098752
--e_spm=__shared_pool_size=352321536
--bs=db_block_size=8192
---------------------------------------------Buffer Cache,Log Buffer
SELECT SNAP_ID,name,BYTES/1024/1024
          FROM DBA_HIST_SGASTAT
         WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
           AND DBID            = :b_dbid
           AND INSTANCE_NUMBER = 1
           AND POOL            IS NULL
           order by name,snap_id;

---------------------------------------------------------Load Profile
    SELECT stat_name,sum(case when snap_id=:e_snap_id then VALUE else -value end)/:ela,
       sum(case when snap_id=:e_snap_id then VALUE else -value end)/:tran
        FROM DBA_HIST_SYSSTAT
       WHERE SNAP_ID         in (:b_snap_id,:e_snap_id)
         AND DBID            = :b_dbid
         AND INSTANCE_NUMBER = 1
         and stat_name in ('redo size','session logical reads','db block changes','physical reads','physical writes','user calls',
         'parse count (total)','parse count (hard)','sorts (memory)','logons cumulative','execute count',
         'user commits','user rollbacks')
        group by STAT_NAME ;
----------------------------------------------Instance Efficiency Percentages (Target 100%)
      SELECT stat_name,sum(case when snap_id=:e_snap_id then VALUE else -value end)
        FROM DBA_HIST_SYSSTAT
       WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
            AND DBID            = :b_dbid
            AND INSTANCE_NUMBER = 1         
        group by STAT_NAME ;
        -----------------下面公式中的名稱對應上條語句的stat_name
Buffer Hit %:=1-(physical reads-physical reads direct-physical reads direct (lob))/(consistent gets from cache+db block gets)
Soft Parse %:=(parse count (total)-parse count (hard))/parse count (total)
In-memory Sort %:=sorts (memory)/(sorts (disk)+sorts (memory))
Parse CPU to Parse Elapsd %:=parse time cpu/parse time elapsed
Execute to Parse %:=1-parse count (total)/execute count
Redo NoWait %:=1-redo log space requests/redo entries
% Non-Parse CPU:=1-parse time cpu/CPU used by this session
         -----------------Library Hit %:
          SELECT  sum(case when snap_id=:e_snap_id then PINHITS else -PINHITS end)/
         sum(case when snap_id=:e_snap_id then PINS else -PINS end) Library_Hit
           FROM DBA_HIST_LIBRARYCACHE
          WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
            AND DBID            = :b_dbid
            AND INSTANCE_NUMBER = 1;
Library Hit %:=Library_Hit
        ---------------Latch Hit %:
          SELECT  1-SUM(case when snap_id=:e_snap_id then MISSES else -MISSES end)/  
          SUM(case when snap_id=:e_snap_id then GETS else -GETS end) Latch_Hit
          FROM DBA_HIST_LATCH
          WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
            AND DBID            = :b_dbid
            AND INSTANCE_NUMBER = 1;
Latch Hit %:=Latch_Hit
        ----------------Buffer Nowait %:
          SELECT sum(case when snap_id=:e_snap_id then WAIT_COUNT else -WAIT_COUNT end) buffer_busy_wait
          FROM DBA_HIST_WAITSTAT
          WHERE SNAP_ID         in( :b_snap_id,:e_snap_id)
            AND DBID            = :b_dbid
            AND INSTANCE_NUMBER = 1  ;
Buffer Nowait %:=buffer_busy_wait/session logical reads      
-------------------------------------------------Top 5 Timed Events
SELECT EVENT, WAITS, TIME,
            DECODE(WAITS, NULL, TO_NUMBER(NULL),
                          0,    TO_NUMBER(NULL),
                          TIME/WAITS*1000) AVGWT,
            PCTWTT, WAIT_CLASS
      FROM (SELECT EVENT, WAITS, TIME, PCTWTT, WAIT_CLASS
             FROM (SELECT E.EVENT_NAME                           EVENT,
                          E.TOTAL_WAITS - NVL(B.TOTAL_WAITS,0)   WAITS,
                          (E.TIME_WAITED_MICRO -
                           NVL(B.TIME_WAITED_MICRO,0)) / 1000000 TIME,
                          100 * (E.TIME_WAITED_MICRO -
                                        NVL(B.TIME_WAITED_MICRO,0)) /:db_time PCTWTT,
                          E.WAIT_CLASS                           WAIT_CLASS
                    FROM DBA_HIST_SYSTEM_EVENT B,
                         DBA_HIST_SYSTEM_EVENT E
                    WHERE B.SNAP_ID(+)          = :b_snap_id
                      AND E.SNAP_ID             = :e_snap_id
                      AND B.DBID(+)             = :b_dbid
                      AND E.DBID                = :b_dbid
                      AND B.INSTANCE_NUMBER(+)  = 1
                      AND E.INSTANCE_NUMBER     = 1
                      AND B.EVENT_ID(+)         = E.EVENT_ID
                      AND E.TOTAL_WAITS         > NVL(B.TOTAL_WAITS,0)
                      AND E.WAIT_CLASS         != 'Idle'
                   UNION ALL
                   SELECT 'CPU time'                             EVENT,
                          TO_NUMBER(NULL)                        WAITS,
                          :db_cpu       TIME,
                          100 * :db_cpu/:db_time  PCTWTT,
                          NULL                                   WAIT_CLASS
                    FROM DUAL
                    WHERE :db_cpu         > 0)
             ORDER BY TIME DESC, WAITS DESC)
      WHERE ROWNUM <= 5;
-------------------------------------------------Time Model Statistics
select stat_name,
            seconds,
            decode((dbt + bglast), 0, percent, to_number(null)),
            (dbt + bglast) order_col
     from
      (select b.stat_name,
              (b.value - a.value) / 1000000 as seconds,
              100 * ((b.value - a.value) / :db_time) as percent,
              decode(b.stat_name, 'DB time', 1, 0) dbt,
              decode(instr(b.stat_name, 'background'), 1, 2, 0) bglast
       from dba_hist_sys_time_model a,
            dba_hist_sys_time_model b
    where a.dbid = :b_dbid
      and b.dbid = :b_dbid
      and a.instance_number = 1
      and b.instance_number = 1
      and a.snap_id = :b_snap_id
      and b.snap_id = :e_snap_id
         and a.stat_id = b.stat_id
         and b.value - a.value > 0)
       order by order_col asc, seconds desc, stat_name;
----------------------------------------Wait Class
select e.wait_class                                          wait_class,
            sum(e.total_waits - nvl(b.total_waits,0))             waits,
            decode(sum(e.total_waits - nvl(b.total_waits,0)),
                   0, to_number(NULL),
                   100 * sum(e.total_timeouts - nvl(b.total_timeouts,0)) /
                   sum(e.total_waits - nvl(b.total_waits,0)))     topct,
            sum(e.time_waited_micro - nvl(b.time_waited_micro,0))/1000000 time,
            decode(sum(e.total_waits - nvl(b.total_waits, 0)),
                   0, to_number(NULL),
                   (sum(e.time_waited_micro - nvl(b.time_waited_micro,0))/1000)
                    / sum(e.total_waits - nvl(b.total_waits,0)))  avgwt,
            sum(e.total_waits - nvl(b.total_waits,0)) / :tran     txwaits
         from dba_hist_system_event b,
              dba_hist_system_event e
     where b.snap_id(+)          = :b_snap_id
       and e.snap_id             = :e_snap_id
       and b.dbid(+)             = :b_dbid
       and e.dbid                = :b_dbid
       and b.instance_number(+)  = 1
       and e.instance_number     = 1
           and b.event_id(+)         = e.event_id
           and e.total_waits         > nvl(b.total_waits,0)
           and e.wait_class         != 'Idle'
         group by e.wait_class
      order by time desc, waits desc, wait_class;

----------------------------------------Wait Events
select e.event_name                               event,
           e.total_waits - nvl(b.total_waits,0)       waits,
           decode (e.total_waits - nvl(b.total_waits,0), 0, to_number(NULL),
                   100 * (e.total_timeouts - nvl(b.total_timeouts,0)) /
                   (e.total_waits - nvl(b.total_waits,0)))  pctto,
           (e.time_waited_micro - nvl(b.time_waited_micro,0))/1000000  time,
           decode ((e.total_waits - nvl(b.total_waits, 0)),
                   0, to_number(NULL),
                   ((e.time_waited_micro - nvl(b.time_waited_micro,0))/1000)
                    / (e.total_waits - nvl(b.total_waits,0))) avgwt,
           (e.total_waits - nvl(b.total_waits,0)) / :tran     txwaits,
           decode(e.wait_class, 'Idle', 99, 0)              idle
     from dba_hist_system_event b,
          dba_hist_system_event e
     where b.snap_id(+)          = :b_snap_id
       and e.snap_id             = :e_snap_id
       and b.dbid(+)             = :b_dbid
       and e.dbid                = :b_dbid
       and b.instance_number(+)  = 1
       and e.instance_number     = 1
       and b.event_id(+)         = e.event_id
       and e.total_waits         > nvl(b.total_waits,0)
       and e.event_name not in ('smon timer',
                                'pmon timer',
                                'dispatcher timer',
                                'dispatcher listen timer',
                                'rdbms ipc message')
     order by idle, time desc, waits desc, event;
-------------------------------------------Background Wait Events
select e.event_name                               event,
           e.total_waits - nvl(b.total_waits,0)       waits,
           decode (e.total_waits - nvl(b.total_waits,0), 0, to_number(NULL),
                   100 * (e.total_timeouts - nvl(b.total_timeouts,0)) /
                   (e.total_waits - nvl(b.total_waits,0)))  pctto,
           (e.time_waited_micro - nvl(b.time_waited_micro,0))/1000000  time,
           decode ((e.total_waits - nvl(b.total_waits, 0)),
                   0, to_number(NULL),
                   ((e.time_waited_micro - nvl(b.time_waited_micro,0))/1000)
                    / (e.total_waits - nvl(b.total_waits,0)) ) avgwt,
           (e.total_waits - nvl(b.total_waits,0))/:tran        txwaits,
           decode(e.wait_class, 'Idle', 99, 0)               idle
     from dba_hist_bg_event_summary   b,
          dba_hist_bg_event_summary   e
     where b.snap_id(+)          = :b_snap_id
       and e.snap_id             = :e_snap_id
       and b.dbid(+)             = :b_dbid
       and e.dbid                = :b_dbid
       and b.instance_number(+)  = 1
       and e.instance_number     = 1
       and b.event_id(+)        = e.event_id
       and e.total_waits        > nvl(b.total_waits,0)
     order by idle, time desc, waits desc, event;
----------------------------------------------Operating System Statistics
select
      substr(e.stat_name, 1, 35) as name,
      (case when e.stat_name like 'NUM_CPU%'           then e.value
            when e.stat_name = 'LOAD'                  then e.value
            when e.stat_name = 'PHYSICAL_MEMORY_BYTES' then e.value
            else e.value - b.value
       end) as value,
      ( decode(instrb(e.stat_name, 'TIME'),            0, 0, 1)
      + decode(instrb(e.stat_name, 'LOAD'),            0, 0, 2)
      + decode(instrb(e.stat_name, 'CPU_WAIT'),        0, 0, 3)
      + decode(instrb(e.stat_name, 'VM_'),             0, 0, 4)
      + decode(instrb(e.stat_name, 'PHYSICAL_MEMORY'), 0, 0, 5)
      + decode(instrb(e.stat_name, 'NUM_CPU'),         0, 0, 6)
      )                                                      styp
    from dba_hist_osstat b,
         dba_hist_osstat e
    where b.dbid = :b_dbid
      and e.dbid = :b_dbid
      and b.instance_number = 1
      and e.instance_number = 1
      and b.snap_id = :b_snap_id
      and e.snap_id = :e_snap_id
      and b.stat_id = e.stat_id
    order by styp asc, name asc;
-----------------------------------------------Service Statistics
select service_name,
           round(db_time / 1000000, 1),
           round(db_cpu  / 1000000, 1),
           phy_reads,
           log_reads
    from
     (select s1.service_name,
             sum(decode(s1.stat_name, 'DB time', s1.diff, 0))   db_time,
             sum(decode(s1.stat_name, 'DB CPU',  s1.diff, 0))   db_cpu,
             sum(decode(s1.stat_name, 'physical reads',
                        s1.diff, 0))                              phy_reads,
             sum(decode(s1.stat_name, 'session logical reads',
                        s1.diff, 0))                              log_reads
      from
        (select e.service_name     service_name,
                e.stat_name        stat_name,
                e.value - b.value  diff
          from dba_hist_service_stat b,
               dba_hist_service_stat e
    where b.dbid = :b_dbid
      and e.dbid = :b_dbid
      and b.instance_number = 1
      and e.instance_number = 1
      and b.snap_id = :b_snap_id
      and e.snap_id = :e_snap_id
            and b.stat_id         = e.stat_id
            and b.service_name_hash = e.service_name_hash) s1
      group by
        s1.service_name
      order by
        db_time desc, service_name)
      where rownum <= 10;
---------------------------------------------------Service Wait Class Stats
select
     service_name, uio_waits, uio_time, con_waits, con_time,
     adm_waits, adm_time, net_waits, net_time
   from
    (select
       stat1.service_name service_name, db_time, uio_waits, uio_time,
       con_waits, con_time, adm_waits, adm_time, net_waits, net_time
     from
      (select s1.service_name,
              sum(decode(s1.stat_name, 'DB time', s1.diff, 0)) db_time
       from
         (select e.service_name     service_name,
                 e.stat_name        stat_name,
                 e.value - b.value  diff
           from dba_hist_service_stat b,
                dba_hist_service_stat e
    where b.dbid = :b_dbid
      and e.dbid = :b_dbid
      and b.instance_number = 1
      and e.instance_number = 1
      and b.snap_id = :b_snap_id
      and e.snap_id = :e_snap_id
             and b.stat_id         = e.stat_id
             and b.service_name_hash = e.service_name_hash) s1
           group by s1.service_name) stat1,
      (select s2.service_name,
              sum(decode(s2.wait_class, 'User I/O',
                         s2.total_waits, 0))              uio_waits,
              sum(decode(s2.wait_class, 'User I/O',
                         s2.time_waited, 0))              uio_time,
              sum(decode(s2.wait_class, 'Concurrency',  
                         s2.total_waits, 0))              con_waits,
              sum(decode(s2.wait_class, 'Concurrency',
                         s2.time_waited, 0))              con_time,
              sum(decode(s2.wait_class, 'Administrative',
                         s2.total_waits, 0))              adm_waits,
              sum(decode(s2.wait_class, 'Administrative',
                         s2.time_waited, 0))              adm_time,
              sum(decode(s2.wait_class, 'Network',
                         s2.total_waits, 0))              net_waits,
              sum(decode(s2.wait_class, 'Network',
                         s2.time_waited, 0))              net_time
        from
         (select e.service_name                 service_name,
                 e.wait_class                   wait_class,
                 e.total_waits - b.total_waits  total_waits,
                 e.time_waited - b.time_waited  time_waited
           from dba_hist_service_wait_class b,
                dba_hist_service_wait_class e
    where b.dbid = :b_dbid
      and e.dbid = :b_dbid
      and b.instance_number = 1
      and e.instance_number = 1
      and b.snap_id = :b_snap_id
      and e.snap_id = :e_snap_id
             and b.wait_class_id   = e.wait_class_id
             and b.service_name_hash = e.service_name_hash) s2
           group by s2.service_name) stat2
    where
      stat1.service_name = stat2.service_name
    order by
      db_time desc, service_name)
  where rownum <= 10;
----------------------------------------------------SQL ordered by Elapsed Time  
select *
      from (select
             nvl((sqt.elap/1000000), to_number(null)),
             nvl((sqt.cput/1000000), to_number(null)),
             sqt.exec,
             decode(sqt.exec, 0, to_number(null),
                    (sqt.elap / sqt.exec / 1000000)),
             (100 * (sqt.elap / :db_time)) norm_val,
             sqt.sql_id,
             to_clob(decode(sqt.module, null,
                            null, 'Module: ' || sqt.module)),
             nvl(st.sql_text,
                 to_clob('** SQL Text Not Available **'))
            from (select sql_id, max(module) module,
                         sum(elapsed_time_delta) elap,
                         sum(cpu_time_delta)     cput,
                         sum(executions_delta)   exec
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
                  group by sql_id) sqt,
                 dba_hist_sqltext st
            where st.sql_id(+)             = sqt.sql_id
              and st.dbid(+)               = :b_dbid
            order by nvl(sqt.elap, -1) desc, sqt.sql_id)
     where rownum < 65 and
           (rownum <= 10 or
            norm_val > 1.0);       
------------------------------------------------------SQL ordered by CPU Time                       
select *
      from (select
             nvl((sqt.cput/1000000), to_number(null)),
             nvl((sqt.elap/1000000), to_number(null)),
             sqt.exec,
             decode(sqt.exec, 0, to_number(null),
                    (sqt.cput / sqt.exec / 1000000)),
             (100 * (sqt.elap / :db_time)) norm_val,
             sqt.sql_id,
             to_clob(decode(sqt.module, null,
                            null, 'Module: ' || sqt.module)),
             nvl(st.sql_text,
                 to_clob('** SQL Text Not Available **'))
            from (select sql_id, max(module) module,
                         sum(cpu_time_delta)     cput,
                         sum(elapsed_time_delta) elap,
                         sum(executions_delta)   exec
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
                  group by sql_id) sqt,
                 dba_hist_sqltext st
            where st.sql_id(+)             = sqt.sql_id
              and st.dbid(+)               = :b_dbid
            order by nvl(sqt.cput, -1) desc, sqt.sql_id)
     where rownum < 65 and
           (rownum <= 10 or
            norm_val > 1.0);       
------------------------------------------------------SQL ordered by Gets
select *
     from (select
             sqt.bget,
             sqt.exec,
             decode(sqt.exec, 0, to_number(null),
                    (sqt.bget / sqt.exec)),
             (100*sqt.bget)/:gets  norm_val,
             nvl((sqt.cput/1000000), to_number(null)),
             nvl((sqt.elap/1000000), to_number(null)),
             sqt.sql_id,
             to_clob(decode(sqt.module, null,
                            null, 'Module: ' || sqt.module)),
             nvl(st.sql_text,
                 to_clob('** SQL Text Not Available **'))
            from (select sql_id, max(module) module,
                         sum(buffer_gets_delta) bget,
                         sum(executions_delta) exec,
                         sum(cpu_time_delta) cput,
                         sum(elapsed_time_delta) elap
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
                  group by sql_id) sqt,
                 dba_hist_sqltext st
            where st.sql_id(+)             = sqt.sql_id
              and st.dbid(+)               = :b_dbid
            order by nvl(sqt.bget, -1) desc, sqt.sql_id)
     where rownum < 65 and
           (rownum <= 10 or
            norm_val > 1.0);
------------------------------------------------SQL ordered by Reads                       
select *
     from (select
            sqt.dskr,
            sqt.exec,
            decode(sqt.exec, 0, to_number(null),
                   (sqt.dskr / sqt.exec)),
            (100 * sqt.dskr)/:phyr  norm_val,
            nvl((sqt.cput / 1000000), to_number(null)),
            nvl((sqt.elap / 1000000), to_number(null)),
            sqt.sql_id,
            decode(sqt.module, null,
                   null, 'Module: ' || sqt.module),
            nvl(st.sql_text,
                to_clob('** SQL Text Not Available **'))
           from (select sql_id, max(module) module,
                        sum(disk_reads_delta) dskr, sum(executions_delta) exec,
                        sum(cpu_time_delta) cput, sum(elapsed_time_delta) elap
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
               group by sql_id) sqt,
               dba_hist_sqltext st
           where st.sql_id(+)             = sqt.sql_id
             and st.dbid(+)               = :b_dbid
             and :phyr                    > 0
           order by nvl(sqt.dskr, -1) desc, sqt.sql_id)
    where rownum < 65 and
          (rownum <= 10 or
           norm_val > 1.0);
-------------------------------------------------SQL ordered by Executions
select *
     from (select
            sqt.exec  exec,
            sqt.rowp,
            sqt.rowp / sqt.exec,
            sqt.cput / sqt.exec / 1000000,
            sqt.elap / sqt.exec / 1000000,
            sqt.sql_id,
            decode(sqt.module, null, null, 'Module: ' || sqt.module),
            nvl(st.sql_text, to_clob('** SQL Text Not Available **'))
            from (select sql_id, max(module) module,
                         sum(executions_delta) exec,
                         sum(rows_processed_delta) rowp,
                         sum(cpu_time_delta) cput,
                         sum(elapsed_time_delta) elap
                   from dba_hist_sqlstat
                   where dbid            = :b_dbid
                     and instance_number = 1
                     and :b_snap_id            < snap_id
                     and snap_id        <= :e_snap_id
                   group by sql_id) sqt,
                        dba_hist_sqltext st
             where st.sql_id(+)            = sqt.sql_id
               and st.dbid(+)              = :b_dbid
               and sqt.exec                > 0
              order by nvl(sqt.exec, -1) desc, sqt.sql_id)
     where rownum < 65 and
           (rownum <= 10 or
            (100 * exec) / :exe > 1.0);
---------------------------------------------SQL ordered by Parse Calls
select *
     from (select
            sqt.prsc,
            sqt.exec,
            decode(:prse, 0, 0, 100 * sqt.prsc / :prse) norm_val,
            sqt.sql_id,
            decode(sqt.module, null, null, 'Module: ' || sqt.module),
            nvl(st.sql_text, to_clob('** SQL Text Not Available **'))
           from (select sql_id, max(module) module,
                        sum(buffer_gets_delta) bget,
                        sum(disk_reads_delta) dskr,
                        sum(executions_delta) exec,
                        sum(parse_calls_delta) prsc
                 from dba_hist_sqlstat
                 where dbid            = :b_dbid
                   and instance_number = 1
                   and :b_snap_id            < snap_id
                   and snap_id        <= :e_snap_id
                 group by sql_id) sqt,
                      dba_hist_sqltext st
           where st.sql_id(+)             = sqt.sql_id
             and st.dbid(+)               = :b_dbid
           order by nvl(sqt.prsc, -1) desc, sqt.sql_id)
   where rownum < 65 and
         (rownum <= 10 or
          norm_val > 1.0);

----------------------------------------------SQL ordered by Sharable Memory
select *
     from (select /*+ ordered use_nl (b st) */
            e.sharable_mem,
            sqt.exec,
            decode(:e_spm, 0, 0, 100 * e.sharable_mem/:e_spm),
            e.sql_id,
            decode(e.module, null, null, 'Module: ' || e.module),
            nvl(st.sql_text, to_clob('** SQL Text Not Available **'))
           from dba_hist_sqlstat e,
                (select sql_id, sum(executions_delta) exec
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
                  group by sql_id) sqt,
                dba_hist_sqltext  st
           where sqt.sql_id(+)          = e.sql_id
             and e.snap_id              = :e_snap_id
             and e.dbid                 = :b_dbid
             and e.instance_number      = 1
             and st.sql_id(+)           = e.sql_id
             and st.dbid(+)             = :b_dbid
             and e.sharable_mem         > 1048576
          order by nvl(e.sharable_mem, -1) desc, e.sql_id)
    where rownum < 65;
---------------------------------------------SQL ordered by Version Count
select *
     from (select /*+ ordered use_nl (b st) */
            e.version_count,
            sqt.exec,
            e.sql_id,
            decode(e.module, null, null, 'Module: ' || e.module),
            nvl(st.sql_text, to_clob('** SQL Text Not Available **'))
           from dba_hist_sqlstat e,
                (select sql_id, sum(executions_delta) exec
                  from dba_hist_sqlstat
                  where dbid            = :b_dbid
                    and instance_number = 1
                    and :b_snap_id            < snap_id
                    and snap_id        <= :e_snap_id
               group by sql_id) sqt,
                dba_hist_sqltext     st
           where sqt.sql_id(+)          = e.sql_id
             and e.snap_id              = :e_snap_id
             and e.dbid                 = :b_dbid
             and e.instance_number      = 1
             and st.sql_id(+)           = e.sql_id
             and st.dbid(+)             = :b_dbid
             and e.version_count        > 20
           order by nvl(e.version_count, -1) desc, e.sql_id)
     where rownum < 65;
-----------------------------------------------Instance Activity Stats
select b.stat_name st,
             e.value - b.value,
             round((e.value - b.value)/:ela,2),
             round((e.value - b.value)/:tran,2)
       from  dba_hist_sysstat b,
             dba_hist_sysstat e
       where b.snap_id         = :b_snap_id
         and e.snap_id         = :e_snap_id
         and b.dbid            = :b_dbid
         and e.dbid            = :b_dbid
         and b.instance_number = 1
         and e.instance_number = 1
         and b.stat_id         = e.stat_id
         and e.stat_name not in  
                         ('logons current',
                          'opened cursors current',
                          'workarea memory allocated',
                          'session cursor cache count')
         and e.value          >= b.value
         and e.value          >  0
       order by st;
---------------------------------------------Instance Activity Stats - Absolute Values
select b.stat_name st,
            b.value,
            e.value
      from dba_hist_sysstat b,
           dba_hist_sysstat e
      where b.snap_id         = :b_snap_id
        and e.snap_id         = :e_snap_id
        and b.dbid            = :b_dbid
        and e.dbid            = :b_dbid
        and b.instance_number = 1
        and e.instance_number = 1
        and b.stat_id         = e.stat_id
        and e.stat_name in
                        ('logons current',
                         'opened cursors current',
                         'workarea memory allocated',
                         'session cursor cache count')
        and e.value >  0;
---------------------------------------------Instance Activity Stats - Thread Activity
select 'log switches (derived)',
             e.sequence# - b.sequence# ,
             (e.sequence# - b.sequence#)/(:ela/3600)
       from dba_hist_thread e,
            dba_hist_thread b
       where b.snap_id                = :b_snap_id
         and e.snap_id                = :e_snap_id
         and b.dbid                   = :b_dbid
         and e.dbid                   = :b_dbid
         and b.instance_number        = 1
         and e.instance_number        = 1
         and b.thread#                = e.thread#
         and b.thread_instance_number = e.thread_instance_number
         and e.thread_instance_number = 1;
-------------------------------------------Tablespace IO Stats
select e.tsname                                               tsname,
           sum(e.phyrds - nvl(b.phyrds,0))                        reads,
           sum(e.phyrds - nvl(b.phyrds,0)) / :ela                 rps,
           decode(sum(e.phyrds - nvl(b.phyrds, 0)),
                  0, 0,
                  10 * (sum(e.readtim - nvl(b.readtim, 0)) /
                        sum(e.phyrds  - nvl(b.phyrds,  0))))      atpr,
           decode(sum(e.phyrds - nvl(b.phyrds,0)),
                  0, 0,
                  sum(e.phyblkrd - nvl(b.phyblkrd,0)) /
                  sum(e.phyrds   - nvl(b.phyrds,0)))              bpr,
           sum(e.phywrts    - nvl(b.phywrts,0))                   writes,
           sum(e.phywrts    - nvl(b.phywrts,0)) / :ela            wps,
           sum(e.wait_count - nvl(b.wait_count,0))                waits,
           decode(sum(e.wait_count - nvl(b.wait_count, 0)),
                  0, 0,
                  10 * (sum(e.time       - nvl(b.time,0)) /
                        sum(e.wait_count - nvl(b.wait_count,0)))) atpwt,
           sum(e.phyrds  - nvl(b.phyrds,0))  +  
           sum (e.phywrts - nvl(b.phywrts,0))                     ios
     from dba_hist_filestatxs e,
          dba_hist_filestatxs b
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.tsname(+)          = e.tsname
       and b.file#(+)           = e.file#
       and b.creation_change#(+) = e.creation_change#
       and ((e.phyrds  - nvl(b.phyrds,0))  +
            (e.phywrts - nvl(b.phywrts,0))) > 0
     group by e.tsname
    union all
    select e.tsname                                             tsname,
           sum(e.phyrds - nvl(b.phyrds,0))                      reads,
           sum(e.phyrds - nvl(b.phyrds,0))/:ela                 rps,
           decode(sum(e.phyrds - nvl(b.phyrds,0)),
                  0, 0,
                  (sum(e.readtim - nvl(b.readtim,0)) /
                   sum(e.phyrds  - nvl(b.phyrds,0)))*10)        atpr,
           decode(sum(e.phyrds - nvl(b.phyrds,0)),
                  0, to_number(NULL),
                  sum(e.phyblkrd - nvl(b.phyblkrd,0)) /
                  sum(e.phyrds   - nvl(b.phyrds,0)))            bpr,
           sum(e.phywrts    - nvl(b.phywrts,0))                 writes,
           sum(e.phywrts    - nvl(b.phywrts,0)) / :ela          wps,
           sum(e.wait_count - nvl(b.wait_count,0))              waits,
           decode(sum(e.wait_count - nvl(b.wait_count, 0)),
                  0, 0,
                  (sum(e.time       - nvl(b.time,0)) /
                   sum(e.wait_count - nvl(b.wait_count,0)))*10) atpwt,
           sum(e.phyrds  - nvl(b.phyrds,0)) +  
           sum(e.phywrts - nvl(b.phywrts,0))                    ios
     from dba_hist_tempstatxs e,
          dba_hist_tempstatxs b
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.tsname(+)          = e.tsname
       and b.file#(+)           = e.file#
       and b.creation_change#(+) = e.creation_change#
       and ((e.phyrds  - nvl(b.phyrds,0))  +
            (e.phywrts - nvl(b.phywrts,0))) > 0
     group by e.tsname
     order by ios desc, tsname;
---------------------------------------------------File IO Stats
select e.tsname
         , substr(e.filename, 1, 52)                       filename
         , e.phyrds- nvl(b.phyrds,0)                       reads
         , (e.phyrds- nvl(b.phyrds,0))/:ela                rps
         , decode ((e.phyrds - nvl(b.phyrds, 0)), 0, to_number(NULL),
              ((e.readtim  - nvl(b.readtim,0)) /
               (e.phyrds   - nvl(b.phyrds,0)))*10)         atpr
         , decode ((e.phyrds - nvl(b.phyrds, 0)), 0, to_number(NULL),
              (e.phyblkrd - nvl(b.phyblkrd,0)) /
              (e.phyrds   - nvl(b.phyrds,0)) )             bpr
         , e.phywrts - nvl(b.phywrts,0)                    writes
         , (e.phywrts - nvl(b.phywrts,0))/:ela             wps
         , e.wait_count - nvl(b.wait_count,0)              waits
         , decode ((e.wait_count - nvl(b.wait_count, 0)), 0, 0,
              ((e.time       - nvl(b.time,0)) /
               (e.wait_count - nvl(b.wait_count,0)))*10)   atpwt
      from dba_hist_filestatxs e
         , dba_hist_filestatxs b
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.tsname(+)          = e.tsname
       and b.file#(+)           = e.file#
       and b.creation_change#(+) = e.creation_change#
       and ( (e.phyrds  - nvl(b.phyrds,0)  ) +
             (e.phywrts - nvl(b.phywrts,0) ) ) > 0
    union all
    select e.tsname
         , substr(e.filename, 1, 52)                       filename
         , e.phyrds- nvl(b.phyrds,0)                       reads
         , (e.phyrds- nvl(b.phyrds,0))/:ela                rps
         , decode ((e.phyrds - nvl(b.phyrds, 0)), 0, to_number(NULL),
              ((e.readtim  - nvl(b.readtim,0)) /
               (e.phyrds   - nvl(b.phyrds,0)))*10)         atpr
         , decode ((e.phyrds - nvl(b.phyrds, 0)), 0, to_number(NULL),
              (e.phyblkrd - nvl(b.phyblkrd,0)) /
              (e.phyrds   - nvl(b.phyrds,0)) )             bpr
         , e.phywrts - nvl(b.phywrts,0)                    writes
         , (e.phywrts - nvl(b.phywrts,0))/:ela             wps
         , e.wait_count - nvl(b.wait_count,0)              waits
         , decode ((e.wait_count - nvl(b.wait_count, 0)), 0, to_number(NULL),
              ((e.time       - nvl(b.time,0)) /
               (e.wait_count - nvl(b.wait_count,0)))*10)   atpwt
      from dba_hist_tempstatxs e
         , dba_hist_tempstatxs b
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.tsname(+)          = e.tsname
       and b.file#(+)           = e.file#
       and b.creation_change#(+) = e.creation_change#
       and ( (e.phyrds  - nvl(b.phyrds,0)  ) +
             (e.phywrts - nvl(b.phywrts,0) ) ) > 0
     order by tsname, filename;

-------------------------------------------Buffer Pool Statistics         
select replace(e.block_size/1024||'k',:bs/1024||'k', substr(e.name,1,1))             name
         , e.set_msize                                            numbufs
         , decode(   e.db_block_gets     - nvl(b.db_block_gets,0)
                  +  e.consistent_gets   - nvl(b.consistent_gets,0)
                 , 0, to_number(null)
                 , (100* (1 - (  (e.physical_reads - nvl(b.physical_reads,0))
                            / (  e.db_block_gets     - nvl(b.db_block_gets,0)
                              + e.consistent_gets   - nvl(b.consistent_gets,0))
                               )
                          )
                    )
                  )                                               poolhr
         ,    e.db_block_gets    - nvl(b.db_block_gets,0)
           +  e.consistent_gets  - nvl(b.consistent_gets,0)       buffs
         , e.physical_reads      - nvl(b.physical_reads,0)        phread
         , e.physical_writes     - nvl(b.physical_writes,0)       phwrite
         , e.free_buffer_wait    - nvl(b.free_buffer_wait,0)      fbwait
         , e.write_complete_wait - nvl(b.write_complete_wait,0)   wcwait
         , e.buffer_busy_wait    - nvl(b.buffer_busy_wait,0)      bbwait
      from dba_hist_buffer_pool_stat b
         , dba_hist_buffer_pool_stat e
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.id(+)              = e.id
     order by e.name;
----------------------------------------------Instance Recovery Stats
select 'B'                          beg
         , target_mttr                    tm
         , estimated_mttr                 em
         , recovery_estimated_ios         rei
         , actual_redo_blks               arb
         , target_redo_blks               trb
         , log_file_size_redo_blks        lfrb
         , log_chkpt_timeout_redo_blks    lctrb
         , log_chkpt_interval_redo_blks   lcirb
         , snap_id                        snid
      from dba_hist_instance_recovery b
     where b.snap_id         = :b_snap_id
       and b.dbid            = :b_dbid
       and b.instance_number = 1
    union all
    select 'E'                          beg
         , target_mttr                    tm
         , estimated_mttr                 em
         , recovery_estimated_ios         rei
         , actual_redo_blks               arb
         , target_redo_blks               trb
         , log_file_size_redo_blks        lfrb
         , log_chkpt_timeout_redo_blks    lctrb
         , log_chkpt_interval_redo_blks   lcirb
         , snap_id                        snid
      from dba_hist_instance_recovery e
     where e.snap_id         = :e_snap_id
       and e.dbid            = :b_dbid
       and e.instance_number = 1
    order by snid;
-------------------------------------------------Buffer Pool Advisory
select replace(block_size/1024||'k', 8192/1024||'k',substr(name,1,1)) name
         , size_for_estimate
         , size_factor
         , buffers_for_estimate
         , decode(base_physical_reads, 0, to_number(null),
                  round((physical_reads / base_physical_reads), 4))
              estd_physical_read_factor
         , decode(base_physical_reads, 0, physical_reads,
                  round((physical_reads *
                         (actual_physical_reads / base_physical_reads)), 0))
              estd_physical_reads
         , decode(e.block_size, :bs, 1, 2) order_def_bs
      from dba_hist_db_cache_advice e
     where snap_id             = :e_snap_id
       and dbid                = :b_dbid
       and instance_number     = 1
       and physical_reads > 0
     order by order_def_bs, block_size, e.name, buffers_for_estimate;
----------------------------------------------PGA Aggr Summary
select 100
           * (e.bytes   - nvl(b.bytes,0))
           / (e.bytes   - nvl(b.bytes,0)  + e.bytesrw - nvl(b.bytesrw,0))
         , (e.bytes     - nvl(b.bytes,0))  /1024/1024               tbp
         , (e.bytesrw   - nvl(b.bytesrw,0))/1024/1024               tbrw
      from (select sum(case when name = 'bytes processed'
                            then value else 0 end)                  bytes
                 , sum(case when name = 'extra bytes read/written'
                            then value else 0 end)                  bytesrw
             from dba_hist_pgastat e1
            where e1.snap_id          = :e_snap_id
              and e1.dbid             = :b_dbid
              and e1.instance_number  = 1
              and e1.name             in ('bytes processed',
                                          'extra bytes read/written')
           ) e
         , (select sum(case when name = 'bytes processed'
                            then value else 0 end)                  bytes
                 , sum(case when name = 'extra bytes read/written'
                            then value else 0 end)                  bytesrw
             from dba_hist_pgastat b1
            where b1.snap_id          = :b_snap_id
              and b1.dbid             = :b_dbid
              and b1.instance_number  = 1
              and b1.name             in ('bytes processed',
                                          'extra bytes read/written')
           ) b
      where e.bytes - nvl(b.bytes,0) > 0;
------------------------------------------------PGA Aggr Target Stats
select 'B'                                                 snap
         , to_number(p.value)/1024/1024                          pgaat
         , mu.pat/1024/1024                                      pat
         , mu.PGA_alloc/1024/1024                                tot_pga_allo
         , (mu.PGA_used_auto + mu.PGA_used_man)/1024/1024        tot_tun_used
         , 100*(mu.PGA_used_auto + mu.PGA_used_man) / PGA_alloc  pct_tun
         , decode(mu.PGA_used_auto + mu.PGA_used_man, 0, 0
                 , 100* mu.PGA_used_auto/(mu.PGA_used_auto + mu.PGA_used_man)
                 )                                               pct_auto_tun
         , decode(mu.PGA_used_auto + mu.PGA_used_man, 0, 0
                 , 100* mu.PGA_used_man  / (mu.PGA_used_auto + mu.PGA_used_man)
                 )                                               pct_man_tun
         , mu.glob_mem_bnd/1024                                  glo_mem_bnd
      from (select sum(case when name = 'total PGA allocated'
                        then value else 0 end)               PGA_alloc
                 , sum(case when name = 'total PGA used for auto workareas'
                        then value else 0 end)               PGA_used_auto
                 , sum(case when name = 'total PGA used for manual workareas'
                        then value else 0 end)               PGA_used_man
                 , sum(case when name = 'global memory bound'
                        then value else 0 end)               glob_mem_bnd
                 , sum(case when name = 'aggregate PGA auto target'
                        then value else 0 end)               pat
             from dba_hist_pgastat pga
             where pga.snap_id            = :b_snap_id
               and pga.dbid               = :b_dbid
               and pga.instance_number    = 1
           ) mu
            , dba_hist_parameter p
    where p.snap_id            = :b_snap_id
      and p.dbid               = :b_dbid
      and p.instance_number    = 1
      and p.parameter_name     = 'pga_aggregate_target'
      and p.value             != '0'
    union
    select 'E'                                                 snap
         , to_number(p.value)/1024/1024                          pgaat
         , mu.pat/1024/1024                                      pat
         , mu.PGA_alloc/1024/1024                                tot_pga_allo
         , (mu.PGA_used_auto + mu.PGA_used_man)/1024/1024        tot_tun_used
         , 100*(mu.PGA_used_auto + mu.PGA_used_man) / PGA_alloc  pct_tun
         , decode(mu.PGA_used_auto + mu.PGA_used_man, 0, 0
                 , 100* mu.PGA_used_auto/(mu.PGA_used_auto + mu.PGA_used_man)
                 )                                               pct_auto_tun
         , decode(mu.PGA_used_auto + mu.PGA_used_man, 0, 0
                 , 100* mu.PGA_used_man  / (mu.PGA_used_auto + mu.PGA_used_man)
                 )                                               pct_man_tun
         , mu.glob_mem_bnd/1024                                  glo_mem_bnd
      from (select sum(case when name = 'total PGA allocated'
                            then value else 0 end)               PGA_alloc
                 , sum(case when name = 'total PGA used for auto workareas'
                            then value else 0 end)               PGA_used_auto
                 , sum(case when name = 'total PGA used for manual workareas'
                            then value else 0 end)               PGA_used_man
                 , sum(case when name = 'global memory bound'
                            then value else 0 end)               glob_mem_bnd
                 , sum(case when name = 'aggregate PGA auto target'
                            then value else 0 end)               pat
              from dba_hist_pgastat pga
             where pga.snap_id            = :e_snap_id
               and pga.dbid               = :b_dbid
               and pga.instance_number    = 1
           ) mu
             , dba_hist_parameter p
     where p.snap_id            = :e_snap_id
       and p.dbid               = :b_dbid
       and p.instance_number    = 1
       and p.parameter_name     = 'pga_aggregate_target'
       and p.value             != '0'
     order by snap;
------------------------------------------------PGA Aggr Target Histogram
select case when e.low_optimal_size >= 1024*1024*1024*1024
            then lpad(round(e.low_optimal_size/1024/1024/1024/1024) || 'T',7)
            when e.low_optimal_size >= 1024*1024*1024
            then lpad(round(e.low_optimal_size/1024/1024/1024) || 'G' ,7)
            when e.low_optimal_size >= 1024*1024
            then lpad(round(e.low_optimal_size/1024/1024) || 'M',7)
            when e.low_optimal_size >= 1024
            then lpad(round(e.low_optimal_size/1024) || 'K',7)
            else lpad(e.low_optimal_size || 'B',7)
           end                                                         low_o
         , case when e.high_optimal_size >= 1024*1024*1024*1024
            then lpad(round(e.high_optimal_size/1024/1024/1024/1024) ||'T',7)
            when e.high_optimal_size >= 1024*1024*1024
            then lpad(round(e.high_optimal_size/1024/1024/1024) || 'G',7)
            when e.high_optimal_size >= 1024*1024
            then lpad(round(e.high_optimal_size/1024/1024) || 'M',7)
            when e.high_optimal_size >= 1024
            then lpad(round(e.high_optimal_size/1024) || 'K',7)
            else e.high_optimal_size || 'B'
           end                                                         high_o
         , e.total_executions       - nvl(b.total_executions,0)        tot_e
         , e.optimal_executions     - nvl(b.optimal_executions,0)      opt_e
         , e.onepass_executions     - nvl(b.onepass_executions,0)      one_e
         , e.multipasses_executions - nvl(b.multipasses_executions,0)  mul_e
      from dba_hist_sql_workarea_hstgrm e
         , dba_hist_sql_workarea_hstgrm b
     where e.snap_id              = :e_snap_id
       and e.dbid                 = :b_dbid
       and e.instance_number      = 1
       and b.snap_id(+)           = :b_snap_id
       and b.dbid(+)              = e.dbid
       and b.instance_number(+)   = e.instance_number
       and b.low_optimal_size(+)  = e.low_optimal_size
       and b.high_optimal_size(+) = e.high_optimal_size
       and e.total_executions  - nvl(b.total_executions,0) > 0
     order by e.low_optimal_size;
-----------------------------------------------PGA Memory Advisory
select pga_target_for_estimate/1024/1024  pga_t
         , pga_target_factor                  pga_tf
         , bytes_processed/1024/1024          byt_p
         , estd_extra_bytes_rw/1024/1024      byt_rw
         , estd_pga_cache_hit_percentage      epchp
         , estd_overalloc_count               eoc
      from dba_hist_pga_target_advice e
     where snap_id             = :e_snap_id
       and dbid                = :b_dbid
       and instance_number     = 1
     order by pga_target_for_estimate;
-----------------------------------------------Shared Pool Advisory
select shared_pool_size_for_estimate,
            shared_pool_size_factor,
            estd_lc_size,
            estd_lc_memory_objects,
            estd_lc_time_saved,
            estd_lc_time_saved_factor,
            estd_lc_load_time,
            estd_lc_load_time_factor,
            estd_lc_memory_object_hits
      from dba_hist_shared_pool_advice
      where snap_id             = :e_snap_id
        and dbid                = :b_dbid
        and instance_number     = 1
     order by shared_pool_size_for_estimate;
-----------------------------------------------SGA Target Advisory
select sga_size,
           sga_size_factor,
           estd_db_time,
           estd_physical_reads
      from dba_hist_sga_target_advice e
     where snap_id             = :e_snap_id
       and dbid                = :b_dbid
       and instance_number     = 1
     order by sga_size;
----------------------------------------------Streams Pool Advisory
select size_for_estimate,
           size_factor,
           estd_spill_count,
           estd_spill_time,
           estd_unspill_count,
           estd_unspill_time
      from dba_hist_streams_pool_advice e
     where snap_id             = :e_snap_id
       and dbid                = :b_dbid
       and instance_number     = 1
     order by size_factor;
----------------------------------------------Java Pool Advisory
select java_pool_size_for_estimate,
            java_pool_size_factor,
            estd_lc_size,
            estd_lc_memory_objects,
            estd_lc_time_saved,
            nvl(estd_lc_time_saved_factor,0),
            estd_lc_load_time,
            nvl(estd_lc_load_time_factor,0),
            estd_lc_memory_object_hits
      from dba_hist_java_pool_advice
      where snap_id             = :e_snap_id
        and dbid                = :b_dbid
        and instance_number     = 1
        and estd_lc_memory_objects > 0
      order by java_pool_size_for_estimate;

----------------------------------------------Buffer Wait Statistics
select e.class                                   class
         , e.wait_count  - nvl(b.wait_count,0)       icnt
         , (e.time        - nvl(b.time,0))/100       itim
         , 10*  (e.time       - nvl(b.time,0))
             / (e.wait_count - nvl(b.wait_count,0))  iavg  
      from dba_hist_waitstat b
         , dba_hist_waitstat e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.dbid            = e.dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.instance_number = e.instance_number
       and b.class           = e.class
       and b.wait_count      < e.wait_count
     order by itim desc, icnt desc, class;
---------------------------------------------Enqueue Activity
select /*+ ordered */
          substr(e.eq_type || '-' || to_char(nvl(l.name,' '))
              || decode( upper(e.req_reason)
                       , 'CONTENTION', null
                       , '-',          null
                       , ' ('||e.req_reason||')'), 1, 78)  ety
         , e.total_req#    - nvl(b.total_req#,0)            reqs
         , e.succ_req#     - nvl(b.succ_req#,0)             sreq
         , e.failed_req#   - nvl(b.failed_req#,0)           freq
         , e.total_wait#   - nvl(b.total_wait#,0)           waits
         , (e.cum_wait_time - nvl(b.cum_wait_time,0))/1000  wttm
         , decode(  (e.total_wait#   - nvl(b.total_wait#,0))
                   , 0, to_number(NULL)
                   , (  (e.cum_wait_time - nvl(b.cum_wait_time,0))
                      / (e.total_wait#   - nvl(b.total_wait#,0))
                     )
                 )                                          awttm
      from dba_hist_enqueue_stat b
         , dba_hist_enqueue_stat e
         , v$lock_type           l
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.eq_type(+)         = e.eq_type
       and b.req_reason(+)      = e.req_reason
       and e.total_wait# - nvl(b.total_wait#,0) > 0
       and l.type(+)            = e.eq_type
     order by wttm desc, waits desc, e.eq_type;
--------------------------------------------Undo Segment Summary
select undotsn
         , sum(undoblks)/1000           undob
         , sum(txncount)                txcnt
         , max(maxquerylen)             maxq
         , max(maxconcurrency)          maxc
         ,           min(tuned_undoretention)/60
           ||'/'|| max(tuned_undoretention)/60   mintun
         ,           sum(ssolderrcnt)
           ||'/'|| sum(nospaceerrcnt)            snolno
         ,           sum(unxpstealcnt)
           ||'/'|| sum(unxpblkrelcnt)
           ||'/'|| sum(unxpblkreucnt)
           ||'/'|| sum(expstealcnt)
           ||'/'|| sum(expblkrelcnt)
           ||'/'|| sum(expblkreucnt)  blkst
      from dba_hist_undostat
     where dbid            = :b_dbid
       and instance_number = 1
       and end_time        > :B_END_INTERVAL_TIME
       and begin_time      < :E_END_INTERVAL_TIME
     group by undotsn;
---------------------------------------------Undo Segment Stats
select endt
         , undob
         , txcnt
         , maxq
         , maxc
         , mintun
         , snolno
         , blkst
         , undotsn
      from (select undotsn
                 , to_char(end_time,   'DD-Mon HH24:MI')  endt
                 , undoblks                                 undob
                 , txncount                                 txcnt
                 , maxquerylen                              maxq
                 , maxconcurrency                           maxc
                 , tuned_undoretention/60                   mintun
                 , ssolderrcnt ||'/'|| nospaceerrcnt      snolno
                 ,         unxpstealcnt
                   ||'/'|| unxpblkrelcnt
                   ||'/'|| unxpblkreucnt
                   ||'/'|| expstealcnt
                   ||'/'|| expblkrelcnt
                   ||'/'|| expblkreucnt                   blkst
              from dba_hist_undostat
             where dbid            = :b_dbid
               and instance_number = 1
               and end_time        >  :B_END_INTERVAL_TIME
               and begin_time      <  :E_END_INTERVAL_TIME
             order by begin_time desc
           )
     where rownum < 35;
---------------------------------------------Latch Statistics
select e.latch_name    parent
         , e.gets        - b.gets                      gets
         , e.misses      - b.misses                    misses
         , e.sleeps      - b.sleeps                    sleeps
         , to_char(e.spin_gets          - b.spin_gets)
           ||'/'||to_char(e.sleep1      - b.sleep1)
           ||'/'||to_char(e.sleep2      - b.sleep2)
           ||'/'||to_char(e.sleep3      - b.sleep3)    sleephist
      from dba_hist_latch_parent b
         , dba_hist_latch_parent e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.latch_hash      = e.latch_hash
       and e.sleeps - b.sleeps > 0
     order by parent;
--------------------------------------------Latch Activity
select b.latch_name                                      name
         , e.gets    - b.gets                                gets
         , to_number(decode(e.gets, b.gets, null,
           (e.misses - b.misses) * 100/(e.gets - b.gets)))   missed
         , to_number(decode(e.misses, b.misses, null,
           (e.sleeps - b.sleeps)/(e.misses - b.misses)))     sleeps
         , (e.wait_time - b.wait_time)/1000000               wt
         , e.immediate_gets - b.immediate_gets               nowai
         , to_number(decode(e.immediate_gets,
                            b.immediate_gets, null,
                            (e.immediate_misses - b.immediate_misses) * 100 /
                            (e.immediate_gets   - b.immediate_gets)))   imiss
     from  dba_hist_latch b
         , dba_hist_latch e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.latch_hash      = e.latch_hash
       and (   e.gets           - b.gets
             + e.immediate_gets - b.immediate_gets
           ) > 0
     order by b.latch_name;
----------------------------------------------Latch Sleep Breakdown
select b.latch_name                                name
         , e.gets        - b.gets                      gets
         , e.misses      - b.misses                    misses
         , e.sleeps      - b.sleeps                    sleeps
         , e.spin_gets   - b.spin_gets                 spin_gets
         , e.sleep1      - b.sleep1                    sleep1
         , e.sleep2      - b.sleep2                    sleep2
         , e.sleep3      - b.sleep3                    sleep3
      from dba_hist_latch b
         , dba_hist_latch e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.latch_hash      = e.latch_hash
       and e.sleeps - b.sleeps > 0
     order by misses desc, name;
--------------------------------------------Latch Miss Sources
select e.parent_name                              parent
         , e.where_in_code                            where_from
         , e.nwfail_count  - nvl(b.nwfail_count,0)    nwmisses
         , e.sleep_count   - nvl(b.sleep_count,0)     sleeps
         , e.wtr_slp_count - nvl(b.wtr_slp_count,0)   waiter_sleeps
      from dba_hist_latch_misses_summary b
         , dba_hist_latch_misses_summary e
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.dbid(+)            = e.dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.instance_number(+) = e.instance_number
       and b.parent_name(+)     = e.parent_name
       and b.where_in_code(+)   = e.where_in_code
       and e.sleep_count        > nvl(b.sleep_count,0)
     order by e.parent_name, sleeps desc, e.where_in_code;
----------------------------------------------Parent Latch Statistics
select e.latch_name    parent
         , e.gets        - b.gets                      gets
         , e.misses      - b.misses                    misses
         , e.sleeps      - b.sleeps                    sleeps
         , to_char(e.spin_gets          - b.spin_gets)
           ||'/'||to_char(e.sleep1      - b.sleep1)
           ||'/'||to_char(e.sleep2      - b.sleep2)
           ||'/'||to_char(e.sleep3      - b.sleep3)    sleephist
      from dba_hist_latch_parent b
         , dba_hist_latch_parent e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.latch_hash      = e.latch_hash
       and e.sleeps - b.sleeps > 0
     order by parent;

-------------------------------------------Child Latch Statistics
select /*+ ordered use_hash(b) */
           e.latch_name                                name
         , e.child#                                    child
         , e.gets        - b.gets                      gets
         , e.misses      - b.misses                    misses
         , e.sleeps      - b.sleeps                    sleeps
         , to_char(e.spin_gets          - b.spin_gets)
           ||'/'||to_char(e.sleep1      - b.sleep1)
           ||'/'||to_char(e.sleep2      - b.sleep2)
           ||'/'||to_char(e.sleep3      - b.sleep3)    sleephist
      from dba_hist_latch_children e
         , dba_hist_latch_children b
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.latch_hash      = e.latch_hash
       and b.child#          = e.child#
       and e.sleeps - b.sleeps > 0
       and   (e.sleeps - b.sleeps)
           / decode(e.gets - b.gets, 0, 1, e.gets - b.gets) > .00001
     order by name, gets desc;
------------------------------------------Segments by Logical Reads
select owner, tablespace_name, object_name, subobject_name,
           object_type, logical_reads, ratio
    from (select n.owner, n.tablespace_name, n.object_name,
                 case when length(n.subobject_name) < 11 then
                    n.subobject_name
                 else
                    substr(n.subobject_name,length(n.subobject_name)-9)
                 end subobject_name,
                 n.object_type, r.logical_reads,
                 decode(:gets, 0, to_number(null),100 * logical_reads / :gets) ratio
          from dba_hist_seg_stat_obj n,
               (select dataobj#, obj#, dbid,
                       sum(logical_reads_delta) logical_reads
                from dba_hist_seg_stat
                where :b_snap_id              < snap_id
                  and snap_id          <= :e_snap_id
                  and dbid              = :b_dbid
                  and instance_number   = 1
                group by dataobj#, obj#, dbid) r
          where n.dataobj#      = r.dataobj#
            and n.obj#          = r.obj#
            and n.dbid          = r.dbid
            and r.logical_reads > 0
          order by r.logical_reads desc, object_name, owner, subobject_name)
    where rownum <= 5;
------------------------------------------Segments by Physical Reads
select owner, tablespace_name, object_name, subobject_name,
           object_type, physical_reads, ratio
    from (select n.owner, n.tablespace_name, n.object_name,
                 case when length(n.subobject_name) < 11 then
                    n.subobject_name
                 else
                    substr(n.subobject_name,length(n.subobject_name)-9)
                 end subobject_name,
                 n.object_type, r.physical_reads,
                 decode(:phyr, 0, to_number(null),
                        100 * r.physical_reads / :phyr) ratio
          from dba_hist_seg_stat_obj n,
               (select dataobj#, obj#, dbid,
                       sum(physical_reads_delta) physical_reads
                from dba_hist_seg_stat
                where :b_snap_id             < snap_id
                  and snap_id          <= :e_snap_id
                  and dbid              = :b_dbid
                  and instance_number   = 1
                group by dataobj#, obj#, dbid) r
          where n.dataobj#      = r.dataobj#
            and n.obj#          = r.obj#
            and n.dbid          = r.dbid
            and r.physical_reads > 0
          order by r.physical_reads desc, object_name, owner, subobject_name)
    where rownum <= 5;
----------------------------------------------------Segments by Row Lock Waits
select owner, tablespace_name, object_name, subobject_name,
           object_type, row_lock_waits, ratio
    from (select n.owner, n.tablespace_name, n.object_name,
                 case when length(n.subobject_name) < 11 then
                    n.subobject_name
                 else
                    substr(n.subobject_name,length(n.subobject_name)-9)
                 end subobject_name,
                 n.object_type, r.row_lock_waits,
                 round(r.ratio * 100, 2) ratio
          from dba_hist_seg_stat_obj n,
               (select dataobj#, obj#, dbid,
                       sum(row_lock_waits_delta) row_lock_waits,
                       ratio_to_report(sum(row_lock_waits_delta))
                       over () ratio
                from dba_hist_seg_stat
                where :b_snap_id           < snap_id
                  and snap_id          <= :e_snap_id
                  and dbid              = :b_dbid
                  and instance_number   = 1
                group by dataobj#, obj#, dbid) r
          where n.dataobj#      = r.dataobj#
            and n.obj#          = r.obj#
            and n.dbid          = r.dbid
            and r.row_lock_waits > 0
          order by r.row_lock_waits desc, object_name, owner, subobject_name)
    where rownum <= 5;
--------------------------------------------Segments by ITL Waits
select owner, tablespace_name, object_name, subobject_name,
           object_type, itl_waits, ratio
    from (select n.owner, n.tablespace_name, n.object_name,
                 case when length(n.subobject_name) < 11 then
                    n.subobject_name
                 else
                    substr(n.subobject_name,length(n.subobject_name)-9)
                 end subobject_name,
                 n.object_type, r.itl_waits,
                 round(r.ratio * 100, 2) ratio
          from dba_hist_seg_stat_obj n,
               (select dataobj#, obj#, dbid,
                       sum(itl_waits_delta) itl_waits,
                       ratio_to_report(sum(itl_waits_delta))
                       over () ratio
                from dba_hist_seg_stat
                where :b_snap_id            < snap_id
                  and snap_id          <= :e_snap_id
                  and dbid              = :b_dbid
                  and instance_number   = 1
                group by dataobj#, obj#, dbid) r
          where n.dataobj#      = r.dataobj#
            and n.obj#          = r.obj#
            and n.dbid          = r.dbid
            and r.itl_waits > 0
          order by r.itl_waits desc, object_name, owner, subobject_name)
    where rownum <= 5;
-------------------------------------------Segments by Buffer Busy Waits
select owner, tablespace_name, object_name, subobject_name,
           object_type, buffer_busy_waits, ratio
    from (select n.owner, n.tablespace_name, n.object_name,
                 case when length(n.subobject_name) < 11 then
                    n.subobject_name
                 else
                    substr(n.subobject_name,length(n.subobject_name)-9)
                 end subobject_name,
                 n.object_type, r.buffer_busy_waits,
                 round(r.ratio * 100, 2) ratio
          from dba_hist_seg_stat_obj n,
               (select dataobj#, obj#, dbid,
                       sum(buffer_busy_waits_delta) buffer_busy_waits,
                       ratio_to_report(sum(buffer_busy_waits_delta))
                       over () ratio
                from dba_hist_seg_stat
                where :b_snap_id              < snap_id
                  and snap_id          <= :e_snap_id
                  and dbid              = :b_dbid
                  and instance_number   = 1
                group by dataobj#, obj#, dbid) r
          where n.dataobj#      = r.dataobj#
            and n.obj#          = r.obj#
            and n.dbid          = r.dbid
            and r.buffer_busy_waits > 0
          order by r.buffer_busy_waits desc, object_name, owner, subobject_name)
    where rownum <= 5;
--------------------------------------------Dictionary Cache Stats
select lower(b.parameter)                                        param
         , e.gets - b.gets                                           gets
         , to_number(decode(e.gets,b.gets,null,
           (e.getmisses - b.getmisses) * 100/(e.gets - b.gets)))     getm
         , e.scans - b.scans                                         scans
         , to_number(decode(e.scans,b.scans,null,
           (e.scanmisses - b.scanmisses) * 100/(e.scans - b.scans))) scanm
         , e.modifications - b.modifications                         mods
         , e.usage                                                   usage
      from dba_hist_rowcache_summary b
         , dba_hist_rowcache_summary e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.parameter       = e.parameter
       and e.gets - b.gets   > 0
     order by param;
--------------------------------------------Library Cache Activity
select b.namespace
         , e.gets - b.gets                                         gets  
         , to_number(decode(e.gets,b.gets,null,
           100 - (e.gethits - b.gethits) * 100/(e.gets - b.gets))) getm
         , e.pins - b.pins                                         pins  
         , to_number(decode(e.pins,b.pins,null,
           100 - (e.pinhits - b.pinhits) * 100/(e.pins - b.pins))) pinm
         , e.reloads - b.reloads                                   reloads
         , e.invalidations - b.invalidations                       inv
      from dba_hist_librarycache b
         , dba_hist_librarycache e
     where b.snap_id         = :b_snap_id
       and e.snap_id         = :e_snap_id
       and b.dbid            = :b_dbid
       and e.dbid            = :b_dbid
       and b.instance_number = 1
       and e.instance_number = 1
       and b.namespace       = e.namespace
       and e.gets - b.gets   > 0;
---------------------------------------------Process Memory Summary
select decode(snap_id, :b_snap_id, 'B', :e_snap_id, 'E') b_or_e,
               category,
               allocated_total  /1024/1024,
               used_total       /1024/1024,
               allocated_avg    /1024/1024,
               allocated_stddev /1024/1024,
               allocated_max    /1024/1024,
               max_allocated_max/1024/1024,
               num_processes,
               non_zero_allocs
          from dba_hist_process_mem_summary
          where dbid            = :b_dbid
            and instance_number = 1
            and snap_id         in (:b_snap_id, :e_snap_id)
       order by snap_id, allocated_total desc;
-------------------------------------------SGA Memory Summary
select s1.name, s1.value/1024, s2.value/1024
from dba_hist_sga s1, dba_hist_sga s2
where s1.snap_id         = :b_snap_id
   and s1.dbid            = :b_dbid
   and s1.instance_number = 1
   and s2.snap_id         = :e_snap_id
   and s2.dbid            = :b_dbid
   and s2.instance_number = 1
   and s1.name            = s2.name
order by name;
------------------------------------------SGA breakdown difference
select replace(pool,'pool', '')  pool,
           name, snap1, snap2, diff
     from (select nvl(e.pool, b.pool)             pool
                , nvl(e.name, b.name)             name
                , b.bytes/1024/1024               snap1
                , e.bytes/1024/1024               snap2
                , decode(b.bytes, NULL, to_number(NULL),
                         100*(nvl(e.bytes,0) - b.bytes)/b.bytes) diff
            from (select *
                    from dba_hist_sgastat
                   where snap_id         = :b_snap_id
                     and dbid            = :b_dbid
                     and instance_number = 1
                 ) b
                 full outer join
                 (select *
                    from dba_hist_sgastat
                   where snap_id         = :e_snap_id
                     and dbid            = :b_dbid
                     and instance_number = 1
                 ) e
                 on b.name             = e.name
                 and nvl(b.pool, 'a')  = nvl(e.pool, 'a')
            order by nvl(e.bytes, b.bytes))
     order by pool, name;
------------------------------------Streams CPU/IO Usage
select e.session_type
        , e.sum_cpu_time - nvl(b.sum_cpu_time,0) cpu
        , e.sum_user_io_wait - nvl(b.sum_user_io_wait,0)
        , e.sum_sys_io_wait - nvl(b.sum_sys_io_wait,0)
        from dba_hist_sess_time_stats b
             , dba_hist_sess_time_stats e
        where b.snap_id        (+)= :b_snap_id
          and e.snap_id           = :e_snap_id
          and b.dbid           (+)= e.dbid
          and e.dbid              = :b_dbid
          and b.instance_number(+)= e.instance_number
          and e.instance_number   = 1
          and b.min_logon_time (+)= e.min_logon_time
          and b.session_type   (+)= e.session_type
       order by cpu desc;
------------------------------------Streams Capture
select e.capture_name capname
         , (e.total_messages_captured - nvl(b.total_messages_captured,0))/:ela
         , (e.total_messages_enqueued - nvl(b.total_messages_enqueued,0))/:ela
         , (e.lag - nvl(b.lag,0))*24*60*60
         , (e.elapsed_rule_time - nvl(b.elapsed_rule_time,0))/:ela
         , (e.elapsed_enqueue_time - nvl(b.elapsed_enqueue_time,0))/:ela
         , (e.elapsed_redo_wait_time - nvl(b.elapsed_redo_wait_time,0))/:ela
         , (e.elapsed_pause_time - nvl(b.elapsed_pause_time,0))/:ela
      from dba_hist_streams_capture b
         , dba_hist_streams_capture e
     where b.snap_id        (+)= :b_snap_id
       and e.snap_id           = :e_snap_id
       and b.dbid           (+)= e.dbid
       and e.dbid              = :b_dbid
       and b.instance_number(+)= e.instance_number
       and e.instance_number   = 1
       and b.startup_time   (+)= e.startup_time
       and b.capture_name   (+)= e.capture_name
     order by capname;
--------------------------------------Streams Apply
select e.apply_name appname
  , (e.coord_total_applied - nvl(b.coord_total_applied,0))/:ela
  , decode(:tran,0,0,
           100*(e.coord_total_applied - nvl(b.coord_total_applied,0))/:tran)
  , decode((e.coord_total_applied - nvl(b.coord_total_applied,0)),0,0,
           ((e.coord_total_wait_deps - nvl(b.coord_total_wait_deps,0))*100)
            / (e.coord_total_applied - nvl(b.coord_total_applied,0)))
  , decode((e.coord_total_applied - nvl(b.coord_total_applied,0)),0,0,
    ((e.coord_total_wait_cmts - nvl(b.coord_total_wait_cmts,0))*100)
    / (e.coord_total_applied - nvl(b.coord_total_applied,0)))
  , decode((e.coord_total_applied - nvl(b.coord_total_applied,0)),0,0,
    ((e.coord_total_rollbacks - nvl(b.coord_total_rollbacks,0))*100)
    / (e.coord_total_applied - nvl(b.coord_total_applied,0)))
  , (e.server_total_messages_applied - nvl(b.server_total_messages_applied,0))
    /:ela
  , decode(e.server_total_messages_applied
           - nvl(b.server_total_messages_applied,0), 0, 0,
      10*(e.server_elapsed_dequeue_time - nvl(b.server_elapsed_dequeue_time,0))
  / (e.server_total_messages_applied - nvl(b.server_total_messages_applied,0)))
  , decode(e.server_total_messages_applied
           - nvl(b.server_total_messages_applied,0), 0, 0,
      10*(e.server_elapsed_apply_time - nvl(b.server_elapsed_apply_time,0))
  / (e.server_total_messages_applied - nvl(b.server_total_messages_applied,0)))
  , (e.coord_lwm_lag - nvl(b.coord_lwm_lag, 0))*24*60*60
      from dba_hist_streams_apply_sum b
         , dba_hist_streams_apply_sum e
     where b.snap_id         (+)= :b_snap_id
       and e.snap_id            = :e_snap_id
       and e.dbid               = :b_dbid
       and b.dbid            (+)= e.dbid
       and e.instance_number    = 1
       and b.instance_number (+)= e.instance_number
       and b.startup_time    (+)= e.startup_time
       and b.apply_name      (+)= e.apply_name
     order by appname;
---------------------------------------Buffered Queues
select e.queue_schema||'.'||e.queue_name queuename
         , (e.cnum_msgs - nvl(b.cnum_msgs,0))/:ela
         , (e.cnum_msgs-e.num_msgs - nvl((b.cnum_msgs-b.num_msgs),0))/:ela
         , (e.cspill_msgs - nvl(b.cspill_msgs,0))/:ela spillrate
         , (decode(e.num_msgs,0,0,(e.spill_msgs/e.num_msgs)) -
            nvl(decode(b.num_msgs,0,0,(b.spill_msgs/b.num_msgs)),0))*100
      from dba_hist_buffered_queues b
         , dba_hist_buffered_queues e
     where b.snap_id        (+)= :b_snap_id
       and e.snap_id           = :e_snap_id
       and e.dbid              = :b_dbid
       and b.dbid           (+)= e.dbid
       and e.instance_number   = 1
       and b.instance_number(+)= e.instance_number
       and b.queue_schema   (+)= e.queue_schema
       and b.queue_name     (+)= e.queue_name
     order by spillrate desc, queuename;
----------------------------------------Buffered Subscribers
select decode(e.subscriber_type,'PROXY','PROXY: '||
           e.subscriber_address,e.subscriber_name) subsname
         , (e.cnum_msgs - nvl(b.cnum_msgs,0))/:ela
         , (e.cnum_msgs-e.num_msgs - nvl((b.cnum_msgs-b.num_msgs),0))/:ela
         , (e.total_spilled_msg - nvl(b.total_spilled_msg,0))/:ela spillrate
       from dba_hist_buffered_subscribers b
         , dba_hist_buffered_subscribers e
     where b.snap_id        (+)= :b_snap_id
       and e.snap_id           = :e_snap_id
       and e.dbid              = :b_dbid
       and b.dbid           (+)= e.dbid
       and e.instance_number   = 1
       and b.instance_number(+)= e.instance_number
       and b.subscriber_id  (+)= e.subscriber_id
     order by spillrate desc, subsname;
-------------------------------------------Rule Set
select e.owner||'.'||e.name rulesetname
        , e.evaluations - nvl(b.evaluations, 0) evaluations
        , e.sql_free_evaluations - nvl(b.sql_free_evaluations, 0)
        , e.sql_executions - nvl(b.sql_executions, 0)
        , e.cpu_time - nvl(b.cpu_time, 0)
        , e.elapsed_time - nvl(b.elapsed_time, 0)
      from dba_hist_rule_set b
         , dba_hist_rule_set e
     where b.snap_id        (+)= :b_snap_id
       and e.snap_id           = :e_snap_id
       and e.dbid              = :b_dbid
       and b.dbid           (+)= e.dbid
       and e.instance_number   = 1
       and b.instance_number(+)= e.instance_number
       and b.owner(+)          = e.owner
       and b.name(+)           = e.name
       and b.startup_time(+)   = e.startup_time
     order by evaluations desc;
--------------------------------------------Resource Limit Stats
select resource_name         rname
         , current_utilization   curu
         , max_utilization       maxu
         , initial_allocation    inita
         , limit_value           lim
      from dba_hist_resource_limit
     where snap_id         = :e_snap_id
       and dbid            = :b_dbid
       and instance_number = 1
       and (   nvl(current_utilization,0)/limit_value > .8
            or nvl(max_utilization,0)/limit_value     > .8
           )
     order by rname;
---------------------------------------------init.ora Parameters
select e.parameter_name             name
         , b.value                      bval
         , decode(b.value, e.value, NULL, e.value) eval
      from dba_hist_parameter b
         , dba_hist_parameter e
     where b.snap_id(+)         = :b_snap_id
       and e.snap_id            = :e_snap_id
       and b.dbid(+)            = :b_dbid
       and e.dbid               = :b_dbid
       and b.instance_number(+) = 1
       and e.instance_number    = 1
       and b.parameter_hash(+)  = e.parameter_hash
       and (   nvl(b.isdefault, 'X')   = 'FALSE'
            or nvl(b.ismodified,'X')  != 'FALSE'
            or     e.ismodified       != 'FALSE'
            or nvl(e.value,0)         != nvl(b.value,0)
           )
       and e.parameter_name not like '\_\_%' escape '\'
     order by e.parameter_name;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25373498/viewspace-2134837/,如需轉載,請註明出處,否則將追究法律責任。

相關文章