DBA_HIST_ACTIVE_SESS_HISTORY: Query duration of SQL statement executions
with ash as (
select
ash.sample_time, -- ash.sample_time_utc,
ash.sql_exec_start,
ash.user_id,
--
row_number() over (partition by ash.session_id, ash.session_serial#, ash.sql_id, ash.sql_exec_id, dbid order by ash.sample_time ) first_rec_for_sql_exec,
row_number() over (partition by ash.session_id, ash.session_serial#, ash.sql_id, ash.sql_exec_id, dbid order by ash.sample_time desc) last_rec_for_sql_exec,
--
ash.sql_id, ash.sql_child_number, ash.sql_exec_id,
ash.dbid, ash.con_dbid
from
sys.dba_hist_active_sess_history ash
)
select
ash.sql_id, ash.sql_child_number, ash.sql_exec_id,
ash.sql_exec_start,
usr.username,
min (case when ash.first_rec_for_sql_exec =1 then ash.sample_time end) first_sample_time,
max (case when ash. last_rec_for_sql_exec =1 then ash.sample_time end) last_sample_time,
cast(substr(txt.sql_text, 1, 1000) as varchar2(1000)),
count(*) cnt_samples
from
ash left join
sys.dba_hist_sqltext txt on ash.sql_id = txt.sql_id and
ash.dbid = txt.dbid and
ash.con_dbid = txt.con_dbid left join
sys.dba_users usr on ash.user_id = usr.user_id
group by
ash.sql_id, ash.sql_child_number, ash.sql_exec_id,
ash.sql_exec_start,
usr.username,
cast(substr(txt.sql_text, 1, 1000) as varchar2(1000))
-- where
-- lower(substr(ash.sql_text, 1, 1000)) like '%insert %tq84_table%'
order by
min (case when ash.first_rec_for_sql_exec =1 then ash.sample_time end) desc
;