What’s running on the database?

A quick at-the-command-line, one-query overview of which sessions are running, what they’re waiting on and the SQL they’re running:

set linesize 512
set pagesize 4000
col sid format 99999
col serial# format 99999
col session format a25
col username format a12
col osuser format a12
col state format a14
col event format a20
col partial_sql format a30
col latchtype format a20
col comp% format 99.99
col hash_value format 99999999999
col calltime format 99999
With 
Sessions as (select sid, serial#, last_call_et, username, machine, osuser, status 
             from v$session where serial#<>1),
Longops as (select sid, serial#, 
                   elapsed_seconds as elaptime, 
                   round(sofar/totalwork*100,2) as pcomplete 
            from v$session_longops where time_remaining>0),
Waiting as (select sid, state, event, seconds_in_wait, wait_time 
            from v$session_wait),
Running as (select a.sid, a.serial#, substr(b.sql_text,1,30) as sql, 
                   b.hash_value, b.sql_id 
            from v$session a, v$sqlarea b 
            where a.sql_address=b.address),
Latching as (select s.sid, lc.name as latchname 
             from v$latch_children lc, v$session_wait w, v$session s 
             where latch#=w.p2 and lc.addr=w.p1raw and s.sid=w.sid)
select  s.sid, 
  s.serial#, 
  s.username, 
  s.osuser,
  s.status, 
  s.last_call_et calltime,
  l.elaptime,
  l.pcomplete "COMP%",
  case when w.state = 'WAITED SHORT TIME' then 'WAITED S.T.' 
    when w.state = 'WAITED KNOWN TIME' then 'WAITED K.T.'
    else w.state end state,
  substr(replace(replace(replace(replace(w.event,'sequential','seq.'),'scattered','sctrd.'),'db file','db'),'SQL*Net message','SQL msg'),1,20) event,
  case when event = 'latch free' then ch.latchname else NULL end latchtype,
  w.seconds_in_wait as wait_secs, 
  r.hash_value,
  r.sql_id, 
  r.sql as Partial_SQL
from 
  sessions s, 
  longops l,
  waiting w,
  running r,
  latching ch
where  s.sid=l.sid(+)
and  s.sid=w.sid(+)
and  s.sid=r.sid(+)
and  s.sid=ch.sid(+)
and  s.serial#=r.serial#(+)
and  s.serial#=l.serial#(+)
and  s.status = 'ACTIVE'
and  s.username is not null; 

Works in a Putty session that’s set fullscreen!

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>