DBAtlas · Database operations atlas

Top wait events and top SQL from ASH for the last minutes

OraclePerformance

Command

-- Last 30 min: wait event breakdown (each sample ~1 s of active session time)
SELECT NVL(event, 'ON CPU') AS event, wait_class,
       COUNT(*) AS samples,
       ROUND(RATIO_TO_REPORT(COUNT(*)) OVER () * 100, 1) AS pct
FROM   v$active_session_history
WHERE  sample_time > SYSTIMESTAMP - INTERVAL '30' MINUTE
GROUP  BY NVL(event, 'ON CPU'), wait_class
ORDER  BY samples DESC
FETCH FIRST 10 ROWS ONLY;

-- Last 30 min: top SQL (with CPU / User I/O breakdown)
SELECT sql_id, sql_opname,
       COUNT(*) AS samples,
       SUM(CASE WHEN session_state = 'ON CPU' THEN 1 ELSE 0 END) AS cpu,
       SUM(CASE WHEN wait_class = 'User I/O' THEN 1 ELSE 0 END)  AS user_io,
       ROUND(RATIO_TO_REPORT(COUNT(*)) OVER () * 100, 1) AS pct
FROM   v$active_session_history
WHERE  sample_time > SYSTIMESTAMP - INTERVAL '30' MINUTE
  AND  sql_id IS NOT NULL
GROUP  BY sql_id, sql_opname
ORDER  BY samples DESC
FETCH FIRST 10 ROWS ONLY;

-- Older periods: DBA_HIST_ACTIVE_SESS_HISTORY (one sample every 10 s; COUNT(*)*10 = seconds)

-- Ready-made ASH report (last 30 min):
SET LINESIZE 8000 PAGESIZE 0 TRIMSPOOL ON HEADING OFF FEEDBACK OFF
SELECT output
FROM   TABLE(DBMS_WORKLOAD_REPOSITORY.ASH_REPORT_TEXT(
         l_dbid     => (SELECT dbid FROM v$database),
         l_inst_num => (SELECT instance_number FROM v$instance),
         l_btime    => SYSDATE - 30/1440,
         l_etime    => SYSDATE));
-- Interactive: @?/rdbms/admin/ashrpt.sql

What it does

Uses Active Session History samples to show, as percentages, which wait events and which SQL statements consumed DB time in the last minutes; diagnoses the incident without waiting for an AWR snapshot.

When to use it

During a live slowdown, for short spikes that disappear in AWR averages, and to see what a specific session or SQL was waiting on.

Caution

Querying V$ACTIVE_SESSION_HISTORY and DBA_HIST_ACTIVE_SESS_HISTORY requires the Diagnostics Pack license. ASH is sampled: sample counts estimate time, they are not exact measurements, and very short SQL may not appear. The in-memory ASH buffer holds only a few hours on busy systems.

Open in DBAtlas →

Related