Active Session History

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

set lines 180 pages 600

select trunc(last_active_time) as "Run date"


, to_char(min(last_active_time),'HH24:MI:SS') as "First"
, to_char(max(last_active_time),'HH24:MI:SS') as "Last"
, plan_hash_value as "Plan hash"
, sum(executions_calc) as "Times called"
, sum(end_of_fetch_count) as "Times completed"
, least(100, round(100 * sum(end_of_fetch_count) /
sum(executions_calc),1)) as "Success %"
, cast(numtodsinterval(sum(elapsed_time)/1E6,'SECOND') as interval
day(1) to second(2)) as "Total time"
-- , round(sum(elapsed_time)/1e6) as "Total seconds"
, round(sum(elapsed_time)/1E6 / nvl(sum(executions_calc),1),1) as
"Average (s)"
, round(sum(elapsed_time)/1E6 /
nvl(sum(executions_calc),1)/nvl(nullif(sum(px_servers_executions),0),1)) as
"Average (s) PX"
, cast(numtodsinterval(sum(elapsed_time)/1E6 /
nvl(sum(executions_calc),1),'SECOND') as interval day(1) to second(1)) as "Average
time"
, cast(numtodsinterval(sum(elapsed_time)/1E6 /
nvl(sum(executions_calc),1) /nvl(nullif(sum(px_servers_executions),0),1),'SECOND')
as interval day(1) to second(1)) as "Average time PX"
-- , sum(buffer_gets) as "Buffer gets"
, round(sum(buffer_gets)/sum(executions_calc)) as "Buffer gets/exec"
, round(sum(buffer_gets)/nullif(sum(rows_processed),0)) as "Buffer
gets/row"
, sum(rows_processed) as "Rows"
, round(sum(rows_processed) / sum(executions_calc)) as "Rows/exec"
, cast(numtodsinterval(sum(elapsed_time)/nullif(sum(rows_processed),0)/
1E6,'SECOND') as interval day(1) to second(3)) as "Avg time/row"
, cast
( case
when sum(elapsed_time)/nullif(sum(rows_processed),0) < 2147483647
then -- 2**31 -1, limit for 32 bit integers

numtodsinterval(sum(elapsed_time)/nullif(sum(rows_processed),0),'SECOND')
else
numtodsinterval(sum(elapsed_time/3600)/nullif(sum(rows_processed),0),'HOUR')
end -- Avoid "ORA-01873: the leading precision of the interval is
too small" for large values
as interval day(5) to second(0)) as "Avg time/1M rows"
, sum(px_servers_executions) as "PX server executions"
from ( select s.sql_id
, s.last_active_time
, plan_hash_value
--, executions
, case executions when 0 then 1 else executions end as
executions_calc -- to use in expressions without NULLIF
, px_servers_executions
, elapsed_time
, buffer_gets
, rows_processed
, end_of_fetch_count
from v$sqlstats s )
where sql_id = '&sql_id'
group by trunc(last_active_time), plan_hash_value
having sum(executions_calc) > 0
order by trunc(last_active_time), plan_hash_value;
Session count with granularity every one minute:--

set lines 300


select FORMAT(BEGIN_INTERVAL_TIME, 'MM-dd-yyyy HH:mm'),count(*) from
dba_hist_snapshot a,dba_hist_active_sess_history b where a.BEGIN_INTERVAL_TIME
>to_date('07-SEP-2023 19:00:00','dd-mon-yy hh24:mi:ss') and a.BEGIN_INTERVAL_TIME <
to_date('07-SEP-2023 19:30:00','dd-mon-yy hh24:mi:ss')
and a.snap_id=b.snap_id
group by FORMAT(BEGIN_INTERVAL_TIME, 'MM-dd-yyyy HH:mm') order by 1;

select distinct(sql_id) from dba_hist_active_sess_history where


SESSION_ID='447';

select * from dba_hist_active_sess_history where event like 'enq: TX - row


lock contention%' order by sample_time desc;

