获得消耗cpu较高的topsql



一、
SELECT a.snap_id,
       a.dbid,
       a.instance_number,
       DB_TIME,
       to_char(TRUNC((DB_TIME - lag(DB_TIME, 1, DB_TIME)
                      over(PARTITION BY stat_name,
                           instance_number ORDER BY snap_id)) / 1000000 / 60,
                     2)) DB_TIME_
  FROM (SELECT a.snap_id,
               a.dbid,
               a.instance_number,
               a.stat_name,
               SUM(a.value) DB_TIME
          FROM DBA_HIST_SYS_TIME_MODEL a
         WHERE a.stat_name = 'DB time'
         --AND a.instance_number = 2
         GROUP BY a.snap_id, a.dbid, a.instance_number, a.stat_name) a;

SELECT a.snap_id,
       a.dbid,
       a.instance_number,
       DB_TIME,
/*    to_char(TRUNC((DB_TIME - lag(DB_TIME, 1, DB_TIME)
                      over(PARTITION BY stat_name,
                           instance_number ORDER BY snap_id)) / 1000000 / 60,
                     2)) DB_TIME_,   */
       b.END_INTERVAL_TIME
  FROM (SELECT a.snap_id,
               a.dbid,
               a.instance_number,
               a.stat_name,
               SUM(a.value) DB_TIME
          FROM DBA_HIST_SYS_TIME_MODEL a
         WHERE a.stat_name = 'DB time'
         GROUP BY a.snap_id, a.dbid, a.instance_number, a.stat_name) a,dba_hist_snapshot b where a.snap_id=b.snap_id;
执行上面两个语句得到数据之后按照snap_id对应起来,可以得到snap_id、DB_TIME_、END_INTERVAL_TIME这仨个字段,就可以确定某个时间点的db_time值与snap_id所对应的具体的时间。
 

然后把相应时间段的snap_id代入下面的语句即可得到top10的语句

select  to_char(t.INSTANCE_NUMBER) i_n,t.sql_id,to_char(t.username) username,
to_char(t.disk_reads) d_r,to_char(t.buffer_gets_nvl) buffer_nvl,t.bget buffer_gets,t.elap_time,t.cpu_time,
t.exec,t.elap_nvl,t.sql_text
from
(select  sqt.bget,
         sqt.exec,
 username,
 INSTANCE_NUMBER,
 disk_reads,
     decode(sqt.exec, 0, to_number(null) , (sqt.bget / sqt.exec)) buffer_gets_nvl,
     nvl((sqt.cput / 1000000), to_number(null)) cpu_time ,
 nvl((sqt.elap / 1000000), to_number(null)) elap_time,
 (sqt.elap / 1000000) /  sqt.exec  elap_nvl,
 sqt.sql_id,
 st.sql_text
from
(select sql_id,parsing_schema_name username,INSTANCE_NUMBER,
       sum(buffer_gets_delta) bget,
       sum(executions_delta) exec,
       sum(cpu_time_delta) cput,
       sum(elapsed_time_delta) elap,
   sum(DISK_READS_DELTA) disk_reads
from dba_hist_sqlstat
where snap_id>=30281 and snap_id<=30307   ---------------把相关时间点的snap_id写到这里即可
group by sql_id,parsing_schema_name,INSTANCE_NUMBER) sqt,
dba_hist_sqltext st
where st.sql_id(+) = sqt.sql_id
and sqt.exec != 0
and st.sql_text not like '%DUAL%%'
order by nvl((sqt.cput / 1000000), to_number(null)) desc, sqt.sql_id ,username,INSTANCE_NUMBER,disk_reads)  t
 where rownum < 65
   and (rownum <= 10);

你可能感兴趣的:(oracle-小记)