Oracle Session活动持续10秒

时间:2018-08-31 15:49:31

标签: oracle

我想将此查询转换为最后10秒而不是最后几个小时

SELECT TO_CHAR(SAMPLE_TIME, 'HH24:MI ') AS SAMPLE_TIME,
       ROUND(OTHER / 60, 3) AS OTHER,
       ROUND(CLUST / 60, 3) AS CLUST,
       ROUND(QUEUEING / 60, 3) AS QUEUEING,
       ROUND(NETWORK / 60, 3) AS NETWORK,
       ROUND(ADMINISTRATIVE / 60, 3) AS ADMINISTRATIVE,
       ROUND(CONFIGURATION / 60, 3) AS CONFIGURATION,
       ROUND(COMMIT / 60, 3) AS COMMIT,
       ROUND(APPLICATION / 60, 3) AS APPLICATION,
       ROUND(CONCURRENCY / 60, 3) AS CONCURRENCY,
       ROUND(SIO / 60, 3) AS SYSTEM_IO,
       ROUND(UIO / 60, 3) AS USER_IO,
       ROUND(SCHEDULER / 60, 3) AS SCHEDULER,
       ROUND(CPU / 60, 3) AS CPU,
       ROUND(BCPU / 60, 3) AS BACKGROUND_CPU
  FROM (SELECT TRUNC(SAMPLE_TIME, 'MI') AS SAMPLE_TIME,
               DECODE(SESSION_STATE,
                      'ON CPU',
                      DECODE(SESSION_TYPE, 'BACKGROUND', 'BCPU', 'ON CPU'),
                      WAIT_CLASS) AS WAIT_CLASS
          FROM V$ACTIVE_SESSION_HISTORY
         WHERE SAMPLE_TIME > SYSDATE - INTERVAL '1'HOUR AND SAMPLE_TIME <= TRUNC(SYSDATE, 'MI')) ASH PIVOT(COUNT(*) 
  FOR WAIT_CLASS IN('ON CPU' AS CPU,'BCPU' AS BCPU,'Scheduler' AS SCHEDULER,'User I/O' AS UIO,'System I/O' AS SIO, 'Concurrency' AS CONCURRENCY, 
'Application' AS  APPLICATION,COMMIT,                                                                             
'Configuration' AS CONFIGURATION,                     
'Administrative' AS   ADMINISTRATIVE,                                                                                 
'Network' AS  NETWORK,                                                                                 
'Queueing' AS   QUEUEING,                                                                                  
'Cluster' AS   CLUST,                                                                                      
'Other' AS  OTHER))
ORDER BY 1  

我尝试使用此WHERE SAMPLE_TIME> SYSDATE-(10)/(24 * 60 * 60) 但它不起作用。它在几分钟内给了我结果。但是我想要最后10秒(所以10行)

1 个答案:

答案 0 :(得分:0)

您需要更改外部选择列表以包括秒数:

SELECT TO_CHAR(SAMPLE_TIME, 'HH24:MI:SS') AS SAMPLE_TIME,

您需要从内部查询选择列表中删除分钟级别的截断:

  FROM (SELECT SAMPLE_TIME,

您需要更改采样时间过滤器(由于该列是一个时间戳,不妨使用systimestamp和一个间隔,使其保持为一个):

         WHERE SAMPLE_TIME > SYSTIMESTAMP - INTERVAL '10' SECOND
  )
  ASH PIVOT(COUNT(*) 

并且如注释中所述,您的COMMIT应该为'Commit' as COMMIT,否则,您至少在11gR2、12cR1中会收到“ ORA-56901:枢轴值|非枢轴值不允许使用非恒定表达式”和12cR2;并且您需要提供ASH视图中显示的实际wait_class名称。

通过这些更改,查询似乎可以满足您的要求。但是不一定会显示10行,因为每秒可能没有采样。例如在一个非常安静的数据库上,我通常没有行或只有几行。

SELECT TO_CHAR(SAMPLE_TIME, 'HH24:MI:SS ') AS SAMPLE_TIME,
       ROUND(OTHER / 60, 3) AS OTHER,
       ROUND(CLUST / 60, 3) AS CLUST,
       ROUND(QUEUEING / 60, 3) AS QUEUEING,
       ROUND(NETWORK / 60, 3) AS NETWORK,
       ROUND(ADMINISTRATIVE / 60, 3) AS ADMINISTRATIVE,
       ROUND(CONFIGURATION / 60, 3) AS CONFIGURATION,
       ROUND(COMMIT / 60, 3) AS COMMIT,
       ROUND(APPLICATION / 60, 3) AS APPLICATION,
       ROUND(CONCURRENCY / 60, 3) AS CONCURRENCY,
       ROUND(SIO / 60, 3) AS SYSTEM_IO,
       ROUND(UIO / 60, 3) AS USER_IO,
       ROUND(SCHEDULER / 60, 3) AS SCHEDULER,
       ROUND(CPU / 60, 3) AS CPU,
       ROUND(BCPU / 60, 3) AS BACKGROUND_CPU
  FROM (SELECT SAMPLE_TIME AS SAMPLE_TIME,
             DECODE(SESSION_STATE,
                    'ON CPU',
                    DECODE(SESSION_TYPE, 'BACKGROUND', 'BCPU', 'ON CPU'),
                    WAIT_CLASS) AS WAIT_CLASS
          FROM V$ACTIVE_SESSION_HISTORY
         WHERE SAMPLE_TIME > SYSTIMESTAMP - INTERVAL '10' second
  ) ASH
PIVOT (
  COUNT(*) 
  FOR WAIT_CLASS IN ('ON CPU' AS CPU,
    'BCPU' AS BCPU,
    'Scheduler' AS SCHEDULER,
    'User I/O' AS UIO,
    'System I/O' AS SIO,
    'Concurrency' AS CONCURRENCY, 
    'Application' AS  APPLICATION,
    'Commit' as COMMIT,
    'Configuration' AS CONFIGURATION,
    'Administrative' AS   ADMINISTRATIVE,
    'Network' AS  NETWORK,
    'Queueing' AS   QUEUEING,
    'Cluster' AS   CLUST,
    'Other' AS  OTHER)
)
ORDER BY 1  
/
SAMPLE_TI      OTHER      CLUST   QUEUEING    NETWORK ADMINISTRATIVE CONFIGURATION     COMMIT APPLICATION CONCURRENCY  SYSTEM_IO    USER_IO  SCHEDULER        CPU BACKGROUND_CPU
--------- ---------- ---------- ---------- ---------- -------------- ------------- ---------- ----------- ----------- ---------- ---------- ---------- ---------- --------------
17:57:26           0          0          0          0              0             0          0           0           0          0          0          0       .017              0