SELECT ss.instance_number,
to_char(force_matching_signature) as force_matching_signature, stat.sql_id,
plan_hash_value, stat.sql_profile,
executions_delta Exe_D, executions_total Exe_T,
round(elapsed_time_delta/1000000,2) Secs_D,
round(elapsed_time_total/1000000,2) Secs_T,
rows_processed_delta Rows_D, rows_processed_total Rows_T,
disk_reads_delta Reads_D, disk_reads_total Reads_T,
ss.begin_interval_time,
ss.end_interval_time, stat.snap_id,
ss.startup_time, flush_elapsed,
parsing_schema_name as parsing_nm,
fetches_delta Fetch_D,
end_of_fetch_count_delta End_Of_Fetch_Cnt_D,
sql_text
FROM DBA_HIST_SNAPSHOT ss, DBA_HIST_SQLSTAT stat, DBA_HIST_SQLTEXT txt
WHERE stat.sql_id = txt.sql_id
AND stat.dbid = txt.dbid
AND ss.dbid = stat.dbid
AND ss.instance_number = stat.instance_number
AND ss.snap_id = stat.snap_id
AND ss.dbid in (select dbid from v$database)
AND ss.instance_number = (select instance_number from gv$instance where
instance_name ='pdw2s1')
and ss.begin_interval_time >= SYSDATE - 30
and upper(sql_text) LIKE '%INSERT /*+ APPEND */ INTO AUTH_CLOG_MATCHING%'
--d txt.sql_id='6v5w592b20kuv'
order by ss.end_interval_time;

SELECT * FROM dba_hist_active_sess_history h JOIN dba_hist_snapshot sn ON h.snap_id


= sn.snap_id WHERE sn.begin_interval_time BETWEEN TO_DATE('12/15/00
00:00','MM/DD/YY HH24:MI') AND TO_DATE('12/15/20 12:00','MM/DD/YY HH24:MI') and
session_id='447' and sql_id='gat22zszmcrxz'

----------------------------------------Blocking session
history------------------------------

select
a.sample_time, a.session_id, a.session_state, a.event, a.seq#,
a.time_waited/1000000 time_waited,
a.sql_id, is_sqlid_current, a.sql_opcode, a.blocking_session, current_obj#
from
dba_hist_active_sess_history a
where a.sample_time between to_date('20211026 07:00:00','YYYYMMDD HH24:MI:SS')
and to_date('20211027 07:30:00','YYYYMMDD HH24:MI:SS')
and sql_id = '2whrhbggxn44h'
order by a.sample_time, a.session_id;

set lines 300


select a.SNAP_ID,a.BEGIN_INTERVAL_TIME,b.machine,count(*) from dba_hist_snapshot
a,dba_hist_active_sess_history b where a.BEGIN_INTERVAL_TIME >to_date('23-SEP-2021
07:00:00','dd-mon-yy hh24:mi:ss') and a.BEGIN_INTERVAL_TIME < to_date('23-SEP-2021
10:00:00','dd-mon-yy hh24:mi:ss')
and a.snap_id=b.snap_id
group by a.SNAP_ID,a.BEGIN_INTERVAL_TIME,b.machine order by 1;

col program for a35


col module for a40
col event for a30
col machine for a30
col SESSION_ID for 9999
set pages 300 lines 220
SELECT SESSION_ID, program, module , event ,machine
FROM dba_hist_active_sess_history
WHERE sample_time between to_date('06-10-2022 03:00:00','dd-mm-yyyy hh24:mi:ss')
and sysdate and sql_id='45m4vzfkr0hcx' ;

set line 999


set pagesize 999
set verify off
col module format a30;
BREAK ON REPORT
COLUMN DUMMY HEADING ''
compute sum of PCT_CPU on REPORT
with total_cpu as
(select count(1)
from V$ACTIVE_SESSION_HISTORY
where SESSION_TYPE = 'FOREGROUND')
select module,
count(1) "module_cpu",
(select * from total_cpu) "total_cpu",
round((count(1)/(select * from total_cpu))*100,2) PCT_CPU
from V$ACTIVE_SESSION_HISTORY
where SESSION_TYPE = 'FOREGROUND'
group by module order by PCT_CPU desc;
From <https://avdeo.com/tag/dba_hist_active_sess_history/>

---------------------SQLID Start time and end time


--------------------------------------

SELECT query_runs.*,
ROUND ( (end_time - start_time) * 24, 2) AS duration_hrs
FROM ( SELECT u.username,
ash.program,
ash.sql_id,
ASH.SQL_PLAN_HASH_VALUE as plan_hash_value,
ASH.SESSION_ID as sess#,
ASH.SESSION_SERIAL# as sess_ser,
CAST (MIN (ASH.SAMPLE_TIME) AS DATE) AS start_time,
CAST (MAX (ash.sample_time) AS DATE) AS end_time
FROM dba_hist_active_sess_history ash, dba_users u
WHERE u.user_id = ASH.USER_ID AND ash.sql_id = lower(trim('&sql_id'))
GROUP BY u.username,
ash.program,
ash.sql_id,
ASH.SQL_PLAN_HASH_VALUE,
ASH.SESSION_ID,
ASH.SESSION_SERIAL#) query_runs
ORDER BY sql_id, start_time;

You might also like