ORACLE SQL History ....
https://anuj-singh.blogspot.com/2021/05/sql-history.htmlhttps://anuj-singh.blogspot.com/2021/02/sql-report.htmlhttps://anuj-singh.blogspot.com/2024/09/sql-plan-changed.htmlhttps://anuj-singh.blogspot.com/2025/01/coexfrsqlprofilesql-example.htmlhttps://anuj-singh.blogspot.com/2025/ Oracle Sql_id Stale statisticsdefine sql_id='XXXXXXX' set long 30000 pagesize 500 linesize 300 col frm heading from select * from (select 'gv$sql' frm , sql_fulltext sql_text from gv$sql where sql_id='&&sql_id' union all select 'gv$sqlstats' frm , sql_fulltext sql_text from gv$sqlstats where sql_id='&&sql_id' union all select 'dba_hist', sql_text from dba_hist_sqltext where sql_id='&&sql_id' ) where 1=1 and rownum<2 ;==set linesize 300 pagesize 300col FULL_SQL_TEXT for a100 SELECT sql_id, LISTAGG(sql_text, '') WITHIN GROUP (ORDER BY piece) AS full_sql_text FROM v$sqltext WHERE sql_id = '&sql_id' GROUP BY sql_id;===define 1='1dnwkpnptc006' set timing off head off col qtext format a150 select coalesce( (select sql_fulltext from v$sqlarea a where a.sql_id='&1') , (select sql_text from dba_hist_sqltext a where a.sql_id='&1' ) ) qtext from dual ;set linesize 300 col SQL_TEXT for a80 wrap define 1='25-11-25 00:00' define 2='25-11-25 16:00' define sql_id='94qn6y14kw01g' col b.sql_text for a100 wrap select distinct a.con_id, a.sql_id,dbms_lob.substr(b.sql_text,4000,1) sql_text from dba_hist_active_sess_history a, dba_hist_sqltext b where 1=1 and sample_time between to_date('&1','dd-mm-yy hh24:mi') and to_date('&2','dd-mm-yy hh24:mi') and b.sql_id=a.sql_id and b.sql_id='&sql_id' union select distinct a.con_id, a.sql_id ,dbms_lob.substr(b.sql_text,4000,1) sql_text from gv$active_session_history a ,gV$SQLSTATS b where 1=1 and sample_time between to_date('&1','dd-mm-yy hh24:mi') and to_date('&2','dd-mm-yy hh24:mi') and b.sql_id=a.sql_id and b.sql_id='&sql_id' / CON_ID SQL_ID SQL_TEXT ---------- ------------- -------------------------------------------------------------------------------- 0 94qn6y14kw01g SELECT NVL(TO_NUMBER(EXTRACT(XMLTYPE(:B2 ), :B1 )), 0) FROM DUALset long 2000 select sql_id,cast (SQL_FULLTEXT as varchar2(3999)) sql_text from v$sql where sql_text like '%&&1%' union select sql_id,cast (sql_text as varchar2(3999)) from dba_hist_sqltext where sql_text like '%&&1%';set termout on head onvar sqlid VARCHAR2(13); begin :sqlid := '6u9vbmcmg8b95'; end; ----- change sql id / set long 50000 pagesize 500 linesize 300 col frm heading from select * from (select 'gv$sql' frm , con_id, sql_fulltext from gv$sql where sql_id=:sqlid union all select 'dba_hist',con_id, sql_text from dba_hist_sqltext where sql_id=:sqlid ) where 1=1 and rownum <3;===set linesize 300 col SQL_TEXT for a100 wrap select * from ( select --sql_id,piece,address, hash_value, lag(sql_text) over (partition by address, hash_value order by piece) || sql_text || lead(sql_text) over (partition by address, hash_value order by piece) sql_text from v$sqltext_with_newlines where 1=1 and sql_id='9sam1vjv9q6s9' ) where 1=1 --and sql_id='9sam1vjv9q6s9' --hash_value=4167100631 /define 1='cp1g5w1dgc000' set linesize 400 col sql_text for a30 select st.inst_id as "INST", st.con_id, st.executions as execs, st.sql_id, st.plan_hash_value as plan, round(st.elapsed_time/decode(st.executions,0,1,st.executions)) as ela_per_exec, round(st.cpu_time/decode(st.executions,0,1,st.executions)) as cpu_per_exec, round(st.buffer_gets/decode(st.executions,0,1,st.executions)) as gets_per_exec, round(st.concurrency_wait_time/decode(st.executions,0,1,st.executions)) as conc_per_exec, round(st.cluster_wait_time/decode(st.executions,0,1,st.executions)) as clu_per_exec, round(st.user_io_wait_time/decode(st.executions,0,1,st.executions)) as uio_per_exec, round(st.physical_read_bytes/decode(st.executions,0,1,st.executions)/1024/1024) as read_mb_per_exec, round(st.physical_read_requests/decode(st.executions,0,1,st.executions)) as reads_per_exec, round(st.disk_reads/decode(st.executions,0,1,st.executions)/1024/1024) as disk_reads_per_exec, round(st.physical_write_bytes/decode(st.executions,0,1,st.executions)/1024/1024) as writes_mb_per_exec, round(st.physical_write_requests/decode(st.executions,0,1,st.executions)) as writes_per_exec, round(st.direct_writes/decode(st.executions,0,1,st.executions)) as direct_writes_per_exec, round(st.rows_processed/decode(st.executions,0,1,st.executions)) as rows_per_exec, round(st.px_servers_executions/decode(st.executions,0,1,st.executions)) as px_per_exec, dbms_lob.substr(sql_text,30,1) sql_text from gv$sqlstats st where sql_id in ('&&1') /define sql_id='b6usrg82hwsa3' set long 32000 lines 300 col sql_text format a40 col execs for 999,999,999 col etime for 999,999,999.9 col avg_etime for 999,999.999 col lio for 999,999,999,999 col avg_lio for 999,999,999,999 col avg_pio for 999,999,999,999 col rows_proc for 999,999,999,999 head rows col begin_interval_time for a30 col node for 99999 col versions for 99999 col percent_of_total for 999.99 col SQL_TEXT for a50 break on report compute sum of percent_of_total on report select sql_id, sql_text, avg_pio, avg_lio, avg_etime, execs, rows_proc from ( select dbms_lob.substr(sql_text,50,1) sql_text, b.* from dba_hist_sqltext a, ( select sql_id, sum(execs) execs, sum(etime) etime, sum(etime)/sum(execs) avg_etime, sum(pio)/sum(execs) avg_pio, sum(lio)/sum(execs) avg_lio, sum(rows_proc) rows_proc from ( select ss.snap_id, ss.instance_number node, begin_interval_time, sql_id, nvl(executions_delta,0) execs,elapsed_time_delta/1000000 etime, (elapsed_time_delta/decode(nvl(executions_delta,0),0,1,executions_delta))/1000000 avg_etime,buffer_gets_delta lio,disk_reads_delta pio,rows_processed_delta rows_proc, (buffer_gets_delta/decode(nvl(buffer_gets_delta,0),0,1,executions_delta)) avg_lio, (rows_processed_delta/decode(nvl(rows_processed_delta,0),0,1,executions_delta)) avg_rows, (disk_reads_delta/decode(nvl(disk_reads_delta,0),0,1,executions_delta)) avg_pio from DBA_HIST_SQLSTAT S, DBA_HIST_SNAPSHOT SS where ss.snap_id = S.snap_id and ss.instance_number = S.instance_number --and ss.snap_id between nvl('&starting_snap_id',0) and nvl('&ending_snap_id',999999999) and executions_delta > 0 ) group by sql_id order by 5 desc ) b where a.sql_id = b.sql_id and execs > 1 ) where 1=1--rownum <31 --and sql_text like nvl('&sql_text',sql_text) and sql_id like nvl('&sql_id',sql_id) -- group by sql_id, sql_text order by etime desc /======with sql_id define sql_id='8bxmh51wx3r4c' set long 30000 pagesize 500 linesize 300 col frm heading from select * from (select 'gv$sql' frm ,sql_id, sql_fulltext sql_text from gv$sql where sql_id='&&sql_id' union all select 'gv$sqlstats' frm ,sql_id, sql_fulltext sql_text from gv$sqlstats where sql_id='&&sql_id' union all select 'dba_hist', sql_id,sql_text from dba_hist_sqltext where sql_id='&&sql_id' ) where 1=1 and rownum<2 ;set lines 300 pagesize 300 col PLAN_TABLE_OUTPUT for a150 define sql_id='8zd6493fsr0kb' SELECT * FROM table(dbms_xplan.display_awr(nvl('&sql_id','8zd6493fsr0kb'),nvl('&plan_hash_value',null),null,'typical +peeked_binds')) /select * from table(dbms_xplan.display_cursor('&1',null,'ADVANCED OUTLINE ALLSTATS LAST +PEEKED_BINDS')); select * from table(dbms_xplan.display_awr('&1',null,null,'ADVANCED OUTLINE ALLSTATS LAST +PEEKED_BINDS'));https://anuj-singh.blogspot.com/2017/09/sql-information-from-memory.htmlhttps://anuj-singh.blogspot.com/2021/12/object-waits-with-event.htmlset linesize 300 define 1='18-07-25 07:00' define 2='18-07-25 16:00' define sql_id='f27uxgwvkdcxx' col b.sql_text for a100 wrap select distinct a.sql_id,dbms_lob.substr(b.sql_text,4000,1) sql_text from dba_hist_active_sess_history a, dba_hist_sqltext b where 1=1 and sample_time between to_date('&1','dd-mm-yy hh24:mi') and to_date('&2','dd-mm-yy hh24:mi') and b.sql_id=a.sql_id and b.sql_id='&sql_id' union select a.sql_id ,dbms_lob.substr(b.sql_text,4000,1) sql_text from v$active_session_history a ,v$sqlarea b where 1=1 and sample_time between to_date('&1','dd-mm-yy hh24:mi') and to_date('&2','dd-mm-yy hh24:mi') and b.sql_id=a.sql_id and b.sql_id='&sql_id' /set linesize 400 pagesize 300 col SAMPLE_TIME for a27 col USERNAME for a15 col SQL_TEXT for a40 wrap select a.SAMPLE_TIME, a.SQL_ID, SQL_PLAN_HASH_VALUE , a.program, a.client_id, (case when c.executions_delta > 0 then c.elapsed_time_delta/ c.executions_delta/ 1000000 else 0 end) seconds_per_exe, e.username, b.SQL_TEXT from DBA_HIST_ACTIVE_SESS_HISTORY a join dba_hist_sqltext b on (a.SQL_ID = b.SQL_ID) join dba_users e on (a.user_id = e.user_id) left outer join dba_hist_sqlstat c on (a.sql_id = c.sql_id) left outer join dba_hist_snapshot d on (c.snap_id = d.snap_id and c.dbid = d.dbid and c.instance_number = d.instance_number and a.sample_time between d.begin_interval_time and d.end_interval_time) where 1=1 and a.SQL_ID='f27uxgwvkdcxx' -- b.sql_text like '%2%' and a.sample_time between trunc(sysdate) -10 and trunc(sysdate)alter session set nls_date_format='dd-mm-yyyy hh24:mi'; define sql_id='3p3mf64j54xxx' define childnr=0 set linesize 300 col OWNER for a15 col TABLE_NAME for a25 col global_stats for a15 col partitioned for a15 SELECT owner, table_name, last_analyzed, sample_size, num_rows, avg_row_len, blocks, partitioned, global_stats FROM dba_tables WHERE table_name IN ( select distinct rtrim(substr(plan_table_output, instr(plan_table_output, '|', 1, 3)+2, (instr(plan_table_output, '|', 1, 4)-instr(plan_table_output, '|', 1, 3)-2)), ' ') from ( SELECT plan_table_output FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id', &childnr, 'BASIC')) UNION ALL SELECT * FROM TABLE(dbms_xplan.display_awr('&sql_id', null, null, 'ALL')) ) where 1=1 -- plan_table_output like '%TABLE ACCESS%' ) ORDER BY owner, table_name /
-- with rounddefine sql_id='9px0f4mv0tn0t' VARIABLE BgnSnap NUMBER VARIABLE EndSnap NUMBER VARIABLE DID NUMBER VARIABLE INST_NUMBER number VARIABLE x VARCHAR2(30) exec select max(snap_id) -1000 into :BgnSnap from dba_hist_snapshot ; exec select max(snap_id) into :EndSnap from dba_hist_snapshot ; set lines 1000 pages 9999 COL instance_number FOR 9999 HEA 'Inst' COL end_time HEA 'End Time' COL plan_hash_value HEA 'Plan|Hash Value' COL executions_total for 999,999 HEA 'Execs|Total' COL rows_per_exec for 999999.999 HEA 'Rows Per Exec' COL et_secs_per_exec for 999999.999 HEA 'Elap Secs|Per Exec' COL cpu_secs_per_exec for 999999.999 HEA 'CPU Secs|Per Exec' COL io_secs_per_exec for 999999.999 HEA 'IO Secs|Per Exec' COL cl_secs_per_exec for 999999.999 HEA 'Clus Secs|Per Exec' COL ap_secs_per_exec for 999999.999 HEA 'App Secs|Per Exec' COL cc_secs_per_exec for 999999.999 HEA 'Conc Secs|Per Exec' COL pl_secs_per_exec for 999999.999 HEA 'PLSQL Secs|Per Exec' COL ja_secs_per_exec for 999999.999 HEA 'Java Secs|Per Exec' SELECT 'gv$dba_hist_sqlstat' source,h.instance_number,s.snap_id, TO_CHAR(CAST(s.begin_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') snap_time, TO_CHAR(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions_total, TO_CHAR(ROUND(h.rows_processed_total / h.executions_total), '999,999,999,999') rows_per_exec, ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3) et_secs_per_exec, ROUND(h.cpu_time_total / h.executions_total / 1e6, 3) cpu_secs_per_exec, ROUND(h.iowait_total / h.executions_total / 1e6, 3) io_secs_per_exec, ROUND(h.clwait_total / h.executions_total / 1e6, 3) cl_secs_per_exec, ROUND(h.apwait_total / h.executions_total / 1e6, 3) ap_secs_per_exec, ROUND(h.ccwait_total / h.executions_total / 1e6, 3) cc_secs_per_exec, ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3) pl_secs_per_exec, ROUND(h.javexec_time_total / h.executions_total / 1e6, 3) ja_secs_per_exec FROM dba_hist_sqlstat h, dba_hist_snapshot s WHERE h.sql_id = '&sql_id' AND h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number and s.snap_id > :BgnSnap UNION ALL SELECT 'gv$sqlarea_plan_hash' source,h.inst_id,0 "snap_id", TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') snap_time, TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions, TO_CHAR(ROUND(h.rows_processed / h.executions), '999,999,999,999') rows_per_exec, ROUND(h.elapsed_time / h.executions / 1e6, 3) et_secs_per_exec, ROUND(h.cpu_time / h.executions / 1e6, 3) cpu_secs_per_exec, ROUND(h.USER_IO_WAIT_TIME / h.executions / 1e6, 3) io_secs_per_exec, ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3) cl_secs_per_exec, ROUND(h.APPLICATION_WAIT_TIME / h.executions / 1e6, 3) ap_secs_per_exec, ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3) cc_secs_per_exec, ROUND(h.PLSQL_EXEC_TIME / h.executions / 1e6, 3) pl_secs_per_exec, ROUND(h.JAVA_EXEC_TIME / h.executions / 1e6, 3) ja_secs_per_exec FROM gv$sqlarea_plan_hash h WHERE h.sql_id = '&&sql_id' AND h.executions > 0 order by SNAP_ID ;set linesize 500 col END_INTERVAL_TIME for a27 select ss.sql_id, ss.plan_hash_value, sn.END_INTERVAL_TIME, ss.executions_delta, ELAPSED_TIME_DELTA/(executions_delta*1000) "Elapsed Average ms", CPU_TIME_DELTA/(executions_delta*1000) "CPU Average ms", IOWAIT_DELTA/(executions_delta*1000) "IO Average ms", CLWAIT_DELTA/(executions_delta*1000) "Cluster Average ms", APWAIT_DELTA/(executions_delta*1000) "Application Average ms", CCWAIT_DELTA/(executions_delta*1000) "Concurrency Average ms", BUFFER_GETS_DELTA/executions_delta "Average buffer gets", DISK_READS_DELTA/executions_delta "Average disk reads", ROWS_PROCESSED_DELTA/executions_delta "Average rows processed", FETCHES_DELTA/executions_delta "Average fetches", END_OF_FETCH_COUNT_DELTA/executions_delta "Average end of fetch count", SORTS_DELTA/executions_delta "Average sorts", PX_SERVERS_EXECS_DELTA/executions_delta "Average PX servers execs", LOADS_DELTA/executions_delta "Average loads", INVALIDATIONS_DELTA/executions_delta "Average invalidations", PARSE_CALLS_DELTA/executions_delta "Average parse calls", DIRECT_WRITES_DELTA/executions_delta "Average direct writes", PLSEXEC_TIME_DELTA/executions_delta "Average PS/SQL exec time", JAVEXEC_TIME_DELTA/executions_delta "Average Java exec time" from DBA_HIST_SQLSTAT ss,DBA_HIST_SNAPSHOT sn where 1=1 and ss.sql_id = 'a8jcpqxvkzkgu' and ss.snap_id=sn.snap_id --and executions_delta > 100000 order by ss.snap_id,ss.sql_id;*************************************************************https://anuj-singh.blogspot.com/2021/05/sql-history.htmlsql explian------------set linesize 300 pagesize 1000 SELECT t.* FROM gv$sql s, table(dbms_xplan.display_cursor(s.sql_id,s.child_number,FORMAT => 'ADVANCED +ROWS +BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE')) t WHERE s.sql_id in (select sql_id from gv$session where sql_id is not null --and USERNAME='USER' and sql_id='gygw3jdhjdm46' ) ;set linesize 300 pagesize 1000col PLAN_TABLE_OUTPUT for a150 select plan_table_output from table(dbms_xplan.DISPLAY_AWR( sql_id => '372u1agmj611x', plan_hash_value => 1701920901, -- format => 'TYPICAL' FORMAT => 'TYPICAL +ROWS +BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE' )) ;define sql_id='ga5m3ggy97233' WITH p AS ( SELECT sql_id,plan_hash_value FROM gv$sql_plan WHERE sql_id = TRIM('&&sql_id.') AND other_xml IS NOT NULL UNION SELECT sql_id,plan_hash_value FROM dba_hist_sql_plan WHERE sql_id = TRIM('&&sql_id.') AND dbid = (SELECT dbid FROM v$database) AND other_xml IS NOT NULL ), m AS ( SELECT sql_id,plan_hash_value, SUM(elapsed_time)/SUM(executions) avg_et_secs, SUM(executions) executions FROM gv$sql WHERE sql_id = TRIM('&&sql_id.') AND executions > 0 GROUP BY sql_id,plan_hash_value ), a AS ( SELECT sql_id,plan_hash_value, SUM(elapsed_time_delta)/SUM(executions_delta) avg_et_secs, SUM(executions_delta) executions FROM dba_hist_sqlstat WHERE sql_id = TRIM('&&sql_id.') AND executions_delta > 0 GROUP BY sql_id,plan_hash_value ) SELECT p.sql_id, TO_CHAR(ROUND(m.avg_et_secs/1e6, 6), '999,990.000000') avg_et_secs_mem, TO_CHAR(ROUND(a.avg_et_secs/1e6, 6), '999,990.000000') avg_et_secs_awr, p.plan_hash_value, m.executions executions_mem, a.executions executions_awr --TO_CHAR(ROUND(NVL(m.avg_et_secs, a.avg_et_secs)/1e6, 6), '999,990.000000') avg_et_secs FROM p, m, a WHERE p.plan_hash_value = m.plan_hash_value(+) AND p.plan_hash_value = a.plan_hash_value(+) ORDER BY NVL(m.avg_et_secs, a.avg_et_secs) NULLS LAST, a.avg_et_secs; SQL_ID AVG_ET_SECS_MEM AVG_ET_SECS_AWR PLAN_HASH_VALUE EXECUTIONS_MEM EXECUTIONS_AWR ------------- --------------- --------------- --------------- -------------- -------------- ga5m3ggy97233 0.034001 0.036738 3256416886 627957 29520905select distinct SNAP_ID,PLAN_HASH_VALUE,CPU_TIME_TOTAL from dba_hist_sqlstat where sql_id='22v2cd7ychms6'VARIABLE BgnSnap NUMBER VARIABLE EndSnap NUMBER VARIABLE DID NUMBER VARIABLE INST_NUMBER number exec select max(snap_id) -24 into :BgnSnap from dba_hist_snapshot ; exec select max(snap_id) into :EndSnap from dba_hist_snapshot ; set linesize 300 col PLAN_TABLE_OUTPUT for a200 select --s.elapsed_time_delta, --s.buffer_gets_delta, -- s.disk_reads_delta, cursor(select * from table(dbms_xplan.display_awr(t.sql_id, s.plan_hash_value))) from dba_hist_sqltext t, dba_hist_sqlstat s where t.dbid = s.dbid and t.sql_id = s.sql_id --and s.snap_id between :BgnSnap and :EndSnap and t.sql_id = '1y4f4wtu63797' and rownum<3 ;col PLAN_TABLE_OUTPUT for a150 col snap_id new_value v_snap_id select max(snap_id) as snap_id from dba_hist_snapshot; select s.elapsed_time_delta, s.buffer_gets_delta, s.disk_reads_delta, cursor(select * from table(dbms_xplan.display_awr(t.sql_id, s.plan_hash_value,FORMAT => 'ADVANCED'))) from dba_hist_sqltext t, dba_hist_sqlstat s where t.dbid = s.dbid and t.sql_id = s.sql_id and s.snap_id between &v_snap_id-2 and &v_snap_id and t.sql_id='&&sql_id' ;COL inst_child FOR A21 BREAK ON inst_child SKIP 2 SET PAGES 0; WITH v AS ( SELECT /*+ MATERIALIZE */ sql_id, inst_id, child_number FROM gv$sql WHERE sql_id = '&&sql_id.' AND loaded_versions > 0 ORDER BY 1, 2, 3 ) SELECT /*+ ORDERED USE_NL(t) */ RPAD('Inst: '||v.inst_id, 9)||' '||RPAD('Child: '||v.child_number, 11) inst_child, t.plan_table_output FROM v, TABLE(DBMS_XPLAN.DISPLAY('gv$sql_plan_statistics_all', NULL, 'ADVANCED ALLSTATS LAST', 'inst_id = '||v.inst_id||' AND sql_id = '''||v.sql_id||''' AND child_number = '||v.child_number)) t;set linesize 300 pagesize 300 col "PLAN" for a150 col PLAN_TABLE_OUTPUT for a150 Select cursor(select * from table(dbms_xplan.display_cursor(sql_id,CHILD_NUMBER,'all +outline +peeked_binds'))) "PLAN" from gv$sql where 1=1 -- and sql_id='00nxwgnnnhd9z' and rownum<3;alter session set nls_date_format='dd-mm-YYYY HH24:MI:SS'; alter session set nls_timestamp_format='dd-mm-YYYY HH24:MI:SS'; col END_TIME for a27 col READ_IO_BYTES for 999999999999999 select sql_id, starting_time, end_time, (EXTRACT(HOUR FROM run_time) * 3600 + EXTRACT(MINUTE FROM run_time) * 60 + EXTRACT(SECOND FROM run_time)) run_time_sec, READ_IO_BYTES, PGA_ALLOCATED PGA_ALLOCATED_BYTES, TEMP_ALLOCATED TEMP_ALLOCATED_BYTES from ( select sql_id, max(sample_time - sql_exec_start) run_time, max(sample_time) end_time, sql_exec_start starting_time, sum(DELTA_READ_IO_BYTES) READ_IO_BYTES, sum(DELTA_PGA) PGA_ALLOCATED, sum(DELTA_TEMP) TEMP_ALLOCATED from ( select sql_id, sample_time, sql_exec_start, DELTA_READ_IO_BYTES, sql_exec_id, greatest(PGA_ALLOCATED - first_value(PGA_ALLOCATED) over (partition by sql_id,sql_exec_id order by sample_time rows 1 preceding),0) DELTA_PGA, greatest(TEMP_SPACE_ALLOCATED - first_value(TEMP_SPACE_ALLOCATED) over (partition by sql_id,sql_exec_id order by sample_time rows 1 preceding),0) DELTA_TEMP from dba_hist_active_sess_history where 1=1 --sample_time >= to_date ('2019/04/16 00:00:00','YYYY/MM/DD HH24:MI:SS') --and sample_time < to_date ('2019/04/16 03:10:00','YYYY/MM/DD HH24:MI:SS') and sql_exec_start is not null --and IS_SQLID_CURRENT='Y' ) group by sql_id,SQL_EXEC_ID,sql_exec_start order by sql_id ) where sql_id = '1tdyz67zbyb' order by sql_id, run_time_sec desc;set linesize 300 trimspool on pagesize 100 col sql_id format a13 col start_time format a22 col sample_time format a22 col end_time format a22 col v_sql_id new_value v_sql_id noprint set feed off term off head off select '&1' v_sql_id from dual; col logtime new_value u_logtime noprint select to_char(sysdate,'dd-mm-yyyy_hh24-mi-ss') logtime from dual; col logfile new_value u_logfile noprint col DURATION for a25 set feed on term on head on with data as ( select sql_id, sql_exec_id, sql_exec_start, min(sample_time) start_time, max(sample_time) end_time, max(sample_time - sql_exec_start) duration, max (SQL_PLAN_HASH_VALUE) SQL_PLAN_HASH_VALUE, FLOOR(EXTRACT(MINUTE FROM (MAX(sample_time) - MIN(sql_exec_start)))) elapsed_minutes from dba_hist_active_sess_history where sql_id = '&v_sql_id' and sql_exec_id is not null group by sql_id, sql_exec_id, sql_exec_start order by sql_exec_start, sql_id, sql_exec_id ) select sql_id , to_char(sql_exec_start,'dd-mm-yyyy hh24:mi:ss') sql_exec_start ,SQL_PLAN_HASH_VALUE , sql_exec_id , to_char(start_time,'dd-mm-yyyy hh24:mi:ss') start_time , to_char(end_time,'dd-mm-yyyy hh24:mi:ss') end_time , duration ,elapsed_minutes from data order by 1,2,3 /define sql_id='0jrngm5htnzuv'set lines 500 pages 100 col instance_number FOR 9999 HEA 'Inst' col end_time HEA 'End Time' col plan_hash_value HEA 'Plan|Hash Value' col rows_per_exec for a15 HEA 'Rows Per Exec' col et_secs_per_exec for a15 HEA 'Elap Secs|Per Exec' col cpu_secs_per_exec for a15 HEA 'CPU Secs|Per Exec' col io_secs_per_exec for a15 HEA 'IO Secs|Per Exec' col cl_secs_per_exec for a15 HEA 'Clus Secs|Per Exec' col ap_secs_per_exec for a15 HEA 'App Secs|Per Exec' col cc_secs_per_exec for a15 HEA 'Conc Secs|Per Exec' col pl_secs_per_exec for a15 HEA 'PLSQL Secs|Per Exec' col ja_secs_per_exec for a15 HEA 'Java Secs|Per Exec' col executions_total for a15 FOR 999,999 HEA 'Execs|Total' col "End Time" for a27 col end_time for a27 select 'gv$dba_hist_sqlstat' source,h.instance_number, to_char(CAST(s.begin_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') snap_time, to_char(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions_total, to_char(ROUND(h.rows_processed_total / h.executions_total), '999,999,999,999') rows_per_exec, to_char(ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3), '999,990.000') et_secs_per_exec, to_char(ROUND(h.cpu_time_total / h.executions_total / 1e6, 3), '999,990.000') cpu_secs_per_exec, to_char(ROUND(h.iowait_total / h.executions_total / 1e6, 3), '999,990.000') io_secs_per_exec, to_char(ROUND(h.clwait_total / h.executions_total / 1e6, 3), '999,990.000') cl_secs_per_exec, to_char(ROUND(h.apwait_total / h.executions_total / 1e6, 3), '999,990.000') ap_secs_per_exec, to_char(ROUND(h.ccwait_total / h.executions_total / 1e6, 3), '999,990.000') cc_secs_per_exec, to_char(ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3), '999,990.000') pl_secs_per_exec, to_char(ROUND(h.javexec_time_total / h.executions_total / 1e6, 3), '999,990.000') ja_secs_per_exec FROM dba_hist_sqlstat h,dba_hist_snapshot s WHERE h.sql_id = '&sql_id' AND h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number UNION ALL SELECT 'gv$sqlarea_plan_hash' source,h.inst_id, TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') snap_time, TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions, to_char(ROUND(h.rows_processed / h.executions), '999,999,999,999') rows_per_exec, to_char(ROUND(h.elapsed_time / h.executions / 1e6, 3), '999,990.000') et_secs_per_exec, to_char(ROUND(h.cpu_time / h.executions / 1e6, 3), '999,990.000') cpu_secs_per_exec, to_char(ROUND(h.USER_IO_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') io_secs_per_exec, to_char(ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') cl_secs_per_exec, to_char(ROUND(h.APPLICATION_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') ap_secs_per_exec, to_char(ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') cc_secs_per_exec, to_char(ROUND(h.PLSQL_EXEC_TIME / h.executions / 1e6, 3), '999,990.000') pl_secs_per_exec, to_char(ROUND(h.JAVA_EXEC_TIME / h.executions / 1e6, 3), '999,990.000') ja_secs_per_exec FROM gv$sqlarea_plan_hash h WHERE h.sql_id = '&sql_id' --and h.inst_id=inst_id AND h.executions > 0 order by source ;-- with dbms_xplan.format_sizeset lines 500 pages 100 col instance_number FOR 9999 HEA 'Inst' col end_time HEA 'End Time' col plan_hash_value HEA 'Plan|Hash Value' col rows_per_exec for a15 HEA 'Rows Per Exec' col et_secs_per_exec for a15 HEA 'Elap Secs|Per Exec' col cpu_secs_per_exec for a15 HEA 'CPU Secs|Per Exec' col io_secs_per_exec for a15 HEA 'IO Secs|Per Exec' col cl_secs_per_exec for a15 HEA 'Clus Secs|Per Exec' col ap_secs_per_exec for a15 HEA 'App Secs|Per Exec' col cc_secs_per_exec for a15 HEA 'Conc Secs|Per Exec' col pl_secs_per_exec for a15 HEA 'PLSQL Secs|Per Exec' col ja_secs_per_exec for a15 HEA 'Java Secs|Per Exec' col executions_total for a15 FOR 999,999 HEA 'Execs|Total' col "End Time" for a27 col end_time for a27 select 'gv$dba_hist_sqlstat' source,h.instance_number, to_char(CAST(s.begin_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') snap_time, to_char(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions_total, dbms_xplan.format_size(h.rows_processed_total / h.executions_total) rows_per_exec, dbms_xplan.format_size(h.elapsed_time_total / h.executions_total) et_secs_per_exec, dbms_xplan.format_size(h.cpu_time_total / h.executions_total ) cpu_secs_per_exec, dbms_xplan.format_size(h.iowait_total / h.executions_total ) io_secs_per_exec, dbms_xplan.format_size(h.clwait_total / h.executions_total ) cl_secs_per_exec, dbms_xplan.format_size(h.apwait_total / h.executions_total ) ap_secs_per_exec, dbms_xplan.format_size(h.ccwait_total / h.executions_total ) cc_secs_per_exec, dbms_xplan.format_size(h.plsexec_time_total / h.executions_total ) pl_secs_per_exec, dbms_xplan.format_size(h.javexec_time_total / h.executions_total ) ja_secs_per_exec FROM dba_hist_sqlstat h,dba_hist_snapshot s WHERE h.sql_id = '&sql_id' AND h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number UNION ALL SELECT 'gv$sqlarea_plan_hash' source,h.inst_id, TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') snap_time, TO_CHAR(sysdate, 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions, dbms_xplan.format_size(h.rows_processed / h.executions) rows_per_exec, dbms_xplan.format_size(h.elapsed_time / h.executions ) et_secs_per_exec, dbms_xplan.format_size(h.cpu_time / h.executions ) cpu_secs_per_exec, dbms_xplan.format_size(h.USER_IO_WAIT_TIME / h.executions ) io_secs_per_exec, dbms_xplan.format_size(h.CLUSTER_WAIT_TIME / h.executions ) cl_secs_per_exec, dbms_xplan.format_size(h.APPLICATION_WAIT_TIME / h.executions) ap_secs_per_exec, dbms_xplan.format_size(h.CLUSTER_WAIT_TIME / h.executions ) cc_secs_per_exec, dbms_xplan.format_size(h.PLSQL_EXEC_TIME / h.executions ) pl_secs_per_exec, dbms_xplan.format_size(h.JAVA_EXEC_TIME / h.executions ) ja_secs_per_exec FROM gv$sqlarea_plan_hash h WHERE h.sql_id = '&sql_id' --and h.inst_id=inst_id AND h.executions > 0 order by source ;***define sql_id='gqkr2um43ga39' VARIABLE BgnSnap NUMBER VARIABLE EndSnap NUMBER VARIABLE DID NUMBER VARIABLE INST_NUMBER number VARIABLE x VARCHAR2(30) exec select max(snap_id) -60 into :BgnSnap from dba_hist_snapshot ; exec select max(snap_id) into :EndSnap from dba_hist_snapshot ; set lines 500 pages 100 col instance_number FOR 9999 col end_time col plan_hash_value col rows_per_exec for a20 col et_secs_per_exec for a20 col cpu_secs_per_exec for a20 col io_secs_per_exec for a20 col cl_secs_per_exec for a20 col ap_secs_per_exec for a20 col cc_secs_per_exec for a20 col pl_secs_per_exec for a20 col ja_secs_per_exec for a20 col executions_total for a20 FOR 999,999 col "End Time" for a27 col end_time for a27 select 'gv$dba_hist_sqlstat' source,h.instance_number, to_char(CAST(s.begin_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') snap_time, to_char(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions_total, to_char(ROUND(h.rows_processed_total / h.executions_total), '999,999,999,999') rows_per_exec, to_char(ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3), '999,990.000') et_secs_per_exec, to_char(ROUND(h.cpu_time_total / h.executions_total / 1e6, 3), '999,990.000') cpu_secs_per_exec, to_char(ROUND(h.iowait_total / h.executions_total / 1e6, 3), '999,990.000') io_secs_per_exec, to_char(ROUND(h.clwait_total / h.executions_total / 1e6, 3), '999,990.000') cl_secs_per_exec, to_char(ROUND(h.apwait_total / h.executions_total / 1e6, 3), '999,990.000') ap_secs_per_exec, to_char(ROUND(h.ccwait_total / h.executions_total / 1e6, 3), '999,990.000') cc_secs_per_exec, to_char(ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3), '999,990.000') pl_secs_per_exec, to_char(ROUND(h.javexec_time_total / h.executions_total / 1e6, 3), '999,990.000') ja_secs_per_exec FROM dba_hist_sqlstat h,dba_hist_snapshot s WHERE h.sql_id = '&sql_id' AND h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number and s.snap_id > :BgnSnap UNION ALL SELECT 'gv$sqlarea_plan_hash' source,h.inst_id, TO_CHAR(FIRST_LOAD_TIME, 'DD-MM-YYYY HH24:MI') snap_time, TO_CHAR(LAST_LOAD_TIME, 'DD-MM-YYYY HH24:MI') end_time, h.sql_id, h.plan_hash_value, h.executions, to_char(ROUND(h.rows_processed / h.executions), '999,999,999,999') rows_per_exec, to_char(ROUND(h.elapsed_time / h.executions / 1e6, 3), '999,990.000') et_secs_per_exec, to_char(ROUND(h.cpu_time / h.executions / 1e6, 3), '999,990.000') cpu_secs_per_exec, to_char(ROUND(h.USER_IO_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') io_secs_per_exec, to_char(ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') cl_secs_per_exec, to_char(ROUND(h.APPLICATION_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') ap_secs_per_exec, to_char(ROUND(h.CLUSTER_WAIT_TIME / h.executions / 1e6, 3), '999,990.000') cc_secs_per_exec, to_char(ROUND(h.PLSQL_EXEC_TIME / h.executions / 1e6, 3), '999,990.000') pl_secs_per_exec, to_char(ROUND(h.JAVA_EXEC_TIME / h.executions / 1e6, 3), '999,990.000') ja_secs_per_exec FROM gv$sqlarea_plan_hash h WHERE h.sql_id = '&sql_id' --and h.inst_id=inst_id AND h.executions > 0 and FIRST_LOAD_TIME > sysdate -4 order by source ;define sql_id='b3cyyrgtgxakt' COL instance_number FOR 9999 HEA 'Inst' COL end_time for a18 HEA 'End Time' COL executions_total FOR 99,999,999 COL rows_per_exec FOR A12 COL et_secs_per_exec FOR A16 COL cpu_secs_per_exec FOR A16 COL io_secs_per_exec FOR A16 COL cl_secs_per_exec FOR A16 COL ap_secs_per_exec FOR A16 COL cc_secs_per_exec FOR A16 COL pl_secs_per_exec FOR A16 COL ja_secs_per_exec FOR A16 -- col "End Time" for a27 SELECT h.instance_number, TO_CHAR(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time, h.plan_hash_value, h.executions_total, TO_CHAR(ROUND(h.rows_processed_total / h.executions_total), '999999999') rows_per_exec, TO_CHAR(ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3), '999,990.000') et_secs_per_exec, TO_CHAR(ROUND(h.cpu_time_total / h.executions_total / 1e6, 3), '999,990.000') cpu_secs_per_exec, TO_CHAR(ROUND(h.iowait_total / h.executions_total / 1e6, 3), '999,990.000') io_secs_per_exec, TO_CHAR(ROUND(h.clwait_total / h.executions_total / 1e6, 3), '999,990.000') cl_secs_per_exec, TO_CHAR(ROUND(h.apwait_total / h.executions_total / 1e6, 3), '999,990.000') ap_secs_per_exec, TO_CHAR(ROUND(h.ccwait_total / h.executions_total / 1e6, 3), '999,990.000') cc_secs_per_exec, TO_CHAR(ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3), '999,990.000') pl_secs_per_exec, TO_CHAR(ROUND(h.javexec_time_total / h.executions_total / 1e6, 3), '999,990.000') ja_secs_per_exec FROM dba_hist_sqlstat h, dba_hist_snapshot s WHERE h.sql_id = '&sql_id' AND h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number ORDER BY h.sql_id, h.instance_number, s.end_interval_time, h.plan_hash_value /from https://github.com/tanelpoder/tpt-oracle/blob/master/awr/awr_sqlstats_per_exec.sql alter session set nla_date_format-'dd-mm-yyyy hh24:mi'; define 1='dnm7fqpf08p8w' DEF 3="TIMESTAMP'2023-07-01 01:00:00'" DEF 4="TIMESTAMP'2023-07-15 03:15:00'" define 2 =' ' SET TERMOUT OFF pagesize 5000 tab off verify off linesize 999 trimspool on trimout on null "" SET TERMOUT ON col BEGIN_INTERVAL_TIME for a27 COL executions FOR 999999999990 COL ela_ms_per_exec FOR 999999999990 COL rows_per_exec FOR 999999999990.0 COL lios_per_exec FOR 999999999990 COL blkrd_per_exec FOR 999999999990 COL cpu_ms_per_exec FOR 999999999990 COL iow_ms_per_exec FOR 999999999990 COL avg_iow_ms FOR 999999999990.0 COL clw_ms_per_exec FOR 999999999990 COL apw_ms_per_exec FOR 999999999990 COL ccw_ms_per_exec FOR 999999999990 SELECT -- CAST(begin_interval_time AS DATE) begin_interval_time begin_interval_time , sql_id , plan_hash_value , SUM(executions_delta) executions , ROUND(SUM(elapsed_time_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) ela_ms_per_exec , ROUND(SUM(cpu_time_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) cpu_ms_per_exec , ROUND(SUM(rows_processed_delta) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta)),1) rows_per_exec , ROUND(SUM(buffer_gets_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta)),1) lios_per_exec , ROUND(SUM(disk_reads_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta)),1) blkrd_per_exec , ROUND(SUM(iowait_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) iow_ms_per_exec , ROUND(SUM(iowait_delta ) / DECODE(SUM(physical_read_requests_delta)+SUM(physical_write_requests_delta),0,1 ,SUM(physical_read_requests_delta)+SUM(physical_write_requests_delta))/1000,1) avg_iow_ms , ROUND(SUM(clwait_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) clw_ms_per_exec , ROUND(SUM(apwait_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) apw_ms_per_exec , ROUND(SUM(ccwait_delta ) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta))/1000) ccw_ms_per_exec -- , ROUND(SUM(elapsed_time_delta - cpu_time_delta - iowait_delta - clwait_delta - apwait_delta - ccwait_delta) / DECODE(SUM(executions_delta),0,1,SUM(executions_delta)) / 1000, 1) oth_ms_per_exec FROM dba_hist_snapshot sn , dba_hist_sqlstat st WHERE sn.snap_id = st.snap_id AND sn.dbid = st.dbid AND sn.instance_number = st.instance_number AND sql_id = '&1' --AND plan_hash_value LIKE '&2' AND begin_interval_time >= &3 AND end_interval_time <= &4 AND (elapsed_time_delta != 0 AND cpu_time_delta != 0) GROUP BY -- CAST(begin_interval_time AS DATE) begin_interval_time , sql_id , plan_hash_value ORDER BY begin_interval_time , sql_id , plan_hash_value /***********select DBMS_SQL_TRANSLATOR.SQL_ID(sql_text) as sql_id,sql_handle,plan_name from dba_sql_plan_baselines where DBMS_SQL_TRANSLATOR.SQL_ID(sql_text) = '&SQL_ID';set linesize 32000 pagesize 1000 long 2000000000 longchunksize 1000 head off verify off termout offset head on echo on termout on trimspool oncolumn u new_value us noprint; column n new_value ns noprint; select name n from v$database; select user u from dual; column END_INTERVAL_TIME format a25 -- 48vf4pg4g5508
select sql_id, plan_hash_value, END_INTERVAL_TIME, executions_delta, ELAPSED_TIME_DELTA/(nonzeroexecutions*1000) "Elapsed Average ms", CPU_TIME_DELTA/(nonzeroexecutions*1000) "CPU Average ms", IOWAIT_DELTA/(nonzeroexecutions*1000) "IO Average ms", CLWAIT_DELTA/(nonzeroexecutions*1000) "Cluster Average ms", APWAIT_DELTA/(nonzeroexecutions*1000) "Application Average ms", CCWAIT_DELTA/(nonzeroexecutions*1000) "Concurrency Average ms", BUFFER_GETS_DELTA/nonzeroexecutions "Average buffer gets", DISK_READS_DELTA/nonzeroexecutions "Average disk reads", trunc(PHYSICAL_WRITE_BYTES_DELTA/(1024*1024*nonzeroexecutions)) "Average disk write megabytes", ROWS_PROCESSED_DELTA/nonzeroexecutions "Average rows processed" from (select ss.snap_id, ss.sql_id, ss.plan_hash_value, sn.END_INTERVAL_TIME, ss.executions_delta, case ss.executions_delta when 0 then 1 else ss.executions_delta end nonzeroexecutions, ELAPSED_TIME_DELTA, CPU_TIME_DELTA, IOWAIT_DELTA, CLWAIT_DELTA, APWAIT_DELTA, CCWAIT_DELTA, BUFFER_GETS_DELTA, DISK_READS_DELTA, PHYSICAL_WRITE_BYTES_DELTA, ROWS_PROCESSED_DELTA from DBA_HIST_SQLSTAT ss,DBA_HIST_SNAPSHOT sn where ss.sql_id = '48vf4pg4g5508' and ss.snap_id=sn.snap_id and ss.INSTANCE_NUMBER=sn.INSTANCE_NUMBER) where ELAPSED_TIME_DELTA > 0 order by snap_id,sql_id; ===define sql_id='48vf4pg4g5508' set linesize 999 col avg_et_secs justify right format 9999999.99 col cost justify right format 9999999999 col timestamp justify center format a25 col parsing_schema_name justify center format a30 col inst_id format 999999999 alter session set nls_date_format='dd-mm-yyyy hh24:mi:ss'; select 'gv$sqlarea_plan_hash' source, INST_ID, SQL_ID, PLAN_HASH_VALUE, round(elapsed_time/decode(nvl(executions,0),0,1,executions)/1e6/ decode(px_servers_executions,0,1,px_servers_executions)/decode(nvl(executions,0),0,1,executions),2) avg_et_secs, px_servers_executions/decode(nvl(executions,0),0,1,executions) avg_px, optimizer_cost cost, LAST_LOAD_TIME timestamp, parsing_schema_name --FIRST_LOAD_TIME, LAST_LOAD_TIME, LAST_ACTIVE_TIME, SQL_PROFILE from gv$sqlarea_plan_hash where sql_id = nvl(trim('&&sql_id'),sql_id) UNION SELECT 'dba_hist_sql_plan' source, null INST_ID, t1.sql_id sql_id, t1.plan_hash_value plan_hash_value, t2.avg_et_secs avg_et_secs, t2.avg_px, t1.cost cost, t1.timestamp timestamp, NULL parsing_schema_name FROM dba_hist_sql_plan t1, ( SELECT sql_id, plan_hash_value, --round(SUM(elapsed_time_total)/decode(SUM(executions_total),0,1,SUM(executions_total))/1e6,2) avg_et_secs round(SUM(elapsed_time_total)/decode(SUM(executions_total),0,1,SUM(executions_total))/1e6/ decode(SUM(px_servers_execs_total),0,1,SUM(px_servers_execs_total))/decode(SUM(executions_total),0,1,SUM(executions_total)),2) avg_et_secs, SUM(px_servers_execs_total)/decode(SUM(executions_total),0,1,SUM(executions_total)) avg_px FROM dba_hist_sqlstat WHERE executions_total > 0 GROUP BY sql_id, plan_hash_value ) t2 WHERE t1.sql_id = nvl(TRIM('&sql_id.'), t1.sql_id) AND t1.depth = 0 AND t1.sql_id = t2.sql_id(+) AND t1.plan_hash_value = t2.plan_hash_value(+) order by avg_et_secs, cost / undef sql_id====set feedback off heading on timi off pages 200 lines 1000 echo off VERIFY OFF col INST for 9999 col EXECS for 999999999999 col CHILD for 99999 col BIND_SENSE for a10 col BIND_AWARE for a10 col SHAREABLE for a10 col USE_FEEDBACK_STATS for a18 col OPTIMIZER_STATS for a16 col BIND_EQ_FAILURE for a16 col fix_control#1 for a40 col fix_control#2 for a40 col SQL_PLAN_BASELINE for a30 col SQL_PATCH for a30 col SQL_PROFILE for a64 col ROLL for a4 col REOPT for a5 col ADAPT for a5 col FIRST_LOAD_TIME for a20 col LAST_LOAD_TIME for a20 col PARSE_USER for a30 col SPD_Valid for a9 col SPD_Used for a9 col CURSOR_STATUS for a19 col DS_LEVEL for a8 col DOP for a3 col DOP_REASON for a30 col PARSING_SCHEMA_NAME for a20 col reason#1 for a40 col reason#2 for a40 col reason#3 for a40 col reason#4 for a40 pro pro -------------------------------------------------------------- pro SQL_ID=&&1 Shared Pool statistics by PLAN_HASH_VALUE pro -------------------------------------------------------------- define 1='2u2r9ashfn00h' select s.inst_id as INST, s.parsing_schema_name as PARSING_SCHEMA_NAME, sum(s.EXECUTIONS) as EXECS, s.PLAN_HASH_VALUE, s.FULL_PLAN_HASH_VALUE, max(s.last_load_time) as LAST_LOAD_TIME, max(to_char(s.last_active_time, 'dd.mm.yyyy hh24:mi:ss')) as LAST_ACTIVE_TIME, round(sum(s.elapsed_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as ELA_PER_EXEC, round(sum(s.cpu_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as CPU_PER_EXEC, round(sum(s.parse_calls)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as PARSES_PER_EXEC, round(sum(s.buffer_gets)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as GETS_PER_EXEC, round(sum(s.disk_reads)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as READS_PER_EXEC, round(sum(s.user_io_wait_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as UIO_PER_EXEC, round(sum(s.concurrency_wait_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as CONC_PER_EXEC, round(sum(s.cluster_wait_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as CLU_PER_EXEC, round(sum(s.PLSQL_EXEC_TIME)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as PLSQL_PER_EXEC, round(sum(s.FETCHES)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as FETCH_PER_EXEC, round(sum(s.ROWS_PROCESSED)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) as ROWS_PER_EXEC, round(avg(s.optimizer_cost)) as AVG_CBO_COST, count(distinct s.child_number) as CHILDS, max(s.IS_BIND_SENSITIVE) as "BIND_SENSE", max(s.IS_BIND_AWARE) as "BIND_AWARE", max(s.IS_REOPTIMIZABLE) as "REOPT", max(nvl(s.IS_RESOLVED_ADAPTIVE_PLAN,'N')) as "ADAPT", s.SQL_PLAN_BASELINE, s.SQL_PATCH, s.OUTLINE_CATEGORY, s.SQL_PROFILE from gv$sql s where s.sql_id = '&1' --and (s.PLAN_HASH_VALUE = NVL('&&2',s.PLAN_HASH_VALUE) or '&&2' = '0') group by s.inst_id, s.parsing_schema_name, s.PLAN_HASH_VALUE, s.FULL_PLAN_HASH_VALUE, s.SQL_PLAN_BASELINE, s.SQL_PATCH, s.OUTLINE_CATEGORY, s.SQL_PROFILE order by max(to_char(s.last_active_time, 'dd.mm.yyyy hh24:mi:ss')), max(s.last_load_time) --round(sum(s.elapsed_time)/decode(sum(s.EXECUTIONS),0,1,sum(s.EXECUTIONS))) desc /
with cpu !!!
define sql_id='7grvag6ayaxn1'
SET lin 400 ver OFF
COL instance_number FOR 9999 HEA 'Inst'
COL end_time HEA 'End Time'
COL plan_hash_value HEA 'Plan|Hash Value'
COL executions_total FOR 999,999,999,999 HEA 'Execs|Total'
COL rows_per_exec for a15 HEA 'Rows Per Exec'
COL et_secs_per_exec for a15 HEA 'Elap Secs|Per Exec'
COL cpu_secs_per_exec for a15 HEA 'CPU Secs|Per Exec'
COL io_secs_per_exec for a15 HEA 'IO Secs|Per Exec'
COL cl_secs_per_exec for a15 HEA 'Clus Secs|Per Exec'
COL ap_secs_per_exec for a15 HEA 'App Secs|Per Exec'
COL cc_secs_per_exec for a15 HEA 'Conc Secs|Per Exec'
COL pl_secs_per_exec for a15 HEA 'PLSQL Secs|Per Exec'
COL ja_secs_per_exec for a15 HEA 'Java Secs|Per Exec'
COL cpu_per_exec for a15 HEA 'Cpu Secs|Per Exec'
SELECT h.instance_number,h.snap_id,
TO_CHAR(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time,
h.con_id,
h.sql_id,
h.plan_hash_value,
h.executions_total,
TO_CHAR(ROUND(h.rows_processed_total / h.executions_total), '999,999,999,999') rows_per_exec,
TO_CHAR(ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3), '999,990.000') et_secs_per_exec,
TO_CHAR(ROUND(h.cpu_time_total / h.executions_total / 1e6, 3), '999,990.000') cpu_secs_per_exec,
TO_CHAR(ROUND(h.iowait_total / h.executions_total / 1e6, 3), '999,990.000') io_secs_per_exec,
TO_CHAR(ROUND(h.clwait_total / h.executions_total / 1e6, 3), '999,990.000') cl_secs_per_exec,
TO_CHAR(ROUND(h.apwait_total / h.executions_total / 1e6, 3), '999,990.000') ap_secs_per_exec,
TO_CHAR(ROUND(h.ccwait_total / h.executions_total / 1e6, 3), '999,990.000') cc_secs_per_exec,
TO_CHAR(ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3), '999,990.000') pl_secs_per_exec,
TO_CHAR(ROUND(h.javexec_time_total / h.executions_total / 1e6, 3), '999,990.000') ja_secs_per_exec,
TO_CHAR(ROUND(h.CPU_TIME_TOTAL / h.executions_total / 1e6, 3), '999,990.000') cpu_per_exec
FROM dba_hist_sqlstat h,
dba_hist_snapshot s
WHERE h.sql_id = '&sql_id.'
-- AND h.executions_total > 16000000
AND s.snap_id = h.snap_id
AND s.dbid = h.dbid
AND s.instance_number = h.instance_number
ORDER BY
h.sql_id,
h.instance_number,
s.end_interval_time,
h.plan_hash_value
/
set linesize 400 pagesize 300 col sql_text for a80 select * from ( select count(sc.sql_id) cursor_count, a.con_id ,a.sql_id, a.sql_text from v$sqlarea a, v$sql_shared_cursor sc where a.sql_id = sc.sql_id group by a.sql_id, a.con_id,a.sql_text order by cursor_count desc) where rownum <=10;
Oracle version count from History
define sql_id='xxxx'
-- define sql_id ='xxxxx'
set serveroutput on size 1000000 lines 200 pages 1000
set verify off
col begin_time for a16
col end_time for a10
col module for a20
col action for a20
col execs for 999,999,990 heading "Executions"
col physrd_exec for 999,999,990 heading "Disk|Reads|per Exec"
col logrds_exec for 999,999,990 heading "Buffer|Gets|per Exec"
col CPU_EXEC for 999,990.9999 heading "CPU|Time per|Exec(secs)"
col ELA_EXEC for 999,990.9999 heading "Elapsed|Time per|Exec(secs)"
col drwrites_exec for 999,999,990 heading "Direct|Writes|per Exec"
col rowsprc_exec for 999,999,990 heading "Rows|Processed|per Exec"
col fetch_EXEC for 999,990 heading "Fetches|per Exec"
col sharable_mb for 999,990.99 heading "Sharable|Mem(mb)"
col VERSION_COUNT for 999,990 heading "Version|Count"
col plan_hash for 999999999999
select to_char(b.BEGIN_INTERVAL_TIME,'DD-MON-RR HH24:MI') begin_time,
to_char(b.END_INTERVAL_TIME,'HH24:MI') end_time,
a.sql_id,
a.plan_hash_value plan_hash,
--a.invalidations_delta,
--a.module,
--a.action,
a.executions_delta execs,
(case when a.executions_delta > 0 then round((a.elapsed_time_delta/a.executions_delta)/1000000,4)
when a.executions_delta = 0 then NULL end) ELA_EXEC,
(case when a.executions_delta > 0 then round((a.cpu_time_delta/a.executions_delta)/1000000,4)
when a.executions_delta = 0 then NULL end) CPU_EXEC,
(case when a.executions_delta > 0 then round(a.buffer_gets_delta/a.executions_delta,0)
when a.executions_delta = 0 then NULL end) logrds_exec,
(case when a.executions_delta > 0 then round(a.disk_reads_delta/a.executions_delta,0)
when a.executions_delta = 0 then NULL end) physrd_exec,
(case when a.executions_delta > 0 then round(a.DIRECT_WRITES_DELTA/a.executions_delta,0)
when a.executions_delta = 0 then NULL end) drwrites_exec,
(case when a.executions_delta > 0 then round(a.ROWS_PROCESSED_DELTA/a.executions_delta,0)
when a.executions_delta = 0 then NULL end) rowsprc_exec
,round(a.SHARABLE_MEM/1024/1024,2) sharable_mb
,a.VERSION_COUNT
--a.disk_reads_delta physrds,
--a.buffer_gets_delta logrds,
--round(a.cpu_time_delta/1000000,0) "cpu_time(s)",
--round(a.elapsed_time_delta/1000000,0) "ela_time(s)",
--(case when a.executions_delta > 0 then round((a.FETCHES_DELTA/a.executions_delta)/1000000,0)
-- when a.executions_delta = 0 then NULL end) fetch_EXEC
--(case when a.executions_delta > 0 then round(a.disk_reads_delta/a.executions_delta,0) when a.executions_delta = 0 then NULL end)/(case when a.executions_delta > 0 then round(a.ROWS_PROCESSED_DELTA/a.executions_delta,0) when a.executions_delta = 0 then NULL end) pr_per_row
from DBA_HIST_SQLSTAT a,
DBA_HIST_SNAPSHOT b
where 1=1
-- a.snap_id between (SELECT MIN(SNAP_ID) FROM DBA_HIST_SNAPSHOT WHERE BEGIN_INTERVAL_TIME >= to_date('&start_in','DD-MON-RRRR HH24:MI:SS')) and (SELECT MAX(SNAP_ID) FROM DBA_HIST_SNAPSHOT WHERE BEGIN_INTERVAL_TIME < to_date('&end_in','DD-MON-RRRR HH24:MI:SS'))
and a.snap_id = b.snap_id
and a.instance_number = b.instance_number
and a.sql_id = '&sql_id'
and a.executions_delta > 0
order by b.BEGIN_INTERVAL_TIME
--,a.plan_hash_value
/
set linesize 300 pagesize 300
col is_bind_aware for a15
col is_bind_sensitive for a18
col is_shareable for a18
select sql_id
, child_number
, is_bind_aware
, is_bind_sensitive
, is_shareable
, to_char(exact_matching_signature) sig
, executions
, plan_hash_value
from gv$sql
where sql_id = '&sql_id'
;
define sql_id='7grvag6ayaxn1'
SET lin 400 ver OFF
COL instance_number FOR 9999 HEA 'Inst'
COL end_time HEA 'End Time'
COL plan_hash_value HEA 'Plan|Hash Value'
COL executions_total FOR 999,999,999,999 HEA 'Execs|Total'
COL rows_per_exec for a15 HEA 'Rows Per Exec'
COL et_secs_per_exec for a15 HEA 'Elap Secs|Per Exec'
COL cpu_secs_per_exec for a15 HEA 'CPU Secs|Per Exec'
COL io_secs_per_exec for a15 HEA 'IO Secs|Per Exec'
COL cl_secs_per_exec for a15 HEA 'Clus Secs|Per Exec'
COL ap_secs_per_exec for a15 HEA 'App Secs|Per Exec'
COL cc_secs_per_exec for a15 HEA 'Conc Secs|Per Exec'
COL pl_secs_per_exec for a15 HEA 'PLSQL Secs|Per Exec'
COL ja_secs_per_exec for a15 HEA 'Java Secs|Per Exec'
COL cpu_per_exec for a15 HEA 'Cpu Secs|Per Exec'
SELECT h.instance_number,h.snap_id,
TO_CHAR(CAST(s.end_interval_time AS DATE), 'DD-MM-YYYY HH24:MI') end_time,
h.con_id,
h.sql_id,
h.plan_hash_value,
h.executions_total,
TO_CHAR(ROUND(h.rows_processed_total / h.executions_total), '999,999,999,999') rows_per_exec,
TO_CHAR(ROUND(h.elapsed_time_total / h.executions_total / 1e6, 3), '999,990.000') et_secs_per_exec,
TO_CHAR(ROUND(h.cpu_time_total / h.executions_total / 1e6, 3), '999,990.000') cpu_secs_per_exec,
TO_CHAR(ROUND(h.iowait_total / h.executions_total / 1e6, 3), '999,990.000') io_secs_per_exec,
TO_CHAR(ROUND(h.clwait_total / h.executions_total / 1e6, 3), '999,990.000') cl_secs_per_exec,
TO_CHAR(ROUND(h.apwait_total / h.executions_total / 1e6, 3), '999,990.000') ap_secs_per_exec,
TO_CHAR(ROUND(h.ccwait_total / h.executions_total / 1e6, 3), '999,990.000') cc_secs_per_exec,
TO_CHAR(ROUND(h.plsexec_time_total / h.executions_total / 1e6, 3), '999,990.000') pl_secs_per_exec,
TO_CHAR(ROUND(h.javexec_time_total / h.executions_total / 1e6, 3), '999,990.000') ja_secs_per_exec,
TO_CHAR(ROUND(h.CPU_TIME_TOTAL / h.executions_total / 1e6, 3), '999,990.000') cpu_per_exec
FROM dba_hist_sqlstat h,
dba_hist_snapshot s
WHERE h.sql_id = '&sql_id.'
-- AND h.executions_total > 16000000
AND s.snap_id = h.snap_id
AND s.dbid = h.dbid
AND s.instance_number = h.instance_number
ORDER BY
h.sql_id,
h.instance_number,
s.end_interval_time,
h.plan_hash_value
/
--- ===
set linesize 400 col TIME for a30 -- def sql_id=" " def days_history="10" def interval_hours="1" select hss.instance_number inst, to_char(trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)- (trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24,'dd.mm.yyyy hh24:mi:ss') time, plan_hash_value, hss.sql_id, sum(hss.executions_delta) executions, round(sum(hss.elapsed_time_delta)/1000000,3) elapsed_time_s, round(sum(hss.cpu_time_delta)/1000000,3) cpu_time_s, round(sum(hss.iowait_delta)/1000000,3) iowait_s, round(sum(hss.clwait_delta)/1000000,3) clwait_s, round(sum(hss.apwait_delta)/1000000,3) apwait_s, round(sum(hss.ccwait_delta)/1000000,3) ccwait_s, round(sum(hss.rows_processed_delta),3) rows_processed, round(sum(hss.buffer_gets_delta),3) buffer_gets, round(sum(hss.disk_reads_delta),3) disk_reads, round(sum(hss.direct_writes_delta),3) direct_writes from dba_hist_sqlstat hss, dba_hist_snapshot hs where hss.sql_id='&sql_id' and hss.snap_id=hs.snap_id and hs.begin_interval_time>=trunc(sysdate)-&days_history+1 group by hss.sql_id,hss.instance_number, trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)-(trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24,plan_hash_value having sum(hss.executions_delta)>0 order by hss.instance_number, trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)-(trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24, 4 desc;
--- =====
from Web
DEF days_of_history_accessed = '10' DEF captured_at_least_x_times = '10' DEF captured_at_least_x_days_apart = '5' DEF med_elap_microsecs_threshold = '1e4' DEF min_slope_threshold = '0.1' DEF max_num_rows = '20' WITH per_time AS ( SELECT h.dbid, h.sql_id, SYSDATE - CAST(s.end_interval_time AS DATE) days_ago, SUM(h.elapsed_time_total) / SUM(h.executions_total) time_per_exec FROM dba_hist_sqlstat h, dba_hist_snapshot s WHERE h.executions_total > 0 AND s.snap_id = h.snap_id AND s.dbid = h.dbid AND s.instance_number = h.instance_number AND CAST(s.end_interval_time AS DATE) > SYSDATE - &&days_of_history_accessed GROUP BY h.dbid, h.sql_id, SYSDATE - CAST(s.end_interval_time AS DATE) ), avg_time AS ( SELECT dbid, sql_id, MEDIAN(time_per_exec) med_time_per_exec, STDDEV(time_per_exec) std_time_per_exec, AVG(time_per_exec) avg_time_per_exec, MIN(time_per_exec) min_time_per_exec, MAX(time_per_exec) max_time_per_exec FROM per_time GROUP BY dbid, sql_id HAVING COUNT(*) >= &&captured_at_least_x_times AND MAX(days_ago) - MIN(days_ago) >= &&captured_at_least_x_days_apart. AND MEDIAN(time_per_exec) > &&med_elap_microsecs_threshold ), time_over_median AS ( SELECT h.dbid, h.sql_id, h.days_ago, (h.time_per_exec / a.med_time_per_exec) time_per_exec_over_med, a.med_time_per_exec, a.std_time_per_exec, a.avg_time_per_exec, a.min_time_per_exec, a.max_time_per_exec FROM per_time h, avg_time a WHERE a.sql_id = h.sql_id ), ranked AS ( SELECT RANK () OVER (ORDER BY ABS(REGR_SLOPE(t.time_per_exec_over_med, t.days_ago)) DESC) rank_num, t.dbid, t.sql_id, CASE WHEN REGR_SLOPE(t.time_per_exec_over_med, t.days_ago) > 0 THEN 'IMPROVING' ELSE 'REGRESSING' END change, ROUND(REGR_SLOPE(t.time_per_exec_over_med, t.days_ago), 3) slope, ROUND(AVG(t.med_time_per_exec)/1e6, 3) med_secs_per_exec, ROUND(AVG(t.std_time_per_exec)/1e6, 3) std_secs_per_exec, ROUND(AVG(t.avg_time_per_exec)/1e6, 3) avg_secs_per_exec, ROUND(MIN(t.min_time_per_exec)/1e6, 3) min_secs_per_exec, ROUND(MAX(t.max_time_per_exec)/1e6, 3) max_secs_per_exec FROM time_over_median t GROUP BY t.dbid, t.sql_id HAVING ABS(REGR_SLOPE(t.time_per_exec_over_med, t.days_ago)) > &&min_slope_threshold. ) SELECT LPAD(ROWNUM, 2) row_n, r.sql_id, r.change, TO_CHAR(r.slope, '990.000MI') slope, TO_CHAR(r.med_secs_per_exec, '999,990.000') med_secs_per_exec, TO_CHAR(r.std_secs_per_exec, '999,990.000') std_secs_per_exec, TO_CHAR(r.avg_secs_per_exec, '999,990.000') avg_secs_per_exec, TO_CHAR(r.min_secs_per_exec, '999,990.000') min_secs_per_exec, TO_CHAR(r.max_secs_per_exec, '999,990.000') max_secs_per_exec, (SELECT COUNT(DISTINCT p.plan_hash_value) FROM dba_hist_sql_plan p WHERE p.dbid = r.dbid AND p.sql_id = r.sql_id) plans, REPLACE((SELECT DBMS_LOB.SUBSTR(s.sql_text, 80) FROM dba_hist_sqltext s WHERE s.dbid = r.dbid AND s.sql_id = r.sql_id and rownum<2), CHR(10)) sql_text_80 FROM ranked r WHERE r.rank_num <= &&max_num_rows. ORDER BY r.rank_num;
set linesize 300 col LAST_SEEN for a25 col FIRST_SEEN for a25 col SQL_TEXT for a50 wrap select 'CACHE' plan_source , p.sql_id , p.plan_hash_value , min(to_date(p.first_load_time, 'YYYY-MM-DD/HH24:MI:SS')) first_seen , max(to_date(p.last_active_time)) last_seen , to_char(substr(p.sql_text, 1, 50)) sql_text from gv$sqlarea p where p.sql_id='&sql_id' group by p.sql_id, p.plan_hash_value, to_char(substr(p.sql_text, 1, 50)) union all select 'AWR' , h.sql_id , h.plan_hash_value , min(s.begin_interval_time) , max(s.end_interval_time) , to_char(substr(t.sql_text, 1, 50)) from dba_hist_sqlstat h , dba_hist_sql_plan p , dba_hist_snapshot s , dba_hist_sqltext t where h.snap_id=s.snap_id and h.dbid=p.dbid and h.sql_id=p.sql_id and h.sql_id='&sql_id' and t.sql_id=h.sql_id group by h.sql_id, h.plan_hash_value, to_char(substr(t.sql_text, 1, 50)) union all select 'SQLSET' , p.sql_id , p.plan_hash_value , p.plan_timestamp , p.plan_timestamp , to_char(p.sql_text) from dba_sqlset_statements p where p.sql_id='&sql_id' ;
===
set linesize 300 pages 1200
col snap_id for 999999
col BEGIN_INTERVAL_TIME for a27
col INST for 9
col END_INTERVAL_TIME for a23
col INSTANCE_NUMBER for 99
col OPTIMIZER_COST for 9999999999
col PLSEXEC_TIME_TOTAL for 999
col STARTUP_TIME for a22
col PARSING_SCHEMA_ID for 9999999999999
col ELAPSED_TIME1 for a15
select st.snap_id,
st.INSTANCE_NUMBER as INST,
st.sql_id ,
st.OPTIMIZER_COST,
sn.BEGIN_INTERVAL_TIME,
st.PLAN_HASH_VALUE,
round( st.CPU_TIME_DELTA/1000000,3) as CPU,
round(st.IOWAIT_DELTA/1000000,3) as IO_Wait,
round(st.ELAPSED_TIME_DELTA/1000000,3) as elapsed_time,
round(st.BUFFER_GETS_DELTA/1000000,3) as buffer_gets_M,
round(st.BUFFER_GETS_TOTAL/1000000,3) as BUFFER_GETS_TOTAL_M,
round(st.ELAPSED_TIME_TOTAL/1000000,3) as ELAPSED_TIME_TOTAL ,
round(st.FETCHES_DELTA,3) as Num_executions ,
case (ELAPSED_TIME_TOTAL-ELAPSED_TIME_DELTA) when 0 then 'start' end ELAPSED_TIME1
from dba_hist_sqlstat st, dba_hist_snapshot sn
where st.snap_id=sn.snap_id
and st.INSTANCE_NUMBER=sn.INSTANCE_NUMBER
and st.sql_id in ('&sql_id')
order by st.INSTANCE_NUMBER , st.SQL_ID, st.snap_id;
=====set linesize 999 col avg_et_secs justify right format 9999999.99 col cost justify right format 9999999999 col timestamp justify center format a25 col parsing_schema_name justify center format a30 col inst_id format 999999999 alter session set nls_date_format='dd-mm-yyyy hh24:mi:ss'; select 'gv$sqlarea_plan_hash' source, INST_ID, SQL_ID, PLAN_HASH_VALUE, round(elapsed_time/decode(nvl(executions,0),0,1,executions)/1e6/decode(px_servers_executions,0,1,px_servers_executions)/decode(nvl(executions,0),0,1,executions),2) avg_et_secs, px_servers_executions/decode(nvl(executions,0),0,1,executions) avg_px, optimizer_cost cost, LAST_LOAD_TIME timestamp, parsing_schema_name --FIRST_LOAD_TIME, LAST_LOAD_TIME, LAST_ACTIVE_TIME, SQL_PROFILE from gv$sqlarea_plan_hash where sql_id = nvl(trim('&sql_id'),sql_id) UNION SELECT 'dba_hist_sql_plan' source, null INST_ID, t1.sql_id sql_id, t1.plan_hash_value plan_hash_value, t2.avg_et_secs avg_et_secs, t2.avg_px, t1.cost cost, t1.timestamp timestamp, NULL parsing_schema_name FROM dba_hist_sql_plan t1, ( SELECT sql_id, plan_hash_value, --round(SUM(elapsed_time_total)/decode(SUM(executions_total),0,1,SUM(executions_total))/1e6,2) avg_et_secs round(SUM(elapsed_time_total)/decode(SUM(executions_total),0,1,SUM(executions_total))/1e6/decode(SUM(px_servers_execs_total),0,1,SUM(px_servers_execs_total))/decode(SUM(executions_total),0,1,SUM(executions_total)),2) avg_et_secs, SUM(px_servers_execs_total)/decode(SUM(executions_total),0,1,SUM(executions_total)) avg_px FROM dba_hist_sqlstat WHERE executions_total > 0 GROUP BY sql_id, plan_hash_value ) t2 WHERE t1.sql_id = nvl(TRIM('&sql_id'), t1.sql_id) AND t1.depth = 0 AND t1.sql_id = t2.sql_id(+) AND t1.plan_hash_value = t2.plan_hash_value(+) order by avg_et_secs, cost /
SOURCE INST_ID SQL_ID PLAN_HASH_VALUE AVG_ET_SECS AVG_PX COST TIMESTAMP PARSING_SCHEMA_NAME
-------------------- ---------- ------------- --------------- ----------- ---------- ----------- ------------------------- ------------------------------
dba_hist_sql_plan 8fc3jxu7p0y25 0 .00 0 1 03-10-2021 15:18:50
gv$sqlarea_plan_hash 1 8fc3jxu7p0y25 0 .00 0 1 04-05-2022 02:53:02 SYS
gv$sqlarea_plan_hash 2 8fc3jxu7p0y25 0 .00 0 1 04-05-2022 03:38:06 SYS
--- ===
| SET TERMOUT OFF pagesize 5000 tab off verify off linesize 999 trimspool on trimout on null "" | |
| SET TERMOUT ON | |
| COL exec_per_sec FOR 99999990 | |
| COL ela_ms_per_sec FOR 99999990 | |
| COL rows_per_sec FOR 99999990 | |
| COL lios_per_sec FOR 99999990 | |
| COL blkrd_per_sec FOR 99999990 | |
| COL cpu_ms_per_sec FOR 99999990 | |
| COL iow_ms_per_sec FOR 99999990 | |
| COL clw_ms_per_sec FOR 99999990 | |
| COL apw_ms_per_sec FOR 99999990 | |
| COL ccw_ms_per_sec FOR 99999990 | |
| SELECT | |
| CAST(begin_interval_time AS DATE) begin_interval_time | |
| , sql_id | |
| , plan_hash_value | |
| , ROUND(SUM(executions_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) exec_per_sec | |
| , ROUND(SUM(elapsed_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ela_ms_per_sec | |
| , ROUND(SUM(rows_processed_delta) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) rows_per_sec | |
| , ROUND(SUM(buffer_gets_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) lios_per_sec | |
| , ROUND(SUM(disk_reads_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) blkrd_per_sec | |
| , ROUND(SUM(cpu_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) cpu_ms_per_sec | |
| , ROUND(SUM(iowait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) iow_ms_per_sec | |
| , ROUND(SUM(clwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) clw_ms_per_sec | |
| , ROUND(SUM(apwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) apw_ms_per_sec | |
| , ROUND(SUM(ccwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ccw_ms_per_sec | |
| FROM | |
| dba_hist_snapshot sn | |
| , dba_hist_sqlstat st | |
| WHERE | |
| sn.snap_id = st.snap_id | |
| AND sn.dbid = st.dbid | |
| AND sn.instance_number = st.instance_number | |
| AND sql_id = '&1' | |
| AND plan_hash_value LIKE '&2' | |
| AND begin_interval_time >= &3 | |
| AND end_interval_time <= &4 | |
| GROUP BY | |
| CAST(begin_interval_time AS DATE) | |
| , CAST(end_interval_time AS DATE) | |
| , sql_id | |
| , plan_hash_value | |
| ORDER BY | |
| begin_interval_time | |
| , sql_id | |
| , plan_hash_value | |
| / |
----===
set linesize 500 pagesize 300
SELECT
hsq.sql_id,
hsq.plan_hash_value,
NVL(SUM(hsq.executions_delta),0) AS total_exec,
ROUND(SUM(hsq.elapsed_time_delta)/1000000,2) AS elapsed_time_total,
ROUND(SUM(hsq.px_servers_execs_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),2) AS avg_px_servers_execs,
ROUND(SUM(hsq.elapsed_time_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_elapsed_time,
ROUND(SUM(hsq.cpu_time_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_cpu_time,
ROUND(SUM(hsq.iowait_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_iowait,
ROUND(SUM(hsq.clwait_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_cluster_wait,
ROUND(SUM(hsq.apwait_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_application_wait,
ROUND(SUM(hsq.ccwait_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/1000000,2) AS avg_concurrency_wait,
ROUND(SUM(hsq.rows_processed_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),2) AS avg_rows_processed,
ROUND(SUM(hsq.buffer_gets_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),2) AS avg_buffer_gets,
ROUND(SUM(hsq.disk_reads_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),2) AS avg_disk_reads,
ROUND(SUM(hsq.direct_writes_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),2) AS avg_direct_writes,
ROUND(SUM(hsq.io_interconnect_bytes_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/(1024*1024),0) AS avg_io_interconnect_mb,
ROUND(SUM(hsq.physical_read_requests_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),0) AS avg_phys_read_requests,
ROUND(SUM(hsq.physical_read_bytes_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/(1024*1024),0) AS avg_phys_read_mb,
ROUND(SUM(hsq.physical_write_requests_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta)),0) AS avg_phys_write_requests,
ROUND(SUM(hsq.physical_write_bytes_delta)/DECODE(SUM(hsq.executions_delta),0,NULL,SUM(hsq.executions_delta))/(1024*1024),0) AS avg_phys_write_mb
FROM dba_hist_sqlstat hsq
WHERE hsq.sql_id='&sql_id.'
GROUP BY hsq.sql_id, hsq.plan_hash_value;
from web
set linesize 1500 pagesize 500
col sql_text for a70 wrap
col begin_interval_time for a25 col end_interval_time for a25
col sql_profile for a15
col module for a15
col action for a15
select
s.sql_id,
round(DECODE(io_offload_elig_bytes_delta, 0, 0, 100 *(io_offload_elig_bytes_delta - io_interconnect_bytes_delta) / DECODE(io_offload_elig_bytes_delta , 0, 1, io_offload_elig_bytes_delta)), 6) "IO_SAVED_PCT",
round(DECODE(io_offload_elig_bytes_delta, 0, 0, 100 *(io_offload_elig_bytes_delta) / DECODE(physical_read_bytes_delta , 0, 1, physical_read_bytes_delta)), 6) "CELL_OFFLOAD_EFFICIENCY",
begin_interval_time,
end_interval_time,
executions_delta,
elapsed_time_delta / 1000000 "ELAPSED_TIME in SECONDS",
round(elapsed_time_delta / nvl(nullif(executions_delta, 0), 1) / 1000000, 6) "ELAPSED_TIME per ex in SEC",
cpu_time_delta / 1000000 "CPU_TIME_delta in SECONDS",
round(cpu_time_delta / nvl(nullif(executions_delta, 0), 1) / 1000000, 6) "CPU_TIME per ex in SEC",
plan_hash_value plan_hash,
command_type,
DECODE(io_offload_elig_bytes_delta, 0, 'No', 'Yes') offload,
s.instance_number,
module,
action,
sql_profile,
fetches_delta,
end_of_fetch_count_delta,
sorts_delta,
px_servers_execs_delta,
loads_delta,
invalidations_delta,
parse_calls_delta,
disk_reads_delta,
buffer_gets_delta,
rows_processed_delta,
iowait_delta / 1000000 "IO WAIT in SECONDS",
clwait_delta / 1000000 "CLUSTER WAIT in SECONDS",
apwait_delta / 1000000 "APPLICATION WAIT in SECONDS",
ccwait_delta / 1000000 "CONCURRENCY WAIT in SECONDS",
plsexec_time_delta / 1000000 "PL/SQL WAIT in SECONDS",
javexec_time_delta / 1000000 "Java EXEC WAIT in SECONDS",
round(io_offload_elig_bytes_delta / 1024 / 1024, 2) "CELL_OFFLD_ELIG in MB",
round(io_offload_elig_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "CELL_OFFLD_ELIG per ex in MB",
round(cell_uncompressed_bytes_delta / 1024 / 1024, 2) "CELL_UNCOMPRSD in MB",
round(cell_uncompressed_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "CELL_UNCOMPRSD per ex in MB",
round(io_offload_return_bytes_delta / 1024 / 1024, 2) "CELL_OFFLD_RTN in MB",
round(io_offload_return_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "CELL_OFFLD_RTN per ex in MB",
round(io_interconnect_bytes_delta / 1024 / 1024, 2) "IO_INTERCONNECT in MB",
round(io_interconnect_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "IO_INTERCONNECT per ex in MB",
round(physical_read_bytes_delta / 1024 / 1024, 2) "PHYSICAL_READ in MB",
round(physical_read_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "PHYSICAL_READ per ex in MB",
physical_read_requests_delta,
round(physical_read_requests_delta / nvl(nullif(executions_delta, 0), 1)) "PHYSICAL_READ per ex",
optimized_physical_reads_delta,
round(optimized_physical_reads_delta / nvl(nullif(executions_delta, 0), 1)) "OPTIMIZED_READS per ex",
round(physical_write_bytes_delta / 1024 / 1024, 2) "PHYSICAL_WRITE in MB",
round(physical_write_bytes_delta / nvl(nullif(executions_delta, 0), 1) / 1024 / 1024, 2) "PHYSICAL_WRITE per ex in MB",
physical_write_requests_delta,
round(physical_write_requests_delta / nvl(nullif(executions_delta, 0), 1)) "OPTIMIZED_WRITES per ex",
direct_writes_delta,
dbms_lob.substr(sql_text, 4000, 1) sql_text
FROM dba_hist_sqlstat s, dba_hist_sqltext t, dba_hist_snapshot u
WHERE 1=1
-- and io_offload_elig_bytes_delta > 0
and s.sql_id = t.sql_id
AND ( s.snap_id = u.snap_id AND s.instance_number = u.instance_number )
and t.sql_id='&sql_id'
ORDER BY 2 DESC
;
SELECT
p.sql_id
,p.plan_hash_value
,p.child_number
,t.phv2
FROM
gv$sql_plan p
,xmltable('for $i in /other_xml/info
where $i/@type eq "plan_hash_2"
return $i'
passing xmltype(p.other_xml)
columns phv2 number path '/') t
WHERE p.sql_id = '&sql_id'
AND p.other_xml is not null;
set linesize 300 pagesize 300
col INDEX_OWNER for a20
col COLUMN_NAME for a20
col TABLE_NAME for a20
col INDEX_NAME for a20
SELECT ic.index_owner, ic.index_name, ic.table_name, ic.column_name, ic.column_position col_pos, tc.last_analyzed, tc. sample_size, tc.num_distinct, tc.num_nulls, tc.density, tc.histogram, tc.num_buckets
FROM dba_ind_columns ic
, dba_tab_columns tc
WHERE ic.index_name IN (
select distinct rtrim(substr(plan_table_output, instr(plan_table_output, '|', 1, 3)+2, (instr(plan_table_output, '|', 1, 4)-instr(plan_table_output, '|', 1, 3)-2)), ' ')
from (
SELECT plan_table_output
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id', 0, 'BASIC'))
)
where plan_table_output like '%INDEX%'
)
AND ic.table_owner=tc.owner
AND ic.table_name=tc.table_name
AND ic.column_name=tc.column_name
ORDER BY ic.table_owner, ic.table_name, ic.index_name, ic.column_position
/
INDEX_OWNER INDEX_NAME TABLE_NAME COLUMN_NAME COL_POS LAST_ANALYZED SAMPLE_SIZE NUM_DISTINCT NUM_NULLS DENSITY HISTOGRAM NUM_BUCKETS
-------------------- -------------------- -------------------- -------------------- ---------- -------------- ----------- ------------ ---------- ---------- --------------- -----------
SYS T_R_I1 T_RANGE ID 1 NONE
SYS T_R_I1 T_RANGE C1 2 NONE
==========================
/* --from
--https://www.williamrobertson.net/documents/performance-for-sqlid.html
-- Diagnostic queries for a specified SQL ID
-- PL/SQL Developer format - open in an SQL window, select-all and execute, entering SQL ID at the prompt.
-- William Robertson, 2015
https://www.williamrobertson.net/documents/
*/
define sql_id='22356bkgsdcnh' ---!!!!!
set linesize 500 pagesize 300
col "Avg time/1M rows" for a15
col "Avg time/row" for a15
col "Total time" for a15
col "Average seconds" for 999999999999
col "Average time" for a15
select
sql_id ,
plan_hash_value as "Plan hash"
, sum(executions_calc) as "Times called"
, sum(end_of_fetch_count) as "Times completed"
, 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 seconds"
, cast(numtodsinterval(sum(elapsed_time)/1E6 / nvl(sum(executions_calc),1),'SECOND') as interval day(1) to second(1)) as "Average time"
-- , 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 and a
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 sql_id
, trunc(s.last_active_time) as exec_date
, 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 gv$sqlstats s
union
select s.sql_id
, trunc(cast(h.begin_interval_time as date)) as exec_date
, plan_hash_value
--, executions_delta executions
, nullif(executions_delta,0) executions_calc
, px_servers_execs_delta as px_servers_executions
, elapsed_time_delta as elapsed_time
, buffer_gets_delta as buffer_gets
, rows_processed_delta as rows_processed
, end_of_fetch_count_delta as end_of_fetch_count
from dba_hist_sqlstat s
join dba_hist_snapshot h on h.snap_id = s.snap_id and h.dbid = s.dbid and h.instance_number = s.instance_number
)
where 1=1
and sql_id = '&sql_id'
group by sql_id,plan_hash_value
having sum(executions_calc) > 0
order by plan_hash_value;
set linesize 700 pagesize 300
col "Avg time/1M rows" for a15
col "Avg time/row" for a15
col "Total time" for a15
col "Average seconds" for 999999999999
col "Average time" for a15
col AVG_TIME for a15
col AVG_TIME_PER_ROW for a15
select
--h.snap_id,
s.sql_id ,case when s.plan_hash_value =
( select plan_hash_value from
( select plan_hash_value, row_number() over (order by timestamp desc) as seq
from gv$sql_plan p
where p.sql_id = '&sql_id'
-- and p.child_number = :sql_child_number
-- and p.inst_id = :instance
)
where seq = 1 )
then 'Y'
end as current_plan
, trunc(cast (h.begin_interval_time as date)) as exec_date
, plan_hash_value
, sum(executions_delta) executions
, cast(numtodsinterval(sum(elapsed_time_delta)/1E6,'SECOND') as interval day(1) to second(2)) as total_time
, cast(numtodsinterval(sum(elapsed_time_delta)/ nullif(sum(executions_delta),0)/1E6,'SECOND') as interval day(1) to second(1)) as avg_time
, sum(buffer_gets_delta) buffer_gets
, round(sum(buffer_gets_delta)/nullif(sum(executions_delta),0)) as buffer_gets_per_exec
, sum(rows_processed_delta) as "ROWS"
, round(sum(rows_processed_delta) / nullif(sum(executions_delta),0),1) as rows_per_exec
, cast(numtodsinterval(sum(elapsed_time_delta)/nullif(sum(rows_processed_delta),0)/1E6,'SECOND') as interval day(1) to second(3)) as avg_time_per_row
, round(sum(buffer_gets_delta)/nullif(sum(rows_processed_delta),0)) as buffer_gets_per_row
from dba_hist_sqlstat s
join dba_hist_snapshot h on h.snap_id = s.snap_id and h.dbid = s.dbid
where s.sql_id = '&sql_id'
group by
--h.snap_id,
s.sql_id ,trunc(cast (h.begin_interval_time as date)), s.plan_hash_value
order by trunc(cast (h.begin_interval_time as date)) desc, 1 nulls last, s.plan_hash_value
;
==============
set linesize 700 pagesize 300
col "Avg time/1M rows" for a15
col "Avg time/row" for a15
col "Total time" for a15
col "Average seconds" for 999999999999
col "Average time" for a15
col PLSQL_EXEC_TIME for a15
col TOTAL_TIME for a15
col JAVA_EXEC_TIME for a15
col AVERAGE_TIME for a15
select sql_id
, plan_hash_value
, last_active_time
, executions
, round(100 * parse_calls/nullif(executions,0),1) as "Parsed%"
, parse_calls
, cast(numtodsinterval(elapsed_time/1E6,'SECOND') as interval day(0) to second(0)) as total_time
, cast(numtodsinterval(elapsed_time / nullif(executions,0) / 1E6,'SECOND') as interval day(0) to second(0)) as average_time
, round(100 * cpu_time / elapsed_time,1) "CPU%"
, round(100 * user_io_wait_time / elapsed_time,1) "IO%"
, round(100 * concurrency_wait_time / elapsed_time,1) "CONCURRRENCY%"
, round(100 * application_wait_time / elapsed_time,1) "APPLICATION%"
, round(100 * plsql_exec_time / elapsed_time,1) "PL/SQL%"
, buffer_gets buffer_gets_total
, round(buffer_gets / nullif(executions,0)) as buffer_gets_per_exec
, disk_reads
, round(rows_processed / nullif(fetches,0),1) as rows_per_fetch
, round(rows_processed / nullif(executions,0),1) as rows_per_exec
, direct_writes
, rows_processed
, fetches
, end_of_fetch_count
, loads
, version_count
, invalidations
, px_servers_executions
, round(avg_hard_parse_time / 1E6,2) as avg_hard_parse_secs
, cluster_wait_time
, cast(numtodsinterval(plsql_exec_time/1E6,'SECOND') as interval day(0) to second(0)) as plsql_exec_time
, cast(numtodsinterval(java_exec_time/1E6,'SECOND') as interval day(0) to second(0)) as java_exec_time
, sorts
, sharable_mem
, total_sharable_mem
, last_active_child_address
, serializable_aborts
from gv$sqlstats s
where s.sql_id = '&sql_id'
;
with parsing schema
set linesize 300 pagesize 200
column_object_name for a31
column owner for a15
column "%TOT%" for a6
column SQL_TEXT for a80 wrap on
col parsing_schema_name for A12
select distinct sql_id1,sub.parsing_schema_name,
-- replace(replace(replace(DBMS_LOB.SUBSTR(DHST.sql_text,4000,1),chr(10),' '),chr(9),' '),' ',' ') SQL_TEXT,
sub.EXECUTIONS,round((sub.EXECUTIONS/sub2.EXECUTIONS_TOTAL)*100)||'%' "%TOT%",
sub.DISK_READS,round((sub.DISK_READS/sub2.DISK_READS_TOTAL)*100)||'%' "%TOT%",
sub.BUFFER_GETS,round((sub.BUFFER_GETS/sub2.BUFFER_GETS_TOTAL)*100)||'%' "%TOT%",
sub.ELAPSED_TIME,round((sub.ELAPSED_TIME/sub2.ELAPSED_TIME_TOTAL)*100)||'%' "%TOT%",
sub.IOWAIT,sub.ROWS_PROCESSED,
sub.SEC_PER_EXEC "SEC/EXE",
round((sub.EXECUTIONS/sub2.EXECUTIONS_TOTAL)*100)+round((sub.DISK_READS/sub2.DISK_READS_TOTAL)*100)+round((sub.BUFFER_GETS/sub2.BUFFER_GETS_TOTAL)*100)+round((sub.ELAPSED_TIME/sub2.ELAPSED_TIME_TOTAL)*100) RANK ,
replace(replace(replace(DBMS_LOB.SUBSTR(DHST.sql_text,4000,1),chr(10),' '),chr(9),' '),' ',' ') SQL_TEXT
from DBA_HIST_SQLTEXT DHST,
(
select distinct
SQL_ID sql_id1,
PARSING_SCHEMA_NAME,
round(sum(EXECUTIONS_DELTA)) as EXECUTIONS,
round(sum(PARSE_CALLS_DELTA)) as PARSE_CALLS,
round(sum(DISK_READS_DELTA)) as DISK_READS,
round(sum(BUFFER_GETS_DELTA)) as BUFFER_GETS,
round(sum(ROWS_PROCESSED_DELTA)) as ROWS_PROCESSED,
round(sum(CPU_TIME_DELTA/1000000)) as CPU_TIME,
round(sum(ELAPSED_TIME_DELTA/1000000)) ELAPSED_TIME,
round(sum(IOWAIT_DELTA)/1000000) as IOWAIT,
sum(ELAPSED_TIME_DELTA/1000000)/decode(sum(EXECUTIONS_DELTA),0,1,sum(EXECUTIONS_DELTA)) SEC_PER_EXEC
from
dba_hist_snapshot
natural join
dba_hist_sqlstat DHS
natural join
dba_hist_sql_plan DHSP
where 1=1
and SQL_ID='&sql_id'
group by SQL_ID,PARSING_SCHEMA_NAME
) sub,
(
select decode(round(sum(EXECUTIONS_DELTA)),0,1,round(sum(EXECUTIONS_DELTA))) as EXECUTIONS_TOTAL,
decode(round(sum(DISK_READS_DELTA)),0,1,round(sum(DISK_READS_DELTA))) as DISK_READS_TOTAL,
decode(round(sum(BUFFER_GETS_DELTA)),0,1,round(sum(BUFFER_GETS_DELTA))) as BUFFER_GETS_TOTAL,
decode(round(sum(ELAPSED_TIME_DELTA/1000000)),0,1,round(sum(ELAPSED_TIME_DELTA/1000000))) as ELAPSED_TIME_TOTAL
from
dba_hist_snapshot
natural join
dba_hist_sqlstat DHS
natural join
dba_hist_sql_plan DHSP
where
1=1
) sub2
where DHST.sql_id = sub.sql_id1
order by RANK Desc
/
col NAME for a20
col VALUE_STRING for a20
select name, datatype_string, last_captured, value_string
from ( select name
, datatype_string
, last_captured
, value_string
, row_number() over(partition by position order by last_captured nulls last) as seq
from gv$sql_bind_capture
where sql_id = '&sql_id'
-- and child_number = :sql_child_number
--and address = :sql_address
-- and hash_value = :sql_hash_value
)
where seq = 1
order by last_captured
;
--=============
select * from
(select sql_id, nonshared_reason, count(*) from gv$sql_shared_cursor
unpivot
(nonshared_value for nonshared_reason in (
UNBOUND_CURSOR as 'UNBOUND_CURSOR',
SQL_TYPE_MISMATCH as 'SQL_TYPE_MISMATCH',
OPTIMIZER_MISMATCH as 'OPTIMIZER_MISMATCH',
OUTLINE_MISMATCH as 'OUTLINE_MISMATCH',
STATS_ROW_MISMATCH as 'STATS_ROW_MISMATCH',
LITERAL_MISMATCH as 'LITERAL_MISMATCH',
FORCE_HARD_PARSE as 'FORCE_HARD_PARSE',
EXPLAIN_PLAN_CURSOR as 'EXPLAIN_PLAN_CURSOR',
BUFFERED_DML_MISMATCH as 'BUFFERED_DML_MISMATCH',
PDML_ENV_MISMATCH as 'PDML_ENV_MISMATCH',
INST_DRTLD_MISMATCH as 'INST_DRTLD_MISMATCH',
SLAVE_QC_MISMATCH as 'SLAVE_QC_MISMATCH',
TYPECHECK_MISMATCH as 'TYPECHECK_MISMATCH',
AUTH_CHECK_MISMATCH as 'AUTH_CHECK_MISMATCH',
BIND_MISMATCH as 'BIND_MISMATCH',
DESCRIBE_MISMATCH as 'DESCRIBE_MISMATCH',
LANGUAGE_MISMATCH as 'LANGUAGE_MISMATCH',
TRANSLATION_MISMATCH as 'TRANSLATION_MISMATCH',
BIND_EQUIV_FAILURE as 'BIND_EQUIV_FAILURE',
INSUFF_PRIVS as 'INSUFF_PRIVS',
INSUFF_PRIVS_REM as 'INSUFF_PRIVS_REM',
REMOTE_TRANS_MISMATCH as 'REMOTE_TRANS_MISMATCH',
LOGMINER_SESSION_MISMATCH as 'LOGMINER_SESSION_MISMATCH',
INCOMP_LTRL_MISMATCH as 'INCOMP_LTRL_MISMATCH',
OVERLAP_TIME_MISMATCH as 'OVERLAP_TIME_MISMATCH',
EDITION_MISMATCH as 'EDITION_MISMATCH',
MV_QUERY_GEN_MISMATCH as 'MV_QUERY_GEN_MISMATCH',
USER_BIND_PEEK_MISMATCH as 'USER_BIND_PEEK_MISMATCH',
TYPCHK_DEP_MISMATCH as 'TYPCHK_DEP_MISMATCH',
NO_TRIGGER_MISMATCH as 'NO_TRIGGER_MISMATCH',
FLASHBACK_CURSOR as 'FLASHBACK_CURSOR',
ANYDATA_TRANSFORMATION as 'ANYDATA_TRANSFORMATION',
PDDL_ENV_MISMATCH as 'PDDL_ENV_MISMATCH',
TOP_LEVEL_RPI_CURSOR as 'TOP_LEVEL_RPI_CURSOR',
DIFFERENT_LONG_LENGTH as 'DIFFERENT_LONG_LENGTH',
LOGICAL_STANDBY_APPLY as 'LOGICAL_STANDBY_APPLY',
DIFF_CALL_DURN as 'DIFF_CALL_DURN',
BIND_UACS_DIFF as 'BIND_UACS_DIFF',
PLSQL_CMP_SWITCHS_DIFF as 'PLSQL_CMP_SWITCHS_DIFF',
CURSOR_PARTS_MISMATCH as 'CURSOR_PARTS_MISMATCH',
STB_OBJECT_MISMATCH as 'STB_OBJECT_MISMATCH',
CROSSEDITION_TRIGGER_MISMATCH as 'CROSSEDITION_TRIGGER_MISMATCH',
PQ_SLAVE_MISMATCH as 'PQ_SLAVE_MISMATCH',
TOP_LEVEL_DDL_MISMATCH as 'TOP_LEVEL_DDL_MISMATCH',
MULTI_PX_MISMATCH as 'MULTI_PX_MISMATCH',
BIND_PEEKED_PQ_MISMATCH as 'BIND_PEEKED_PQ_MISMATCH',
MV_REWRITE_MISMATCH as 'MV_REWRITE_MISMATCH',
ROLL_INVALID_MISMATCH as 'ROLL_INVALID_MISMATCH',
OPTIMIZER_MODE_MISMATCH as 'OPTIMIZER_MODE_MISMATCH',
PX_MISMATCH as 'PX_MISMATCH',
MV_STALEOBJ_MISMATCH as 'MV_STALEOBJ_MISMATCH',
FLASHBACK_TABLE_MISMATCH as 'FLASHBACK_TABLE_MISMATCH',
LITREP_COMP_MISMATCH as 'LITREP_COMP_MISMATCH',
PLSQL_DEBUG as 'PLSQL_DEBUG',
LOAD_OPTIMIZER_STATS as 'LOAD_OPTIMIZER_STATS',
ACL_MISMATCH as 'ACL_MISMATCH',
FLASHBACK_ARCHIVE_MISMATCH as 'FLASHBACK_ARCHIVE_MISMATCH',
LOCK_USER_SCHEMA_FAILED as 'LOCK_USER_SCHEMA_FAILED',
REMOTE_MAPPING_MISMATCH as 'REMOTE_MAPPING_MISMATCH',
LOAD_RUNTIME_HEAP_FAILED as 'LOAD_RUNTIME_HEAP_FAILED',
HASH_MATCH_FAILED as 'HASH_MATCH_FAILED',
PURGED_CURSOR as 'PURGED_CURSOR',
BIND_LENGTH_UPGRADEABLE as 'BIND_LENGTH_UPGRADEABLE',
USE_FEEDBACK_STATS as 'USE_FEEDBACK_STATS'
))
where nonshared_value = 'Y'
group by sql_id, nonshared_reason
)
where 1=1
--and sql_id = :sql_id
and rownum <30
order by 3 desc
SQL_ID NONSHARED_REASON COUNT(*)
------------- ----------------------------- ----------
01xv155rhts3j ROLL_INVALID_MISMATCH 17
03gumnj26cbhx ROLL_INVALID_MISMATCH 11
02mh01mykt89k ROLL_INVALID_MISMATCH 9
062savj8zgzut ROLL_INVALID_MISMATCH 7
05tfhbgcrxa1t ROLL_INVALID_MISMATCH 5
04ws1hx445575 ROLL_INVALID_MISMATCH 5
04g72vx2dm3hj ROLL_INVALID_MISMATCH 5
04kug40zbu4dm OPTIMIZER_MISMATCH 4
---
define sql_id ='8m0g708ggsfsm'
set serveroutput on
DECLARE
v_count number;
v_sql varchar2(500);
v_sql_id varchar2(30) := '&sql_id';
BEGIN
v_sql_id := lower(v_sql_id);
dbms_output.put_line(chr(13)||chr(10));
dbms_output.put_line('sql_id: '||v_sql_id);
dbms_output.put_line('------------------------');
FOR c1 in
(select column_name
from dba_tab_columns
where table_name ='V_$SQL_SHARED_CURSOR'
and column_name not in ('SQL_ID', 'ADDRESS', 'CHILD_ADDRESS', 'CHILD_NUMBER', 'REASON', 'CON_ID')
order by column_id)
LOOP
v_sql := 'select count(*) from V_$SQL_SHARED_CURSOR
where sql_id='||''''||v_sql_id||''''||'
and '||c1.column_name||'='||''''||'Y'||'''';
execute immediate v_sql into v_count;
IF v_count > 0
THEN
dbms_output.put_line(' - '||rpad(c1.column_name,30)||' count: '||v_count);
END IF;
END LOOP;
END;
/
11.2.0.3: _cursor_obsolete_threshold=100
11.2.0.4: _cursor_obsolete_threshold=1024
12.1.0.1: _cursor_obsolete_threshold=1024
12.1.0.2: _cursor_obsolete_threshold=1024
12.2.0.1: _cursor_obsolete_threshold=8192 <<<<< _cursor_obsolete_threshold 1024 OD
18.3.0: _cursor_obsolete_threshold=8192
_cursor_obsolete_threshold
http://anuj-singh.blogspot.com/2021/05/sql-history.html
-- tab=Executions (historical)
set linesize 500
col "Run date" for a25
select run_date as "Run date"
, sql_id
, "First"
, "Last"
, plan_hash_value as "Plan hash"
, executions as "Times called"
, end_of_fetch_count as "Times completed"
, success_rate as "Success %"
, elapsed_time as "Total time"
, avg_time as "Avg time"
, avg_s as "Avg seconds"
, round(avg_s * (sum(avg_rows) over() / greatest(sum(avg_s) over(),1))) as "Avg s scaled to rows" -- for charting time vs rows
, avg_rows as "Avg rows"
, avg_bg as "Avg Buffer gets"
, bg_per_row as "Buffer gets/row"
, avg_time_per_row as "Time/row"
, px_servers_execs as "PX server executions"
from
( select s.sql_id,trunc(cast(t.begin_interval_time as date)) as run_date
, plan_hash_value
, to_char(min(cast(t.begin_interval_time as date)),'HH24:MI:SS') as "First"
, to_char(max(cast(t.end_interval_time as date)),'HH24:MI:SS') as "Last"
, sum(s.executions_delta) as executions
, sum(s.end_of_fetch_count_delta) as end_of_fetch_count
, max(s.executions_total) as executions_total
, max(s.end_of_fetch_count_total) as end_of_fetch_count_total
, least(100, round(100 * max(s.end_of_fetch_count_total) / nullif(max(s.executions_total),0),1)) as success_rate
, cast(numtodsinterval(max(s.elapsed_time_total)/1E6,'SECOND') as interval day(1) to second(2)) as elapsed_time
, cast(numtodsinterval(max(s.elapsed_time_total)/1E6 / nvl(nullif(max(s.executions_total),0),1),'SECOND') as interval day(1) to second(1)) as avg_time
, round(max(s.elapsed_time_total)/1E6 / nvl(nullif(max(s.executions_total),0),1),1) as avg_s
, round(max(s.buffer_gets_total)/nullif(max(s.executions_total),0)) as avg_bg
, round(max(s.buffer_gets_total)/nullif(max(s.rows_processed_total),0)) as bg_per_row
, max(s.rows_processed_total) as rows_processed
, round(max(s.rows_processed_total) / nullif(max(s.executions_total),0)) as avg_rows
, cast(numtodsinterval(max(s.elapsed_time_total)/nullif(max(s.rows_processed_total),0)/1E6,'SECOND') as interval day(1) to second(3)) as avg_time_per_row
, max(s.elapsed_time_total)/nullif(max(s.rows_processed_total),0)/1E6 as avg_s_per_row
, max(s.px_servers_execs_total) as px_servers_execs
from dba_hist_sqlstat s
join dba_hist_snapshot t on t.snap_id = s.snap_id and t.dbid = s.dbid and t.instance_number = s.instance_number
where 1=1
and sql_id = '&sql_id'
and rownum <10
group by sql_id,trunc(cast(t.begin_interval_time as date)), s.plan_hash_value )
order by 1, 2, plan_hash_value;
-- tab=Executions (recent)
set linesize 1000
col Average time" for a20
col "Average time PX" for a20
col "Average time" for a20
col "Total time" for a20
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 gv$sqlstats s )
where 1=1
and 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;
-- tab=Recent ASH history
set linesize 500
col TOP_LEVEL_CALL for a50
col CURRENT_PROCEDURE for a70 wrap
select ash.sql_id, ash.sql_exec_start, ash.sql_exec_id, ash.sql_plan_hash_value, ash.sql_child_number
, cast(numtodsinterval(count(*),'SECOND') as interval day(0) to second(0)) as elapsed
, ash.qc_session_id, ash.session_id, ash.session_serial#
-- , sum(io.block_gets) as block_gets, sum(io.consistent_gets) as consistent_gets, sum(io.physical_reads) as physical_reads, sum(io.consistent_changes) as consistent_changes
, tls.sql_text as top_level_call
, rtrim(p.owner ||'.'|| p.object_name ||'.'|| p.procedure_name,'.') as current_procedure
from gv$active_session_history ash
left join dba_procedures p on p.object_id = ash.plsql_object_id and nvl(p.subprogram_id,.5) = nvl(ash.plsql_subprogram_id,.5)
left join gv$sqlstats tls on tls.sql_id = ash.top_level_sql_id
where 1=1
and ash.sql_id = '&sql_id'
-- and ash.sql_exec_id is not null
and rownum<10
group by ash.sql_id, ash.sql_exec_start,ash.sql_plan_hash_value, ash.sql_child_number, ash.qc_session_id, ash.session_id, ash.session_serial#, ash.sql_exec_id
, tls.sql_text , p.owner, p.object_name, p.procedure_name
order by min(ash.sample_time), ash.sql_exec_start, ash.sql_exec_id, ash.qc_session_id nulls first;
-- tab=Recent ASH history with wait objs
set linesize 500
col USERNAME for a25
col WAIT_OBJECT for a15
col ELAPSED for a25
select ash.sql_id, ash.sql_exec_start, ash.sql_exec_id, ash.sql_plan_hash_value
, ash.session_id, ash.session_serial#, u.username
, case when current_obj# > 0 then
( select distinct o.owner || '.' || o.object_name || rtrim('.' || o.subobject_name,'.')
from dba_objects o where o.object_id = current_obj# )
end as wait_object
, round(100*(ratio_to_report(count(*)) over())) as percent
, cast(numtodsinterval(count(*),'SECOND') as interval day(0) to second(0)) as elapsed
, sum(ash.delta_read_io_bytes) as read_bytes
, sum(ash.delta_write_io_requests) as write_bytes
, ash.sql_child_number, ash.qc_session_id
from gv$active_session_history ash
join dba_users u on u.user_id = ash.user_id
where 1=1
and ash.sql_id = '&sql_id'
and ash.sql_exec_id is not null
and rownum<10
group by ash.sql_id, ash.sql_exec_start,ash.sql_plan_hash_value, ash.sql_child_number, ash.qc_session_id, ash.session_id, ash.session_serial#, u.username, ash.sql_exec_id, ash.current_obj#
order by ash.sql_exec_start;
-- tab=SQL Stats
set linesize 1000
col PARSING_SCHEMA_NAME for a20
col SQL_TEXT for a70 wrap
col SOURCE for a20
col TOTAL_TIME for a15
col AVG_TIME for a15
col PLSQL_EXEC_TIME for a15
col JAVA_EXEC_TIME for a15
select sql_id
, s.child_number
, s.parsing_schema_name
, rtrim(ltrim(o.owner || '.' || o.object_name || '.', '.') ||
( select regexp_substr(min(ltrim(upper(sp1.text))) keep (dense_rank first order by sp1.line desc),'[^( ]+',1,2)
from dba_source sp1
where sp1.owner = o.owner and sp1.name = o.object_name and sp1.type = o.object_type
and sp1.line < s.program_line#
and regexp_like(ltrim(upper(sp1.text)),'^(PROCEDURE|FUNCTION)\s') )
, '.') as source
, s.program_line# as source_line
, plan_hash_value
, last_active_time
, executions
, parse_calls
, least(100, round(100 * parse_calls/nvl(nullif(executions,0),1),1)) as "Parsed%"
, cast(numtodsinterval(elapsed_time/1E6,'SECOND') as interval day(0) to second(0)) as total_time
, cast(numtodsinterval(elapsed_time / nvl(nullif(executions,0),1) / 1E6,'SECOND') as interval day(0) to second(4)) as avg_time
, round(100 * cpu_time / nullif(elapsed_time,0),1) "CPU%"
, round(100 * user_io_wait_time / nullif(elapsed_time,0),1) "IO%"
, round(100 * concurrency_wait_time / nullif(elapsed_time,0),1) "CONCURRRENCY%"
, round(100 * application_wait_time / nullif(elapsed_time,0),1) "APPLICATION%"
, round(100 * plsql_exec_time / nullif(elapsed_time,0),1) "PL/SQL%"
, buffer_gets buffer_gets_total
, round(buffer_gets / nvl(nullif(executions,0),1)) as buffer_gets_per_exec
, disk_reads
, round(rows_processed / nullif(fetches,0),1) as rows_per_fetch
, round(rows_processed / nvl(nullif(executions,0),1),1) as rows_per_exec
, direct_writes
, rows_processed
, fetches
, end_of_fetch_count
, loads
, s.loaded_versions version_count
, invalidations
, px_servers_executions
, cluster_wait_time
, cast(numtodsinterval(plsql_exec_time/1E6,'SECOND') as interval day(0) to second(0)) as plsql_exec_time
, cast(numtodsinterval(java_exec_time/1E6,'SECOND') as interval day(0) to second(0)) as java_exec_time
, sorts
, sharable_mem
, serializable_aborts
, sql_text
from gv$sql s
left join dba_objects o on o.object_id = s.program_id
where 1=1
--and rownum<10
and sql_id = '&sql_id'
;
-- sql text
set linesize 300 pagesize 300
col SQL_TEXT for a100 wrap
select *
from (
select sql_id,address
, hash_value
, count(*) cnt,
max(decode(piece,0,sql_text))||
max(decode(piece,1,sql_text))||
max(decode(piece,2,sql_text))||
max(decode(piece,3,sql_text))||
max(decode(piece,4,sql_text))||
max(decode(piece,5,sql_text))||
max(decode(piece,6,sql_text))||
max(decode(piece,7,sql_text))||
max(decode(piece,8,sql_text))||
max(decode(piece,9,sql_text))||
max(decode(piece,10,sql_text))||
max(decode(piece,11,sql_text))||
max(decode(piece,12,sql_text))||
max(decode(piece,13,sql_text))||
max(decode(piece,14,sql_text))||
max(decode(piece,15,sql_text))||
max(decode(piece,16,sql_text))||
max(decode(piece,17,sql_text))||
max(decode(piece,18,sql_text))||
max(decode(piece,19,sql_text))||
max(decode(piece,20,sql_text))||
max(decode(piece,21,sql_text))||
max(decode(piece,22,sql_text))||
max(decode(piece,23,sql_text))||
max(decode(piece,24,sql_text) )||
max(decode(piece,25,sql_text))||
max(decode(piece,26,sql_text))||
max(decode(piece,27,sql_text))||
max(decode(piece,28,sql_text))||
max(decode(piece,29,sql_text))||
max(decode(piece,30,sql_text))||
max(decode(piece,31,sql_text))||
max(decode(piece,32,sql_text))||
max(decode(piece,33,sql_text))||
max(decode(piece,34,sql_text))||
max(decode(piece,35,sql_text))||
max(decode(piece,36,sql_text))||
max(decode(piece,37,sql_text))||
max(decode(piece,38,sql_text))||
max(decode(piece,39,sql_text))||
max(decode(piece,40,sql_text)) ||
max(decode(piece,41,sql_text))||
max(decode(piece,42,sql_text))||
max(decode(piece,43,sql_text))||
max(decode(piece,44,sql_text))||
max(decode(piece,45,sql_text))||
max(decode(piece,46,sql_text))||
max(decode(piece,47,sql_text)) ||
max(decode(piece,48,sql_text))||
max(decode(piece,49,sql_text) )||
max(decode(piece,50,sql_text))||
max(decode(piece,51,sql_text))||
max(decode(piece,52,sql_text))||
max(decode(piece,53,sql_text))||
max(decode(piece,54,sql_text))||
max(decode(piece,55,sql_text))||
max(decode(piece,56,sql_text))||
max(decode(piece,57,sql_text))||
max(decode(piece,58,sql_text))||
max(decode(piece,59,sql_text) )||
max(decode(piece,60,sql_text) )||
max(decode(piece,61,sql_text)
--)||
/* max(decode(piece,62,sql_text)
*/
) sql_text
from gv$sqltext
where 1=1
and sql_id='&sql_id'
group by sql_id,address , hash_value
order by 3 desc
)
where rownum = 1
CREATE OR REPLACE FUNCTION GET_CLOB (srcclob in clob ,s in integer, e in integer) RETURN CLOB IS newclob clob ; l_buffer VARCHAR2 (32767); l_amount integer := 24000; dev integer; md integer; BEGIN dev:=floor((e-s+1)/l_amount); md:=mod(e-s+1,l_amount); dbms_output.put_line('DEV:' || dev || ' MOD:' || md); dbms_output.put_line('e:' || e || ' s:' || s); FOR i in 1 .. dev LOOP newclob:=newclob || dbms_lob.substr(srcclob,l_amount,(i-1)*l_amount +s); END LOOP ; newclob:=newclob || dbms_lob.substr(srcclob,md,dev*l_amount+s); RETURN(newclob); END; / -- Select GET_CLOB( <CLOB_COLUMN> , 1 , dbms_lob.getlength(<CLOB_COLUMN>)) from Table_name ; set long 10000 linesize 300 longchunksize 30000 Select sql_id,GET_CLOB( sql_fulltext , 1 , dbms_lob.getlength(sql_fulltext)) sql_text from gv$sql where sql_id in ('c8bj35pys60g5','72y7hnnquc17u','g9m7yw866z4d4','6a82k6u4sbaav'); define sql_id='6a82k6u4sbaav' set long 10000 linesize 300 longchunksize 30000 Select GET_CLOB( sql_fulltext , 1 , dbms_lob.getlength(sql_fulltext)) sql_text from gv$sql where sql_id in ('&sql_id'); drop FUNCTION GET_CLOB ;
from https://asktom.oracle.com/pls/apex/f?p=100:11::::RP:P11_QUESTION_ID:9539494200346328435
set pagesize 0
with data as
( select sql_fulltext, dbms_lob.getlength(sql_fulltext) len from gv$sql
where 1=1
--and sql_text like '%ayyyyyyyyyyyyyyyyyyyy%'
and sql_id='c93a0ckz9rh38'
)
select
dbms_lob.substr(sql_fulltext,4000,1) piece1,
case when len > 4000 then dbms_lob.substr(sql_fulltext,4000,4001) end piece2,
case when len > 8000 then dbms_lob.substr(sql_fulltext,4000,8001) end piece3,
case when len > 12000 then dbms_lob.substr(sql_fulltext,4000,12001) end piece4,
case when len > 16000 then dbms_lob.substr(sql_fulltext,4000,165001) end piece5
from data
;
set pagesize 80
-- search text
set pagesize 0
with data as
( select sql_fulltext, dbms_lob.getlength(sql_fulltext) len from gv$sql
where 1=1
--and sql_id='c93a0ckz9rh38'
and ( sql_text like '%I_OBJ#%' or sql_text like '%I_OBJ#%' )
and sql_text not like '%select sql_id, sql_text from gv$sql%'
)
select
dbms_lob.substr(sql_fulltext,4000,1) piece1,
case when len > 4000 then dbms_lob.substr(sql_fulltext,4000,4001) end piece2,
case when len > 8000 then dbms_lob.substr(sql_fulltext,4000,8001) end piece3,
case when len > 12000 then dbms_lob.substr(sql_fulltext,4000,12001) end piece4,
case when len > 16000 then dbms_lob.substr(sql_fulltext,4000,165001) end piece5
from data
;
set pagesize 80
====with sql _id set pagesize 0 with data as ( select sql_id,sql_fulltext, dbms_lob.getlength(sql_fulltext) len from gv$sql where 1=1 --and sql_id='c93a0ckz9rh38' and ( sql_text like '%MERGE INTO %' or sql_text like '%MERGE INTO %' ) and sql_text not like '%select sql_id, sql_text from gv$sql%' ) select sql_id, dbms_lob.substr(sql_fulltext,4000,1) piece1, case when len > 4000 then dbms_lob.substr(sql_fulltext,4000,4001) end piece2, case when len > 8000 then dbms_lob.substr(sql_fulltext,4000,8001) end piece3, case when len > 12000 then dbms_lob.substr(sql_fulltext,4000,12001) end piece4, case when len > 16000 then dbms_lob.substr(sql_fulltext,4000,165001) end piece5 from data ; set pagesize 80
set lines 400
undef sql_id
define sql_id='9zg9qd9bm4spu'
col apwpx format 99999.999 head 'AppWms|PerX'
col bgpx format 999999999 head 'BGets|PerX'
col conw format 99999.999 head 'Conwms|PerX'
col cpx format 99999999.999 head 'CPUms|PerX'
col drpx format 9999999.99 head 'DReads|PerX'
col fetchx format 99999 head 'Fetchs|PerX'
col sortx format 999 head 'Sorts|PerX'
col elpx format 99999999.999 head 'Elapms|PerX'
col exec format 999999999999 head 'Execs'
col iowpx format 99999999.999 head 'IOWms|PerX'
col latime format a11 head 'Last Active'
col lltime format a14 head 'Last Load'
col maxsnapid format 999999 head 'Max|SnapId'
col m format a01 trunc
col minsnapid format 999999 head 'Min|SnapId'
col module format a14 trunc head 'Module'
col o format a01 head 'O' trunc
col opt_mode format a08 trunc head 'Opt|Mode' trunc
col parse_usr format a08 head 'ParsUser' trunc
col phash format 9999999999 head 'PlanHash'
col phashp format a12 head 'PlanHash P'
col rwpx format 999999.99 head 'RwsP|PerX'
col s_cn format a07 head 'S:Child' trunc
col sql_id format a15 head 'SQL Id'
col sqltext format a12 trunc head 'Sql Text'
col ue format 999
col cpct format 999 head 'CPU|Pct' trunc
col ipct format 999 head 'IO|Pct' trunc
col btime format a11 head 'Begin Time'
col smem format 99999 head 'ShrMem|KB'
-- profile/baseline associated with sql_id
col name format a26
col category format a04 trunc
col created format a14
col type format a03 trunc
col status format a03 trunc
col sql_text format a80 trunc
col min_ago format 9999 trunc head 'Mins|Ago'
select parsing_schema_name parse_usr
,sql_id
,con_id
,lpad(plan_hash_value,10,' ')||' '||
case when sql_profile is not null then 'P'
when sql_plan_baseline is not null then 'B'
else ' '
end phashp
--,is_bind_sensitive||is_bind_aware||is_shareable pas
--,optimizer_mode m
--,is_obsolete o
,users_executing ue
,substr(object_status,1,1)||':'||lpad(child_number,5,' ') s_cn
,to_char(to_date(last_load_time,'YYYY-MM-DD/HH24:MI:SS'),'MMDD HH24MI') lltime
,to_char(last_active_time,'MMDD HH24:MI') latime
,(sysdate-last_active_time)*1440 min_ago
,decode(executions,0,1,executions) exec
,cpu_time/decode(executions,0,1,executions)/1000 cpx
,elapsed_time/decode(executions,0,1,executions)/1000 elpx
,(cpu_time/elapsed_time)*100 cpct
,buffer_gets/decode(executions,0,1,executions) bgpx
,user_io_wait_time/decode(executions,0,1,executions)/1000 iowpx
,(user_io_wait_time/elapsed_time)*100 ipct
,disk_reads/decode(executions,0,1,executions) drpx
,application_wait_time/decode(executions,0,1,executions)/1000 apwpx
,concurrency_wait_time/decode(executions,0,1,executions)/1000 conw
,rows_processed/decode(executions,0,1,executions) rwpx
,sorts/decode(executions,0,1,executions) sortx
,fetches/decode(executions,0,1,executions) fetchx
,module module
,replace(sql_text,chr(30)) sqltext
from gv$sql
where sql_id = '&&sql_id'
and last_active_time > sysdate-7
order by parsing_schema_name
,last_active_time
;
-- tab=Object stats
col OBJECT_OWNER for a20
col OBJECT_TYPE for a20
col PARTITION_START for a20
col OBJECT_NAME for a25
col OPERATION for a25
col PARTITION_STOP for a25
select * from (
with plan_objects as
( select --+ materialize
p.object_owner
, p.object_name
, p.object_type
, p.partition_start
, p.partition_stop
, p.cardinality
, p.operation
, p.options
, count(*) as occurs_in_plan
from gv$sql_plan_statistics_all p
where 1=1
and p.sql_id = '&sql_id'
and p.plan_hash_value =
( select plan_hash_value from
( select plan_hash_value, row_number() over (order by timestamp desc) as seq
from gv$sql_plan p
where 1=1
and p.sql_id = '&sql_id'
and p.inst_id = 1 )
where seq = 1 )
and p.object_type != 'VIEW'
group by p.object_owner, p.object_name, p.object_type, p.partition_start, p.partition_stop, p.cardinality, p.operation, p.options )
, object_stats as
( select ts.owner as object_owner
, ts.table_name as object_name
, ts.table_name as display_name
, ts.num_rows
, ts.blocks
, ts.last_analyzed
, ts.stale_stats
from dba_tab_statistics ts
where (ts.owner, ts.table_name) in
(select object_owner, object_name from plan_objects where object_type like 'TABLE%')
and ts.partition_name is null
union
select xs.owner
, xs.index_name
, '(' || xs.table_name || ') ' || index_name as display_name
, xs.num_rows
, xs.leaf_blocks as blocks
, xs.last_analyzed
, xs.stale_stats
from dba_ind_statistics xs
where (xs.owner, xs.index_name) in
(select object_owner, object_name from plan_objects where object_type like 'INDEX%')
and xs.partition_name is null
)
select --+ dynamic_sampling(8)
object_owner
, o.object_type
, nvl(s.display_name,object_name) as object_name
, s.stale_stats as "Stale?"
-- , o.occurs_in_plan
, o.operation || ' ' || o.options as operation
, o.cardinality
, s.num_rows as "Rows (global)"
, s.blocks
, s.last_analyzed
, o.partition_start
, o.partition_stop
from plan_objects o
left join object_stats s using(object_owner, object_name)
order by
case object_owner when 'SYS' then 2 else 1 end
, object_owner
, ltrim(object_name,'(')
);
===
set linesize 300
col SOURCE for a30
col BEGIN_TIME for a25
-- define sql_id='4wrkq5qmp8004'
SELECT *
FROM (SELECT '1.v$sql'||'Instance number:'||GV$SQL.inst_id source, SQL_ID,
plan_hash_value, TO_CHAR (FIRST_LOAD_TIME) begin_time, ' In the cursor cache' end_time, executions "No. of exec", (buffer_gets / executions) "LIO/exec", (cpu_time / executions / 1000000) "CPUTIM/exec",
(elapsed_time / executions / 1000000) "ETIME/exec ", (disk_reads / executions) "PIO/exec", (ROWS_PROCESSED / executions) "ROWs/exec"
FROM Gv$SQL
WHERE sql_id ='&sql_id'
UNION ALL
SELECT '2.sqltuning set' source, sql_id, plan_hash_value, 'JUST SQLSET NO DATE' begin_time, 'JUST SQLSET NO DATE' end_time,
EXECUTIONS "No. of exec", (buffer_gets / executions) "LIO/exec", (cpu_time / executions / 1000000) "CPUTIM/exec",
(elapsed_time / executions / 1000000) "ETIME/exec", (disk_reads / executions) "PIO/exec",
(ROWS_PROCESSED / executions) "ROWs/exec"
FROM dba_sqlset_statements
WHERE SQL_ID ='&sql_id'
UNION ALL
SELECT '3.dba_advisor_sqlstats' source, sql_id,
plan_hash_value,
'JUST SQLSET NO DATE' begin_time,
'JUST SQLSET NO DATE' end_time,
EXECUTIONS "No. of exec",
(buffer_gets / executions) "LIO/exec",
(cpu_time / executions / 1000000) "CPUTIM/exec",
(elapsed_time / executions / 1000000) "ETIME/exec",
(disk_reads / executions) "PIO/exec",
(ROWS_PROCESSED / executions) "ROWs/exec"
FROM dba_sqlset_statements
WHERE SQL_ID = '&sql_id'
UNION ALL
SELECT DISTINCT '4.dba_hist_sqlstat' ||'Instance number:' || SQL.INSTANCE_NUMBER source,
sql_id,
PLAN_HASH_VALUE,
TO_CHAR (s.BEGIN_INTERVAL_TIME ,'YYYY-MM-DD hh24:mi:ss') begin_time,
TO_CHAR (s. END_INTERVAL_TIME,'YYYY-MM-DD hh24:mi:ss') end_time,
SQL.executions_delta,
SQL.buffer_gets_delta / DECODE (NVL (SQL.executions_delta, 0), 0, 1, SQL.executions_delta) "LIO/exec",
(SQL.cpu_time_delta / 1000000) / DECODE (NVL (SQL.executions_delta, 0), 0, 1, SQL.executions_delta) "CPUTIM/exec",
(SQL.elapsed_time_delta / 1000000) / DECODE (NVL (SQL.executions_delta, 0), 0, 1, SQL .executions_delta) "ETIME/exec",
SQL.DISK_READS_DELTA / DECODE (NVL (SQL.executions_delta, 0), 0, 1, SQL.executions_delta) "PIO/exec",
SQL.ROWS_PROCESSED_DELTA / DECODE (NVL (SQL.executions_delta, 0), 0, 1, SQL.executions_delta) "ROWs/exec"
FROM dba_hist_sqlstat SQL, dba_hist_snapshot s
WHERE SQL.INSTANCE_NUMBER = s.INSTANCE_NUMBER
--AND SQL.dbid FROM v$database)
AND s.snap_id = SQL.snap_id
AND sql_id IN ('&sql_id')
)
ORDER BY source, begin_time DESC;
SOURCE SQL_ID PLAN_HASH_VALUE BEGIN_TIME END_TIME No. of exec LIO/exec CPUTIM/exec ETIME/exec PIO/exec ROWs/exec
------------------------------ ------------- --------------- ------------------------- -------------------- ----------- ---------- ----------- ----------- ---------- ----------
1.v$sqlInstance number:1 4wrkq5qmp8004 873175999 2021-09-12/11:10:05 In the cursor cache 1 14 .002351 .001692 0 29
===
SET TERMOUT OFF pagesize 5000 tab off verify off linesize 999 trimspool on trimout on null ""
SET TERMOUT ON
COL exec_per_sec FOR 99999990
COL ela_ms_per_sec FOR 99999990
COL rows_per_sec FOR 99999990
COL lios_per_sec FOR 99999990
COL blkrd_per_sec FOR 99999990
COL cpu_ms_per_sec FOR 99999990
COL iow_ms_per_sec FOR 99999990
COL clw_ms_per_sec FOR 99999990
COL apw_ms_per_sec FOR 99999990
COL ccw_ms_per_sec FOR 99999990
SELECT
CAST(begin_interval_time AS DATE) begin_interval_time
, sql_id
, plan_hash_value
, ROUND(SUM(executions_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) exec_per_sec
, ROUND(SUM(elapsed_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ela_ms_per_sec
, ROUND(SUM(rows_processed_delta) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) rows_per_sec
, ROUND(SUM(buffer_gets_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) lios_per_sec
, ROUND(SUM(disk_reads_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) blkrd_per_sec
, ROUND(SUM(cpu_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) cpu_ms_per_sec
, ROUND(SUM(iowait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) iow_ms_per_sec
, ROUND(SUM(clwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) clw_ms_per_sec
, ROUND(SUM(apwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) apw_ms_per_sec
, ROUND(SUM(ccwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ccw_ms_per_sec
FROM
dba_hist_snapshot sn
, dba_hist_sqlstat st
WHERE
sn.snap_id = st.snap_id
AND sn.dbid = st.dbid
AND sn.instance_number = st.instance_number
AND sql_id = '&sql_id'
--AND plan_hash_value LIKE '2'
--AND begin_interval_time >= 3
--AND end_interval_time <= 4
-- AND begin_interval_time > sysdate -1
GROUP BY
CAST(begin_interval_time AS DATE)
, CAST(end_interval_time AS DATE)
, sql_id
, plan_hash_value
ORDER BY
begin_interval_time
, sql_id
, plan_hash_value
/
OR
COL exec_per_sec FOR 99999990
COL ela_ms_per_sec FOR 99999990
COL rows_per_sec FOR 99999990
COL lios_per_sec FOR 99999990
COL blkrd_per_sec FOR 99999990
COL cpu_ms_per_sec FOR 99999990
COL iow_ms_per_sec FOR 99999990
COL clw_ms_per_sec FOR 99999990
COL apw_ms_per_sec FOR 99999990
COL ccw_ms_per_sec FOR 99999990
SELECT
CAST(begin_interval_time AS DATE) begin_interval_time
, sql_id
, plan_hash_value
, ROUND(SUM(executions_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) exec_per_sec
, ROUND(SUM(elapsed_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ela_ms_per_sec
, ROUND(SUM(rows_processed_delta) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) rows_per_sec
, ROUND(SUM(buffer_gets_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) lios_per_sec
, ROUND(SUM(disk_reads_delta ) / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) blkrd_per_sec
, ROUND(SUM(cpu_time_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) cpu_ms_per_sec
, ROUND(SUM(iowait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) iow_ms_per_sec
, ROUND(SUM(clwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) clw_ms_per_sec
, ROUND(SUM(apwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) apw_ms_per_sec
, ROUND(SUM(ccwait_delta ) / 1000 / ((CAST(end_interval_time AS DATE) - CAST(begin_interval_time AS DATE)) * 86400), 1) ccw_ms_per_sec
FROM
dba_hist_snapshot sn
, dba_hist_sqlstat st
WHERE
sn.snap_id = st.snap_id
AND sn.dbid = st.dbid
AND sn.instance_number = st.instance_number
AND sql_id = '&sql_id'
--AND plan_hash_value LIKE '2'
AND begin_interval_time >= sysdate -1
--AND end_interval_time <= 4
GROUP BY
CAST(begin_interval_time AS DATE)
, CAST(end_interval_time AS DATE)
, sql_id
, plan_hash_value
ORDER BY
begin_interval_time
, sql_id
, plan_hash_value
/
==============
https://github.com/iusoltsev/sqlplus/blob/master/ash_plsqlmon.sql
define 1='2p9fv35c7zxtg'
set feedback on heading on timi off pages 500 lines 500 echo off VERIFY OFF
col PLAN_OPERATION for a180
col WAIT_PROFILE for a200
col SQL_TEXT for a80
col MIN_TIME for a8
col MAX_TIME for a8
PROMPT
PROMPT ***** Summary by SQL execs *****
with hash as (select /*+ INLINE*/ * from gv$active_session_history where (sql_id = '&&1' or top_level_sql_id = '&&1'))
, ash as (
select count(distinct sh.session_id||sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
decode(session_state,'WAITING',event,session_state) as EVENT,
count(*) as WAIT_COUNT,
count( distinct SQL_EXEC_ID) as EXECS,
MIN(SAMPLE_TIME) as MIN_SAMPLE_TIME,
max(SAMPLE_TIME) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id, nvl(sql_plan_hash_value, 0), decode(session_state,'WAITING',event,session_state),PLSQL_ENTRY_OBJECT_ID,PLSQL_ENTRY_SUBPROGRAM_ID)
select sql_id,
sql_plan_hash_value,
sum(WAIT_COUNT) as ASH_ROWS,
max(EXECS) as EXECS,
to_char(min(min_sample_time),'hh24:mi:ss') as MIN_TIME,
to_char(max(max_sample_time),'hh24:mi:ss') as MAX_TIME,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as sql_text,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE
from ash left join dba_hist_sqltext using (sql_id)
group by sql_id,
sql_plan_hash_value,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))
order by sum(WAIT_COUNT) desc
/
SQL_ID SQL_PLAN_HASH_VALUE ASH_ROWS EXECS MIN_TIME MAX_TIME SQL_TEXT WAIT_PROFILE
------------- ------------------- ---------- ---------- -------- -------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2p9fv35c7zxtg 2367040129 11 5 18:19:49 07:05:57 select /* KSXM:LOAD_DML_INF *//*+ leading(o) index(m) use_nl(m) */ nv ON CPU(6); ON CPU(5)
1 row selected.
set linesize 300
col PLAN_OPERATION for a50
col OBJECT_OWNER for a20
col WAIT_PROFILE for a20
col OBJECT_NAME for a20
col QBLOCK_NAME for a20
PROMPT
PROMPT
PROMPT ***** SQL Plan/PLSQL execs details *****
with
hash as (select /*+ INLINE*/ * from gv$active_session_history where (sql_id = '&&1' or top_level_sql_id = '&&1')),
ash as
(select count(distinct sh.session_id || sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
nvl(sql_plan_line_id, 0) as SQL_PLAN_LINE_ID,
decode(session_state, 'WAITING', event, session_state) as EVENT,
count(*) as WAIT_COUNT,
count(distinct SQL_EXEC_ID) as EXECS,
min(sample_time) as MIN_SAMPLE_TIME,
max(sample_time) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id,
nvl(sql_plan_hash_value, 0),
nvl(sql_plan_line_id, 0),
decode(session_state, 'WAITING', event, session_state),
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
ash_stat as
( -- all SQL exec stats
select sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sum(WAIT_COUNT) as ASH_ROWS,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE,
max(EXECS) as EXECS,
max(MAX_SAMPLE_TIME) as MAX_SAMPLE_TIME
from ash
group by sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
pt as -- Plan Tables for all excuted SQLs (direct+recursive)
(select sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from dba_hist_sql_plan
where (sql_id, plan_hash_value) in
(select sql_id, sql_plan_hash_value from ash)
union all -- for plans not in dba_hist_sql_plan yet
select distinct sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from gv$sql_plan
where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)
and not exists
(select 1 from dba_hist_sql_plan where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)))
SELECT -- standard recursive SQLs
decode(pt.id, 0, 'SQL Query', null) as SQL_PLSQL,
decode(pt.id, 0, pt.sql_id, null) as SQL_ID,
decode(pt.id, 0, pt.plan_hash_value, null) as PLAN_HASH_VALUE,
pt.id,
lpad(' ', 2 * level) || pt.operation || ' ' || pt.options as PLAN_OPERATION,
pt.object_owner,
pt.object_name,
pt.qblock_name,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
FROM pt
left join ash_stat
on pt.id = NVL(ash_stat.sql_plan_line_id, 0)
and pt.sql_id = ash_stat.sql_id
and pt.plan_hash_value = ash_stat.sql_plan_hash_value
where pt.sql_id in (select sql_id from ash_stat)
CONNECT BY PRIOR pt.id = pt.parent_id
and PRIOR pt.sql_id = pt.sql_id
and PRIOR pt.plan_hash_value = pt.plan_hash_value
START WITH pt.id = 0
UNION ALL
select 'PL/SQL' as SQL_PLSQL, -- non-identified by SQL or PLSQL exec stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
nvl2(p.object_name, p.owner||'.'||p.object_name||'.'||p.procedure_name||'"', trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
left join dba_procedures p on ash_stat.PLSQL_ENTRY_OBJECT_ID = p.object_id and ash_stat.PLSQL_ENTRY_SUBPROGRAM_ID = p.subprogram_id
where sql_id is null
or (sql_plan_hash_value = 0 and sql_id not in (select sql_id from pt))
UNION ALL
select 'SQL w/o plan' as SQL_PLSQL, -- SQL with non-identified plan stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
where sql_id not in (select sql_id from pt)
and sql_id is not null
and sql_plan_hash_value != 0
/
set VERIFY ON timi on
SQL_PLSQL SQL_ID PLAN_HASH_VALUE ID PLAN_OPERATION OBJECT_OWNER OBJECT_NAME QBLOCK_NAME EXECS ASH_ROWS WAIT_PROFILE
------------ ------------- --------------- ---------- -------------------------------------------------- -------------------- -------------------- -------------------- ---------- ---------- --------------------
SQL Query 2p9fv35c7zxtg 2367040129 0 SELECT STATEMENT 2 4 ON CPU(4)
1 COUNT STOPKEY SEL$1
2 NESTED LOOPS OUTER
3 VIEW SET$1 1 1 ON CPU(1)
4 UNION-ALL SET$1 1 1 ON CPU(1)
5 TABLE ACCESS CLUSTER SYS TAB$ SEL$2
6 INDEX UNIQUE SCAN SYS I_OBJ# SEL$2
7 TABLE ACCESS BY INDEX ROWID SYS TABPART$ SEL$3 1 1 ON CPU(1)
8 INDEX UNIQUE SCAN SYS I_TABPART_OBJ$ SEL$3
9 TABLE ACCESS BY INDEX ROWID SYS TABCOMPART$ SEL$4
10 INDEX UNIQUE SCAN SYS I_TABCOMPART$ SEL$4
11 TABLE ACCESS BY INDEX ROWID SYS TABSUBPART$ SEL$5
12 INDEX UNIQUE SCAN SYS I_TABSUBPART$_OBJ$ SEL$5
13 TABLE ACCESS BY INDEX ROWID SYS MON_MODS_ALL$ SEL$1
14 INDEX UNIQUE SCAN SYS I_MON_MODS_ALL$_OBJ SEL$1
SQL Query 2p9fv35c7zxtg 2367040129 0 SELECT STATEMENT 4 4 ON CPU(4)
1 COUNT STOPKEY SEL$1
2 NESTED LOOPS OUTER
3 VIEW SET$1 1 1 ON CPU(1)
4 UNION-ALL SET$1 1 1 ON CPU(1)
5 TABLE ACCESS CLUSTER SYS TAB$ SEL$2
6 INDEX UNIQUE SCAN SYS I_OBJ# SEL$2
7 TABLE ACCESS BY INDEX ROWID SYS TABPART$ SEL$3 1 1 ON CPU(1)
8 INDEX UNIQUE SCAN SYS I_TABPART_OBJ$ SEL$3
9 TABLE ACCESS BY INDEX ROWID SYS TABCOMPART$ SEL$4
10 INDEX UNIQUE SCAN SYS I_TABCOMPART$ SEL$4
11 TABLE ACCESS BY INDEX ROWID SYS TABSUBPART$ SEL$5
12 INDEX UNIQUE SCAN SYS I_TABSUBPART$_OBJ$ SEL$5
13 TABLE ACCESS BY INDEX ROWID SYS MON_MODS_ALL$ SEL$1
14 INDEX UNIQUE SCAN SYS I_MON_MODS_ALL$_OBJ SEL$1
30 rows selected.
====
set linesize 300 pagesize 300
VARIABLE snap_start NUMBER
VARIABLE snap_end NUMBER
VARIABLE dbid NUMBER
VARIABLE INST_NUMBER number
exec select max(snap_id) -24 into :snap_start from dba_hist_snapshot ;
exec select max(snap_id) into :snap_end from dba_hist_snapshot ;
exec select DBID into :dbid from v$database;
define 1 ='8cnh50qfgwg73'
define 2=40199
define 3=''
set feedback on heading on timi off pages 500 lines 500 echo off VERIFY OFF
col PLAN_OPERATION for a180
col WAIT_PROFILE for a200
col SQL_TEXT for a80
col MIN_TIME for a8
col MAX_TIME for a8
PROMPT
PROMPT ***** Summary by SQL execs *****
with hash as (select /*+ INLINE*/ * from dba_hist_active_sess_history
where 1=1
-- and (sql_id = '&&1' or top_level_sql_id = '&&1')
and (:snap_start is null OR snap_id between :snap_start and nvl(:snap_end, :snap_start))
--and ('&&2' is null OR snap_id between '&&2' and nvl('&&3', '&&2'))
-- and sql_id='&&1'
)
, ash as (
select count(distinct sh.session_id||sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
decode(session_state,'WAITING',event,session_state) as EVENT,
count(*) as WAIT_COUNT,
count( distinct SQL_EXEC_ID) as EXECS,
MIN(SAMPLE_TIME) as MIN_SAMPLE_TIME,
max(SAMPLE_TIME) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id, nvl(sql_plan_hash_value, 0), decode(session_state,'WAITING',event,session_state),PLSQL_ENTRY_OBJECT_ID,PLSQL_ENTRY_SUBPROGRAM_ID)
select sql_id,
sql_plan_hash_value,
sum(WAIT_COUNT) as ASH_ROWS,
max(EXECS) as EXECS,
to_char(min(min_sample_time),'hh24:mi:ss') as MIN_TIME,
to_char(max(max_sample_time),'hh24:mi:ss') as MAX_TIME,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as sql_text,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE
from ash left join dba_hist_sqltext using (sql_id)
group by sql_id,
sql_plan_hash_value,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))
order by sum(WAIT_COUNT) desc
/
SQL_ID SQL_PLAN_HASH_VALUE ASH_ROWS EXECS MIN_TIME MAX_TIME SQL_TEXT WAIT_PROFILE
------------- ------------------- ---------- ---------- -------- -------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8cnh50qfgwg73 3673574621 681 7 21:00:51 21:59:54 SELECT NVL(SUM(BYTES),0) FROM SYS.DBA_FREE_SPACE WHERE TABLESPACE_NAME = :B1 gc buffer busy acquire(110); db file sequential read(110); gc buffer busy acquire(110); db file sequential read(108); gc cr disk read(44); read by other session(44); gc cr disk read(43); ON CPU(39); O
1 row selected.
set linesize 300
col PLAN_OPERATION for a50
col OBJECT_OWNER for a20
col WAIT_PROFILE for a20
col OBJECT_NAME for a20
col QBLOCK_NAME for a20
define 1 ='8cnh50qfgwg73'
with
hash as (select /*+ INLINE*/ * from dba_hist_active_sess_history
where 1=1
and (sql_id = '&&1' or top_level_sql_id = '&&1') and ('&&2' is null OR snap_id between '&&2' and nvl('&&3', '&&2'))
-- and (:snap_start is null OR snap_id between :snap_start and nvl(:snap_end, :snap_start))
),
ash as
(select count(distinct sh.session_id || sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
nvl(sql_plan_line_id, 0) as SQL_PLAN_LINE_ID,
decode(session_state, 'WAITING', event, session_state) as EVENT,
count(*) as WAIT_COUNT,
count(distinct SQL_EXEC_ID) as EXECS,
min(sample_time) as MIN_SAMPLE_TIME,
max(sample_time) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id,
nvl(sql_plan_hash_value, 0),
nvl(sql_plan_line_id, 0),
decode(session_state, 'WAITING', event, session_state),
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
ash_stat as
( -- all SQL exec stats
select sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sum(WAIT_COUNT) as ASH_ROWS,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE,
max(EXECS) as EXECS,
max(MAX_SAMPLE_TIME) as MAX_SAMPLE_TIME
from ash
group by sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
pt as -- Plan Tables for all excuted SQLs (direct+recursive)
(select sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from dba_hist_sql_plan
where (sql_id, plan_hash_value) in
(select sql_id, sql_plan_hash_value from ash)
union all -- for plans not in dba_hist_sql_plan yet
select distinct sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from gv$sql_plan
where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)
and not exists
(select 1 from dba_hist_sql_plan where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)))
SELECT -- standard recursive SQLs
decode(pt.id, 0, 'SQL Query', null) as SQL_PLSQL,
decode(pt.id, 0, pt.sql_id, null) as SQL_ID,
decode(pt.id, 0, pt.plan_hash_value, null) as PLAN_HASH_VALUE,
pt.id,
lpad(' ', 2 * level) || pt.operation || ' ' || pt.options as PLAN_OPERATION,
pt.object_owner,
pt.object_name,
pt.qblock_name,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
FROM pt
left join ash_stat
on pt.id = NVL(ash_stat.sql_plan_line_id, 0)
and pt.sql_id = ash_stat.sql_id
and pt.plan_hash_value = ash_stat.sql_plan_hash_value
where pt.sql_id in (select sql_id from ash_stat)
CONNECT BY PRIOR pt.id = pt.parent_id
and PRIOR pt.sql_id = pt.sql_id
and PRIOR pt.plan_hash_value = pt.plan_hash_value
START WITH pt.id = 0
UNION ALL
select 'PL/SQL' as SQL_PLSQL, -- non-identified by SQL or PLSQL exec stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
nvl2(p.object_name, p.owner||'.'||p.object_name||'.'||p.procedure_name||'"', trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
left join dba_procedures p on ash_stat.PLSQL_ENTRY_OBJECT_ID = p.object_id and ash_stat.PLSQL_ENTRY_SUBPROGRAM_ID = p.subprogram_id
where sql_id is null
or (sql_plan_hash_value = 0 and sql_id not in (select sql_id from pt))
UNION ALL
select 'SQL w/o plan' as SQL_PLSQL, -- SQL with non-identified plan stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
where sql_id not in (select sql_id from pt)
and sql_id is not null
and sql_plan_hash_value != 0
/
set VERIFY ON timi on
SQL_PLSQL SQL_ID PLAN_HASH_VALUE ID PLAN_OPERATION OBJECT_OWNER OBJECT_NAME QBLOCK_NAME EXECS ASH_ROWS WAIT_PROFILE
------------ ------------- --------------- ---------- -------------------------------------------------- -------------------- -------------------- -------------------- ---------- ---------- --------------------
SQL Query 8cnh50qfgwg73 3673574621 0 SELECT STATEMENT
1 SORT AGGREGATE SEL$1
2 VIEW SYS DBA_FREE_SPACE SET$1
3 UNION-ALL SET$1
4 NESTED LOOPS SEL$2
5 NESTED LOOPS
6 TABLE ACCESS BY INDEX ROWID SYS TS$ SEL$2
7 INDEX UNIQUE SCAN SYS I_TS1 SEL$2
8 TABLE ACCESS CLUSTER SYS FET$ SEL$2
9 INDEX UNIQUE SCAN SYS I_TS# SEL$2
10 INDEX UNIQUE SCAN SYS I_FILE2 SEL$2
11 NESTED LOOPS SEL$3
12 NESTED LOOPS
13 TABLE ACCESS BY INDEX ROWID SYS TS$ SEL$3
14 INDEX UNIQUE SCAN SYS I_TS1 SEL$3
15 FIXED TABLE FIXED INDEX SYS X$KTFBFE (ind:1) SEL$3
16 INDEX UNIQUE SCAN SYS I_FILE2 SEL$3
17 NESTED LOOPS SEL$4
18 HASH JOIN
19 NESTED LOOPS
20 STATISTICS COLLECTOR
21 NESTED LOOPS
====
define 1='8cnh50qfgwg73'
col PLAN_OPERATION for a40
col QBLOCK_NAME for a20
col OBJECT_NAME for a20
col OBJECT_OWNER for a20
PROMPT
PROMPT
PROMPT ***** SQL Plan/PLSQL execs details *****
with
hash as (select /*+ INLINE*/ * from dba_hist_active_sess_history
where 1=1
and sql_id = '&&1'
-- and (sql_id = '&&1' or top_level_sql_id = '&&1') and ('&&2' is null OR snap_id between '&&2' and nvl('&&3', '&&2'))
),
ash as
(select count(distinct sh.session_id || sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
nvl(sql_plan_line_id, 0) as SQL_PLAN_LINE_ID,
decode(session_state, 'WAITING', event, session_state) as EVENT,
count(*) as WAIT_COUNT,
count(distinct SQL_EXEC_ID) as EXECS,
min(sample_time) as MIN_SAMPLE_TIME,
max(sample_time) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id,
nvl(sql_plan_hash_value, 0),
nvl(sql_plan_line_id, 0),
decode(session_state, 'WAITING', event, session_state),
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
ash_stat as
( -- all SQL exec stats
select sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sum(WAIT_COUNT) as ASH_ROWS,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE,
max(EXECS) as EXECS,
max(MAX_SAMPLE_TIME) as MAX_SAMPLE_TIME
from ash
group by sql_id,
sql_plan_hash_value,
sql_plan_line_id,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID),
pt as -- Plan Tables for all excuted SQLs (direct+recursive)
(select sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from dba_hist_sql_plan
where (sql_id, plan_hash_value) in
(select sql_id, sql_plan_hash_value from ash)
union all -- for plans not in dba_hist_sql_plan yet
select distinct sql_id,
plan_hash_value,
id,
operation,
options,
object_owner,
object_name,
qblock_name,
nvl(parent_id, -1) as parent_id
from gv$sql_plan
where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)
and not exists
(select 1 from dba_hist_sql_plan where (sql_id, plan_hash_value) in (select sql_id, sql_plan_hash_value from ash)))
SELECT -- standard recursive SQLs
decode(pt.id, 0, 'SQL Query', null) as SQL_PLSQL,
decode(pt.id, 0, pt.sql_id, null) as SQL_ID,
decode(pt.id, 0, pt.plan_hash_value, null) as PLAN_HASH_VALUE,
pt.id,
lpad(' ', 2 * level) || pt.operation || ' ' || pt.options as PLAN_OPERATION,
pt.object_owner,
pt.object_name,
pt.qblock_name,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
FROM pt
left join ash_stat
on pt.id = NVL(ash_stat.sql_plan_line_id, 0)
and pt.sql_id = ash_stat.sql_id
and pt.plan_hash_value = ash_stat.sql_plan_hash_value
where pt.sql_id in (select sql_id from ash_stat)
CONNECT BY PRIOR pt.id = pt.parent_id
and PRIOR pt.sql_id = pt.sql_id
and PRIOR pt.plan_hash_value = pt.plan_hash_value
START WITH pt.id = 0
UNION ALL
select 'PL/SQL' as SQL_PLSQL, -- non-identified by SQL or PLSQL exec stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
nvl2(p.object_name, p.owner||'.'||p.object_name||'.'||p.procedure_name||'"', trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
left join dba_procedures p on ash_stat.PLSQL_ENTRY_OBJECT_ID = p.object_id and ash_stat.PLSQL_ENTRY_SUBPROGRAM_ID = p.subprogram_id
where sql_id is null
or (sql_plan_hash_value = 0 and sql_id not in (select sql_id from pt))
UNION ALL
select 'SQL w/o plan' as SQL_PLSQL, -- SQL with non-identified plan stats
sql_id,
ash_stat.sql_plan_hash_value as plan_hash_value,
ash_stat.sql_plan_line_id,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as PLAN_OPERATION,
null,
null,
null,
ash_stat.EXECS,
ash_stat.ASH_ROWS,
ash_stat.WAIT_PROFILE
from ash_stat left join dba_hist_sqltext using (sql_id)
where sql_id not in (select sql_id from pt)
and sql_id is not null
and sql_plan_hash_value != 0
/
set VERIFY ON timi on
=====
define 1='22rh3t38yfuxg'
set feedback on heading on timi off pages 500 lines 500 echo off VERIFY OFF
col PLAN_OPERATION for a180
col WAIT_PROFILE for a30
col SQL_TEXT for a80
col MIN_TIME for a8
col MAX_TIME for a8
PROMPT
PROMPT ***** Summary by SQL execs *****
with hash as (select /*+ INLINE*/ * from dba_hist_active_sess_history
where 1=1
and sql_id = '&1'
-- and (sql_id = '&&1' or top_level_sql_id = '&&1')
-- and ('&&2' is null OR snap_id between '&&2' and nvl('&&3', '&&2'))
)
, ash as (
select count(distinct sh.session_id||sh.session_serial#) as SID_COUNT,
PLSQL_ENTRY_OBJECT_ID,
PLSQL_ENTRY_SUBPROGRAM_ID,
sh.SQL_ID,
nvl(sql_plan_hash_value, 0) as SQL_PLAN_HASH_VALUE,
decode(session_state,'WAITING',event,session_state) as EVENT,
count(*) as WAIT_COUNT,
count( distinct SQL_EXEC_ID) as EXECS,
MIN(SAMPLE_TIME) as MIN_SAMPLE_TIME,
max(SAMPLE_TIME) as MAX_SAMPLE_TIME
from hash sh
group by sh.sql_id, nvl(sql_plan_hash_value, 0), decode(session_state,'WAITING',event,session_state),PLSQL_ENTRY_OBJECT_ID,PLSQL_ENTRY_SUBPROGRAM_ID)
select sql_id,
sql_plan_hash_value,
sum(WAIT_COUNT) as ASH_ROWS,
max(EXECS) as EXECS,
to_char(min(min_sample_time),'hh24:mi:ss') as MIN_TIME,
to_char(max(max_sample_time),'hh24:mi:ss') as MAX_TIME,
substr(rtrim(xmlagg(xmlelement(s, EVENT || '(' || WAIT_COUNT, '); ').extract('//text()') order by WAIT_COUNT desc),'; '),1,200) as WAIT_PROFILE,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9))) as sql_text
from ash left join dba_hist_sqltext using (sql_id)
group by sql_id,
sql_plan_hash_value,
trim(replace(replace(replace(dbms_lob.substr(sql_text,80),chr(10)),chr(13)),chr(9)))
order by sum(WAIT_COUNT) desc
/
====
-- tab=Last captured binds
set linesize 300 pagesize 300
col name for a25
col VALUE_STRING for a25
select sql_id,name, value_string, datatype_string, last_captured
from ( select distinct sql_id,name, value_string, datatype_string, b.last_captured, dense_rank() over(partition by name order by last_captured desc) as capture_seq
from dba_hist_sqlbind b
where 1=1
and b.sql_id = '&sql_id'
and b.was_captured = 'YES' )
where capture_seq = 1
-- order by lpad(ltrim(name,':B'),30)
;
-- tab=SQL text
select s.sql_id, a.name as command_type,s.sql_text
from dba_hist_sqltext s
left join ( select action, name from audit_actions union select 189, 'MERGE' from dual ) a on a.action = s.command_type
where 1=1
and s.sql_id = '&sql_id'
;
-- tab=Plans (AWR)
col PLAN_TABLE_OUTPUT for a100
select * from table(dbms_xplan.display_awr('&sql_id', null, null, 'ADVANCED'));
-- tab=Plan (Current)
col PLAN_TABLE_OUTPUT for a100
select * from table(dbms_xplan.display_cursor('&sql_id', null, 'ADVANCED'));
====
-- Hint info
col HINT for a60
-- define 1='01xv155rhts3j'
set verify off feedback off timi off lines 500
select distinct plan_hash_value, hint
from (select plan_hash_value, b.hint
from gv$sql_plan m,
xmltable('/other_xml/outline_data/hint' passing
xmltype(m.OTHER_XML) columns hint varchar2(4000) path '/hint') b
where sql_id = '&&1'
and plan_hash_value = nvl('&&2', plan_hash_value)
and trim(OTHER_XML) is not null
union all
select plan_hash_value, b.hint
from dba_hist_sql_plan m,
xmltable('/other_xml/outline_data/hint' passing
xmltype(m.OTHER_XML) columns hint varchar2(4000) path '/hint') b
where sql_id = '&sql_id'
and plan_hash_value = nvl('&&2', plan_hash_value)
and trim(OTHER_XML) is not null)
where 1=1
-- and hint like upper('%&&3%')
order by 1
/
SELECT sql_id, object_owner, object_name, policy_group, policy, policy_function_owner, predicate FROM gv$vpd_policy where sql_id = '&sql_id'/=======================================
-- undefine sql_id
set long 30000 pagesize 500 linesize 300
col frm heading from
select * from (select 'gv$sql' frm , sql_fulltext sql_text from gv$sql where sql_id='&&sql_id'
union all
select 'dba_hist', sql_text from dba_hist_sqltext where sql_id='&&sql_id'
);
col PLAN_TABLE_OUTPUT for a150
select t.plan_table_output from table(dbms_xplan.display_cursor('&sql_id', '', 'advanced rows bytes cost last')) t
select t.plan_table_output from table(dbms_xplan.display_cursor('&sql_id', '', 'last')) t
select t.plan_table_output from table(dbms_xplan.display_awr('&sql_id')) t ;
set linesize 300 pagesize 600 col PLAN_TABLE_OUTPUT for a150 SELECT * FROM TABLE(DBMS_XPLAN.display_cursor(sql_id=>'&sql_id', format=>'ALLSTATS LAST +cost +bytes'));
=====================================
-- Monitor
set linesize 260 pagesize 200 trimspool on long 200000
column text_line format a254
set heading off
define sql_id = '8cnh50qfgwg73'
SELECT dbms_sqltune.report_sql_monitor(
sql_id=> v.sql_id,
sql_exec_id => v.max_sql_exec_id
) text_line
from (
select
sql_id,
max(sql_exec_id) max_sql_exec_id
from
v$sql_monitor
where
sql_id = '&sql_id'
-- and status like 'DONE%'
group by
sql_id
) v
;
================
--- select max(snap_id) as snap_id from dba_hist_snapshot;
VARIABLE v_snap_id NUMBER
exec select max(snap_id) into :v_snap_id from dba_hist_snapshot ;
define sql_id='8gf11rgyym9fv'
set linesize 150 pagesize 300
select
s.elapsed_time_delta,
s.buffer_gets_delta,
s.disk_reads_delta,
cursor(select * from table(dbms_xplan.display_awr(t.sql_id, s.plan_hash_value)))
from dba_hist_sqltext t, dba_hist_sqlstat s
where 1=1
and t.dbid = s.dbid
and t.sql_id = s.sql_id
and s.snap_id between :v_snap_id-2 and :v_snap_id
-- and t.sql_text like 'select /*+ Anuj */%'
and s.sql_id='&sql_id'
;
col is_bind_sensitive for a20
col is_bind_aware like is_bind_sensitive
col is_shareable like is_bind_sensitive
select sql_id,child_number, parse_calls,executions, buffer_gets, rows_processed, plan_hash_value, is_bind_sensitive, is_bind_aware, is_shareable
from gv$sql
where sql_id='&sql_id'
;
==============
set line 700 pagesize 500
col sql_profile for a20
col sql_plan_baseline for a20
col is_bind_sensitive for a15
col is_bind_aware for a12
col is_shareable for a12
col module for a14
select * from
( select
module,
sql_id,
child_number,
plan_hash_value,
executions,
case
when elapsed_time > 0 then elapsed_time/1000
else 0
end elapsed_time_ms,
case
when executions > 0 then round(elapsed_time/nvl(executions, 1)/1000, 2)
else 0
end elapsed_time_per_exec_ms,
rows_processed,
px_servers_executions,
sorts,
invalidations,
parse_calls,
buffer_gets,
disk_reads,
optimizer_mode,
is_bind_sensitive,
is_bind_aware,
is_shareable,
sql_profile,
sql_plan_baseline,
sql_text
from
gv$sql
where 1=1
and sql_id='&sql_id'
order by elapsed_time_per_exec_ms desc
)
where rownum <= 50
====
set linesize 400 pagesize 300
col sql_text for a100 word_wrap
SELECT
dbms_lob.substr(sql_text,4000,1) sql_text
FROM
dba_hist_sqltext
WHERE
sql_id = '&sql_id'
and rownum<2
/
set linesize 400 pagesize 300 col sql_text for a100 word_wrap SELECT sql_id,dbms_lob.substr(sql_text,4000,1) sql_text FROM dba_hist_sqltext WHERE LOWER ( TRIM ( TO_CHAR ( SUBSTR ( sql_text, 1, 100 ) ) ) ) like 'select file#, block#, type%';
===
set linesize 500 pagesize 300
COL sqlmem_structure HEAD STRUCTURE FOR A20
COL sqlmem_function HEAD FUNCTION FOR A20
COL sqlmem_chunk_com HEAD CHUNK_COM FOR A20
COL sqlmem_heap_desc HEAD HEAP_ADDR FOR A16
col sql_text for a50 wrap
col is_bind_sensitive for a15
col is_bind_aware for a15
col IS_OBSOLETE like is_bind_aware
col IS_SHAREABLE like is_bind_aware
col sql_text for a50 wrap
select
INST_ID,
con_id,
sql_id,
hash_value,
users_executing,
child_number
, sharable_mem
, persistent_mem
, runtime_mem
-- , typecheck_mem
,is_bind_sensitive
,is_bind_aware
,IS_OBSOLETE
,IS_SHAREABLE
,LAST_LOAD_TIME
,sql_text sql_text
FROM
gv$sql
WHERE 1=1
and sql_id = '&sql_id'
and rownum<10
/
--define sql_id='frccccnh76gtx'
SELECT /*+ NO_MERGE(@sel$2) NO_MERGE(@sel$3) LEADING(@sel$3 c) */
-- SELECT /*+ NO_MERGE(@sel$2) NO_MERGE(@sel$3) LEADING(@sel$4 c) xNO_MERGE(@"SEL$4") */
-- sql_text
-- , sql_fulltext
-- hash_value
sql_id
, sum(chunk_size) total_size
, trunc(avg(chunk_size)) avg_size
, count(*) chunks
, alloc_class
, chunk_type
, structure sqlmem_structure
, function sqlmem_function
, chunk_com sqlmem_chunk_com
, heap_desc sqlmem_heap_desc
-- , chunk_ptr
-- , subheap_desc
FROM
gv$sql_shared_memory s
WHERE 1=1
and sql_id = '&sql_id'
-- hash_value = 1
GROUP BY
hash_value
, sql_id
, heap_desc
, structure
, function
, chunk_com
-- , chunk_ptr
, alloc_class
, chunk_type
-- , subheap_desc
ORDER BY
total_size DESC
/
====
VARIABLE BgnSnap NUMBER
VARIABLE EndSnap NUMBER
VARIABLE DID NUMBER
VARIABLE INST_NUMBER number
exec select max(snap_id) -12 into :BgnSnap from dba_hist_snapshot ;
exec select max(snap_id) into :EndSnap from dba_hist_snapshot ;
--and snap_id between :BgnSnap and nvl(:EndSnap, :BgnSnap)
col ELA_PER_EXEC for 999,999,999,999
col CPU_PER_EXEC for 999,999,999,999
col GETS_PER_EXEC for 999,999,999,999
col IOWAITS_PER_EXEC for 999,999,999,999
col CLWAITS_PER_EXEC_uS for 999,999,999,999
col APWAITS_PER_EXEC for 999,999,999,999
col CCWAITS_PER_EXEC for 999,999,999,999
col SQL_PROFILE for a20
select
instance_number as inst,
(snap_id - 1) as Begin_Snap_id,
to_char(sn.begin_interval_time,'dd.mm hh24:mi') as begin_snap_time,
round(st.executions_delta) as execs,
-- round(st.executions_delta * (st.rows_processed_delta/decode(st.executions_delta,0,1,st.executions_delta))) as rows_processed,
st.rows_processed_delta as rows_processed,
st.sql_id,
st.plan_hash_value as plan,
st.SQL_PROFILE,
st.optimizer_cost as cost,
round(st.parse_calls_delta/decode(st.executions_delta,0,1,st.executions_delta)) as PARSE_PER_EXEC,
round(st.elapsed_time_delta/decode(st.executions_delta,0,1,st.executions_delta)) as ELA_PER_EXEC,
round(st.cpu_time_delta/decode(st.executions_delta,0,1,st.executions_delta)) as CPU_PER_EXEC,
round(st.buffer_gets_delta/decode(st.executions_delta,0,1,st.executions_delta)) as GETS_PER_EXEC,
round(st.disk_reads_delta/decode(st.executions_delta,0,1,st.executions_delta)) as disk_reads_per_exec,
round(st.physical_read_bytes_delta/decode(st.executions_delta,0,1,st.executions_delta)/1024/1024) as READ_MB_PER_EXEC,
round(st.physical_read_requests_delta/decode(st.executions_delta,0,1,st.executions_delta)) as READS_PER_EXEC,
round(st.physical_write_bytes_delta/decode(st.executions_delta,0,1,st.executions_delta)/1024/1024) as WRITES_MB_PER_EXEC,
round(st.physical_write_requests_delta/decode(st.executions_delta,0,1,st.executions_delta)) as WRITES_PER_EXEC,
round(st.direct_writes_delta/decode(st.executions_delta,0,1,st.executions_delta)) as DIRECT_WRITES_PER_EXEC,
round(st.rows_processed_delta/decode(st.executions_delta,0,1,st.executions_delta)) as ROWS_PER_EXEC,
round(st.fetches_delta/decode(st.executions_delta,0,1,st.executions_delta)) as FETCHES_PER_EXEC,
round(st.iowait_delta/decode(st.executions_delta,0,1,st.executions_delta)) as IOWAITS_PER_EXEC,
round(st.clwait_delta/decode(st.executions_delta,0,1,st.executions_delta)) as CLWAITS_PER_EXEC_uS,
round(st.apwait_delta/decode(st.executions_delta,0,1,st.executions_delta)) as APWAITS_PER_EXEC,
round(st.ccwait_delta/decode(st.executions_delta,0,1,st.executions_delta)) as CCWAITS_PER_EXEC,
round(st.parse_calls_delta/decode(st.executions_delta,0,1,st.executions_delta)) as PARSE_PER_EXEC,
round(st.plsexec_time_delta/decode(st.executions_delta,0,1,st.executions_delta)) as PLSQL_PER_EXEC,
round(st.px_servers_execs_delta/decode(st.executions_delta,0,1,st.executions_delta)) as PX_PER_EXEC,
round(st.clwait_delta/1000000) as clwaits_sec
from dba_hist_sqlstat st join dba_hist_snapshot sn using(snap_id,instance_number)
where 1=1
and sql_id = '&sql_id'
-- and snap_id between &&2 and nvl('&&3', &&2)
--and snap_id > 39933 ---:BgnSnap
and snap_id between :BgnSnap and nvl(:EndSnap, :BgnSnap)
-- and executions_delta > 0
and (st.elapsed_time_delta > 0 and st.executions_delta is not null)
order by snap_id, instance_number
=========================
set linesize 500 pagesize 500
VARIABLE dbid NUMBER
VARIABLE bid NUMBER
VARIABLE eid NUMBER
exec select max(snap_id) -30 into :bid from dba_hist_snapshot ;
exec select max(snap_id) into :eid from dba_hist_snapshot ;
exec select DBID into :dbid from v$database;
define 1='8cnh50qfgwg73'
col ELA_PER_EXEC for 999,999,999,999
col CPU_PER_EXEC for 999,999,999,999
col GETS_PER_EXEC for 999,999,999,999
col IOWAITS_PER_EXEC for 999,999,999,999
col CLWAITS_PER_EXEC_uS for 999,999,999,999
col APWAITS_PER_EXEC for 999,999,999,999
col CCWAITS_PER_EXEC for 999,999,999,999
col DURATION for a27
col MIN_SAMPLE_TIME for a25
col MAX_SAMPLE_TIME for a25
with ash as
( select --+ parallel(4) materialize
instance_number as inst
, sql_id
, sql_plan_hash_value
, sql_exec_id
, count(*) as ash_rows
, (cast(max(sample_time) as date)-cast(min(sample_time) as date)) as durn
, max(sample_time) - min(sample_time) as dur
, min(sample_time) as min_sample_time
, max(sample_time) as max_sample_time
, min(snap_id) as min_snap_id
, max(snap_id) as max_snap_id
, count(distinct session_id) as px
from dba_hist_active_sess_history
where 1=1
and (snap_id between :bid and nvl(:eid, :bid))
and sql_id = '&1'
and sql_exec_id > 0
group by instance_number, sql_id, sql_plan_hash_value, sql_exec_id
having (cast(max(sample_time) as date)-cast(min(sample_time) as date)) < 1 and (cast(max(sample_time) as date)-cast(min(sample_time) as date)) > 0
order by 3)
select inst, sql_id, sql_plan_hash_value, sql_exec_id, ash_rows
, round(durn*86400) as seconds
, max_sample_time-min_sample_time as duration
, min_sample_time
, max_sample_time
, min_snap_id
, max_snap_id
, px
,(select min(round(st.rows_processed_delta / decode(st.executions_delta, 0, 1, st.executions_delta)))
||' / '||
max(round(st.rows_processed_delta / decode(st.executions_delta, 0, 1, st.executions_delta)))
from dba_hist_sqlstat st
where st.snap_id between min_snap_id and max_snap_id
and st.instance_number = inst
and st.sql_id = '&1'
and st.plan_hash_value = sql_plan_hash_value
and st.snap_id between :bid and nvl(:eid, :bid)) as min_max_rows
from ash
order by sql_plan_hash_value , min_sample_time
/
=======
set ver off pages 50000 lines 400 tab off
undef sql_id
undef days_history
undef interval_hours
undef grby_inst
def sql_id="8cnh50qfgwg73" ---- <<<<<<
def days_history="1"
def interval_hours="1"
def grby_inst="1"
col time for a19
col inst for 9999
col executions for 9999999999
col rows_processed_1exec for 9999999.999
col elapsed_time_s_total for 9999999.999
col javexec_time_s_1exec for 9999999.999
col buffer_gets_1exec for 999999999999.999
col disk_reads_1exec for 999999999999.999
col direct_writes_1exec for 999999999999.999
select to_char(trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)-(trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24,'yyyy-mm-dd hh24:mi:ss') time,
nvl(sum(hss.executions_delta),0) executions,
round(sum(hss.elapsed_time_delta)/1000000,3) elapsed_time_s_total,
round(sum(hss.elapsed_time_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) elapsed_time_s_1exec,
round(sum(hss.cpu_time_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) cpu_time_s_1exec,
round(sum(hss.iowait_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) iowait_s_1exec,
round(sum(hss.clwait_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) clwait_s_1exec,
round(sum(hss.apwait_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) apwait_s_1exec,
round(sum(hss.ccwait_delta)/1000000/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) ccwait_s_1exec,
round(sum(hss.rows_processed_delta)/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) rows_processed_1exec,
round(sum(hss.buffer_gets_delta)/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) buffer_gets_1exec,
round(sum(hss.disk_reads_delta)/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) disk_reads_1exec,
round(sum(hss.direct_writes_delta)/decode(sum(hss.executions_delta),0,null,sum(hss.executions_delta)),3) direct_writes_1exec
from dba_hist_sqlstat hss, (select snap_id, min(hs2.begin_interval_time) begin_interval_time from dba_hist_snapshot hs2 group by snap_id) hs
where hss.sql_id(+)='&sql_id'
and hss.snap_id(+)=hs.snap_id
and hs.begin_interval_time>=trunc(sysdate)-&days_history+1
group by trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)-(trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24
order by trunc(sysdate-&days_history+1)+trunc((cast(hs.begin_interval_time as date)-(trunc(sysdate-&days_history+1)))*24/(&interval_hours))*(&interval_hours)/24;
BREAK ON inst SKIP 1
TIME EXECUTIONS ELAPSED_TIME_S_TOTAL ELAPSED_TIME_S_1EXEC CPU_TIME_S_1EXEC IOWAIT_S_1EXEC CLWAIT_S_1EXEC APWAIT_S_1EXEC CCWAIT_S_1EXEC ROWS_PROCESSED_1EXEC BUFFER_GETS_1EXEC DISK_READS_1EXEC DIRECT_WRITES_1EXEC
------------------- ----------- -------------------- -------------------- ---------------- -------------- -------------- -------------- -------------- -------------------- ----------------- ----------------- -------------------
2022-03-10 00:00:00 108 7216.581 66.82 20.334 28.706 26.735 0 .005 1.000 419494.222 65840.722 .000
2022-03-10 01:00:00 90 7086.855 78.743 21.476 36.847 29.679 0 .006 1.000 434268.778 68373.689 .000
2022-03-10 02:00:00 108 7190.316 66.577 19.615 29.719 26.035 0 .004 1.000 396152.481 62198.833 .000
2022-03-10 03:00:00 108 7211.468 66.773 19.98 29.27 26.378 0 .004 1.000 406881.704 63974.361 .000
2022-03-10 04:00:00 105 7210.533 68.672 20.047 30.463 27.012 0 .004 1.000 .000 63597.400 .000
2022-03-10 05:00:00 93 7212.631 77.555 22.56 34.287 30.824 0 .004 1.000 457872.032 71817.903 .000
2022-03-10 06:00:00 108 7211.188 66.77 19.714 28.951 26.615 0 .004 1.000 402315.056 63165.185 .000
7 rows selected.
================
objects of a specific sql id
set lines 300 set pages 300
col table_name for a40
col owner for a30
select distinct owner, table_name, STALE_STATS, last_analyzed, stattype_locked
from dba_tab_statistics
where (owner, table_name) in
(select distinct owner, table_name
from dba_tables
where ( table_name)
in ( select object_name
from gv$sql_plan
where upper(sql_id) = upper('&sql_id') and object_name is not null))
--and STALE_STATS='YES'
/
======
set linesize 500 pagesize 500
VARIABLE dbid NUMBER
VARIABLE bid NUMBER
VARIABLE eid NUMBER
VARIABLE INST_NUMBER number
exec select max(snap_id) -1 into :bid from dba_hist_snapshot ;
exec select max(snap_id) into :eid from dba_hist_snapshot ;
exec select DBID into :dbid from v$database;
define sql_text='WITH a as (SELECT'
set pagesize 999
set lines 200
col sql_text format a36 trunc
col inst for 99
col total_row for 99999999
col execs for 9,999,999
col avg_etime for 99,999.999
col avg_lio for 999,999,999.9
col avg_pio for 999,999,999.9
col begin_interval_time for a30
col hash_value for 9999999999
col "interval time" for a20
break on hash_value on startup_time skip 1
select to_char(begin_interval_time,'yyyy-mm-dd hh24:mi:ss') "interval time", ss.instance_number inst, s.sql_id, plan_hash_value hash_value,
nvl(executions_delta,0) execs,
elapsed_time_delta/1000000 etime,
(elapsed_time_delta/decode(nvl(executions_delta,0),0,1,executions_delta))/1000000 avg_etime,
buffer_gets_delta lio,
(buffer_gets_delta/decode(nvl(buffer_gets_delta,0),0,1,executions_delta)) avg_lio,
trim(replace(dbms_lob.substr(sql_text,36, 1),chr(9),'')) sql_text
from DBA_HIST_SQLSTAT S, DBA_HIST_SNAPSHOT SS, DBA_HIST_SQLTEXT ST
where 1=1
-- and dbms_lob.substr(sql_text,3999,1) like '&sql_text&'
and ST.sql_id='92yv7gw4g8b94'
and ss.snap_id = S.snap_id
and ss.instance_number = S.instance_number
and s.sql_id = st.sql_id
and executions_delta > 0
and ss.snap_id >= :bid
and ss.snap_id<=:eid
and s.sql_id = st.sql_id
order by begin_interval_time desc
;
undef sql_text
======
from web https://github.com/soliverr/oracle-tpt/blob/master/nonshared.sql
define 1='8y2uufyruukhs'
set serverout on size 1000000
prompt Show why existing SQL child cursors were not reused (gV$SQL_SHARED_CURSOR)...
prompt
def cmd="select * from gv$sql_shared_cursor where sql_id = ''&1''"
declare
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_descTbl dbms_sql.desc_tab;
l_colCnt number;
procedure execute_immediate( p_sql in varchar2 )
is
BEGIN
dbms_sql.parse(l_theCursor,p_sql,dbms_sql.native);
l_status := dbms_sql.execute(l_theCursor);
END;
begin
execute_immediate( 'alter session set nls_date_format= ''dd-mon-yyyy hh24:mi:ss'' ');
dbms_sql.parse( l_theCursor, replace( '&cmd', '"', ''''), dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
for i in 1 .. l_colCnt loop
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
end loop;
l_status := dbms_sql.execute(l_theCursor);
while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
if l_columnValue != 'N' then
dbms_output.put_line ( rpad( l_descTbl(i).col_name, 30 ) || ': ' || l_columnValue );
end if;
end loop;
dbms_output.put_line( '-----------------' );
end loop;
execute_immediate( 'alter session set nls_date_format= ''dd-MON-yy'' ');
exception
when others then
execute_immediate( 'alter session set nls_date_format=''dd-MON-yy'' ');
raise;
end;
/
new 17: replace( 'select * from gv$sql_shared_cursor where sql_id = ''8y2uufyruukhs''', '"', ''''),
INST_ID : 1
SQL_ID : 8y2uufyruukhs
ADDRESS : 00000000F816ADA0
CHILD_ADDRESS : 00000000ED335850
CHILD_NUMBER : 0
HASH_MATCH_FAILED : Y
REASON : <ChildNode><ChildNumber>0</ChildNumber><ID>36</ID><reason>Authorization Check failed(4)</reason><size>5x4</size><translation_table_position>0</translation_table_position><original_handle>4068010336</original_handle><temp_handle>3992620960</temp_handle><schema>0</schema><synonym_object_number>0</synonym_object_number></ChildNode>
CON_ID : 0
-----------------
select SQL_ID,SQL_TEXT from gv$sql
where 1=1
and SQL_TEXT like '%from t_acs%' ---<<<<<
and sql_text not like 'select SQL_ID,SQL_TEXT from gv$sql where%'
and sql_text not like 'select SQL_ID,SQL_TEXT from v$sql where%'
SQL_ID SQL_TEXT
------------- --------------------------------------------------
fbac69md98mr5 select count(*) from t_acs where n2 <= :ln2
3pkkd8g9u2946 select count(*) from t_acs where n2 = :ln2
8y2uufyruukhs select count(*) from t_acs where n2 = :ln2
alter session set nls_date_format='dd-mm-yyyy hh24:mi';
set linesize 500 pagesize 500
col bind for a50 wrap
col is_obsolete for a15
col is_bind_sensitive for a15
col is_bind_aware for a15
col is_shareable for a15
col sql_text for a50 wrap
select sql_id, plan_hash_value, child_number , is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, substr(sql_text,1,50) sql_text, dbms_sqltune.extract_binds(bind_data) bind
from gv$sql where sql_id = '&sql_id';
==================
https://tanelpoder.com/2013/03/17/asqlmon-sql-sql-monitoring-like-execution-plan-line-level-drilldown-into-sql-response-time/
asqlmon.sql
var sqlid VARCHAR2(13);
begin :sqlid := '20ufdw96tvyz8'; end; ----- change sql id
/
define 2=''
define 3=''
define 4=''
set linesize 500
set pages 9999
set verify off
COL asqlmon_operation FOR a100
COL session_state FOR a15
COL EVENT FOR a30
COL AVG_P3 FOR 999999999.99
COL asqlmon_predicates FOR a100 word_wrap
COL options FOR a30
COL asqlmon_plan_hash_value HEAD PLAN_HASH_VALUE
COL asqlmon_sql_id HEAD SQL_ID NOPRINT
COL asqlmon_sql_child HEAD CHILD# NOPRINT
COL asqlmon_sample_time HEAD SAMPLE_HOUR
COL projection FOR A520
COL pct_child HEAD "Activity %" FOR A8
COL pct_child_vis HEAD "Visual" FOR A12
COL asqlmon_id HEAD "Line ID" FOR 9999
COL asqlmon_parent_id HEAD "Parent" FOR 9999
col OBJ_ALIAS_QBC_NAME for a25
col ASQLMON_OPERATION for a45
BREAK ON asqlmon_plan_hash_value SKIP 1 ON asqlmon_sql_id SKIP 1 ON asqlmon_sql_child SKIP 1 ON asqlmon_sample_time SKIP 1 DUP ON asqlmon_operation
WITH sample_times AS (
select * from dual
),
sq AS (
SELECT
-- to_char(ash.sample_time, 'YYYY-MM-DD HH24') sample_time
count(*) samples
, ash.sql_id
, ash.sql_child_number
, ash.sql_plan_hash_value
, ash.sql_plan_line_id
, ash.sql_plan_operation
, ash.sql_plan_options
, ash.session_state
, ash.event
, AVG(ash.p3) avg_p3
, sum(ash.time_waited) sum_time_waited
FROM
dba_hist_active_sess_history ash
WHERE
1=1
--AND ash.session_id = 8 AND ash.session_serial# = 35019
AND ash.sql_id = :sqlid
--AND ash.sql_plan_hash_value = &2
--AND ash.snap_id >= &3 and ash.snap_id<=&4
GROUP BY
--to_char(ash.sample_time, 'YYYY-MM-DD HH24')
ash.sql_id
, ash.sql_child_number
, ash.sql_plan_hash_value
, ash.sql_plan_line_id
, ash.sql_plan_operation
, ash.sql_plan_options
, ash.session_state
, ash.event
)
SELECT
plan.sql_id asqlmon_sql_id
-- , plan.plan_hash_value asqlmon_plan_hash_value
, sq.samples seconds
, LPAD(TO_CHAR(ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 100, 1), 999.9)||' %',8) pct_child
, '|'||RPAD( NVL( LPAD('#', ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 10), '#'), ' '), 10,' ')||'|' pct_child_vis
, sq.session_state
, sq.event
, sq.sum_time_waited
--, sq.avg_p3
--, sq.sample_time asqlmon_sample_time
--, LPAD(plan.id,4)||CASE WHEN parent_id IS NULL THEN ' ' ELSE ' <- ' END||LPAD(plan.parent_id,4) asqlmon_plan_id
, plan.id asqlmon_id
, nvl(plan.parent_id,'-1') asqlmon_parent_id
, LPAD(' ', depth) || plan.operation ||' '|| plan.options || NVL2(plan.object_name, ' ['||plan.object_name ||']', null) asqlmon_operation
, plan.object_alias || CASE WHEN plan.qblock_name IS NOT NULL THEN ' ['|| plan.qblock_name || ']' END obj_alias_qbc_name
FROM
dba_hist_sql_plan plan
, sq
WHERE
1=1
AND sq.sql_id(+) = plan.sql_id
AND sq.sql_plan_line_id(+) = plan.id
AND sq.sql_plan_hash_value(+) = plan.plan_hash_value
AND plan.sql_id = :sqlid
ORDER BY
plan.plan_hash_value
, plan.id
/
===
ASH report
-- last 24hr report
var BgnSnap number;
var EndSnap number;
exec select max(snap_id) -24 into :BgnSnap from dba_hist_snapshot ;
exec select max(snap_id) into :EndSnap from dba_hist_snapshot ;
select * from table(dbms_workload_repository.awr_sql_report_text((select dbid from v$database), (select instance_number from v$instance), :BgnSnap,:EndSnap, '&sql_id'));
====Expalin Plan
define sql_id='f705bwx3q0ydq'set linesize 400 pagesize 300col PLAN_TABLE_OUTPUT for a200select *from (SELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id', null, 'BASIC'))UNION ALLSELECT * FROM TABLE(dbms_xplan.display_awr('&sql_id', null, null, 'ALL')))
define sql_id='f705bwx3q0ydq'set linesize 400 pagesize 300col OWNER for a20col index_name for a50col TABLE_NAME for a30col global_stats for a20SELECT owner, index_name, table_name,last_analyzed, sample_size, num_rows, partitioned, global_stats FROM dba_indexesWHERE index_name IN (select distinct rtrim(substr(plan_table_output, instr(plan_table_output, '|', 1, 3)+2, (instr(plan_table_output, '|', 1, 4)-instr(plan_table_output, '|', 1, 3)-2)), ' ')from (SELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id', null, 'BASIC'))UNION ALLSELECT * FROM TABLE(dbms_xplan.display_awr('&sql_id', null, null, 'ALL')))where plan_table_output like '%INDEX%')ORDER BY owner, table_name, index_name/
===set linesize 300
col id format 99
col operation format a30
col options format a30
col object_name format a30
col cost format 9999
SELECT vs.sql_id,id, operation, options, object_name, cost FROM gv$sql_plan VP, gv$sql VS
WHERE VP.address = VS.address
AND VP.hash_value = VS.hash_value
-- AND sql_text LIKE '%1%'
AND sql_text NOT LIKE '%v$sql%'
-- and object_name=:h
and vs.sql_id='&sql_id'
and rownum < 20
ORDER BY id
/
set lines 1000 pages 300
col name for a30
col task_exec_name for a16
col category for a10
col created for a30
col sql_text for a150
col signature for 9999999999999999999999999
select sql.sql_id,sql.child_number as child , prof.name, prof.category, prof.created, prof.task_exec_name,prof.force_matching, prof.status, prof.signature,prof.sql_text
from dba_sql_profiles prof,gv$sql sql
where sql.sql_id in ('&sql_id')
order by created;
set long 100000 linesize 100 pagesize 300
select other_xml from v$sql_plan where sql_id like '&sql_id' and id=1;
===
set linesize 200 pagesize 0
SELECT * FROM TABLE(DBMS_XPLAN.display_cursor(sql_id=>'&sql_id', format=>'ALLSTATS LAST +cost +bytes +outline +PEEKED_BINDS +adaptive'));
select * from table(dbms_xplan.display_awr(sql_id=>'&sql_id', format=>'ALLSTATS LAST +cost +bytes +outline +PEEKED_BINDS +adaptive'));
col SQL_TEXT for a70 wrapselect sql_id,child_number,HASH_VALUE,EXACT_MATCHING_SIGNATURE,sql_text from gv$sql where sql_text like '%select count(*) from func_test%';
define sql_id='2yh5y6s2vh1yg'--define sql_id='01twva1ky0bp9'set linesize 200 pagesize 0SELECT * FROM TABLE(dbms_xplan.display_cursor(sql_id => '&sql_id', cursor_child_no => 2, FORMAT => 'TYPICAL -ROWS -BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE'));
--
set linesize 300 pagesize 300
col OWNER for a20col OBJECT_NAME for a20col EVENT for a27define sql_id='8cnh50qfgwg73'
VARIABLE BgnSnap NUMBERVARIABLE EndSnap NUMBERVARIABLE DID NUMBERVARIABLE INST_NUMBER numberVARIABLE x VARCHAR2(30)exec select max(snap_id) -2 into :BgnSnap from dba_hist_snapshot ;exec select max(snap_id) into :EndSnap from dba_hist_snapshot ;exec select DBID into :DID from v$database;exec select INSTANCE_NUMBER into :INST_NUMBER from v$instance ;
SELECT ss.sql_id, ss.event, ss.current_obj#, ss.owner, ss.object_name, ss.object_type, ss.time_waited, ss.counts_waited, tt.total_time, ROUND((ss.time_waited*100/tt.total_time),1) percentFROM (SELECT s.sql_id, s.event, s.current_obj#, o.owner, o.object_name, o.object_type, COUNT(*) counts_waited, SUM(time_waited) time_waited FROM dba_hist_active_sess_history s, dba_hist_seg_stat_obj o, v$database d, v$instance i WHERE s.dbid = d.dbid AND s.instance_number = i.instance_number AND s.sql_id = '&sql_id' -- AND s.event = '<wait name>' AND o.dbid (+) = s.dbid AND o.obj# (+) = s.current_obj# AND s.snap_id > :BgnSnap AND s.snap_id <= :EndSnap GROUP BY s.sql_id, s.event, s.current_obj#, o.owner, o.object_name, o.object_type) ss, (SELECT SUM(time_waited) total_time FROM dba_hist_active_sess_history t, v$database d, v$instance i WHERE t.dbid = d.dbid AND t.instance_number = i.instance_number AND t.sql_id = '&sql_id' -- AND t.event = '<wait name>' AND t.snap_id > :BgnSnap AND t.snap_id <= :EndSnap ) tt ORDER BY ss.counts_waited DESC;
=====
from web
define 1='64umkzksavzym'
define 2='Y'
ttitle off
btitle off
col s_diag_pack new_value s_diag_pack noprint
col s_sql_id new_value s_sql_id noprint
var v_sql_id varchar2(13)
set feed off term off
select '&1' s_sql_id from dual;
set term on
whenever sqlerror exit 128
begin
:v_sql_id := '&s_sql_id';
if
length(:v_sql_id) < 1
or
:v_sql_id is null
then
raise value_error;
end if;
end;
/
whenever sqlerror continue
set feed on
set feed off term off
select decode(upper('&2'),'Y','','--') s_diag_pack from dual;
set feed on term on
set pagesize 100 linesize 300 trimspool on
col partition_start format a6 head 'PSTART'
col sql_id format a13
col partition_stop format a6 head 'PSTOP'
col owner format a20
col table_name format a30
col index_name format a40
col phv format a43 wrap
col last_analyzed format a19
col stale_stats format a11 head 'stale_stats'
col num_rows format 99,999,999,999
col blocks format 9,99,999,999
col partition_position format 999999 head 'PP'
col phv for a15
break on sql_id skip 1
--spool stats-sqlid.txt
with objects as (
-- extra inline view is to eliminate duplicates in listagg()
select
sql_id
, listagg(phv,',') within group(order by phv) phv
, object_owner
, object_name
, object_type
, partition_start
, partition_stop
from (
select distinct
sql_id
, phv
, object_owner
, object_name
, object_type
, partition_start
, partition_stop
from (
select
sql_id
, plan_hash_value phv
, object_owner
, object_name
, object_type
, case partition_start
when 'ROW LOCATION' then 'ROWID'
else partition_start
end partition_start
, case partition_stop
when 'ROW LOCATION' then 'ROWID'
else partition_stop
end partition_stop
from v$sql_plan
where sql_id = :v_sql_id
and object_owner is not null
and object_type in ('TABLE','INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)','CLUSTER','TABLE (FIXED)')
&s_diag_pack union all
&s_diag_pack select
&s_diag_pack sql_id
&s_diag_pack , plan_hash_value phv
&s_diag_pack , object_owner
&s_diag_pack , object_name
&s_diag_pack , object_type
&s_diag_pack , case partition_start
&s_diag_pack when 'ROW LOCATION' then 'ROWID'
&s_diag_pack else partition_start
&s_diag_pack end partition_start
&s_diag_pack , case partition_stop
&s_diag_pack when 'ROW LOCATION' then 'ROWID'
&s_diag_pack else partition_stop
&s_diag_pack end partition_stop
&s_diag_pack from dba_hist_sql_plan
&s_diag_pack where sql_id = :v_sql_id
&s_diag_pack and object_owner is not null
&s_diag_pack and object_type in ('TABLE','INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)','CLUSTER','TABLE (FIXED)')
)
)
group by
sql_id
, object_owner
, object_name
, object_type
, partition_start
, partition_stop
),
indexes as (
select * from objects where object_type in ('INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)')
),
tables as (
select * from objects where object_type in ('TABLE','CLUSTER','TABLE (FIXED)')
)
select
sql_id
, phv
, owner
, table_name
|| decode(s.partition_name, null,'','.' || s.partition_name)
as table_name
, null index_name
, partition_position
, t.partition_start
, t.partition_stop
, num_rows
, blocks
, to_char(last_analyzed,'yyyy-mm-dd hh24:mi:ss') last_analyzed
, stale_stats
from dba_tab_statistics s
join tables t on t.object_owner = s.owner
and t.object_name = s.table_name
union all
select
sql_id
, phv
, owner
, table_name
, index_name
|| decode(s.partition_name, null,'','.' || s.partition_name)
as index_name
, partition_position
, i.partition_start
, i.partition_stop
, num_rows
, leaf_blocks blocks
, to_char(last_analyzed,'yyyy-mm-dd hh24:mi:ss') last_analyzed
, stale_stats
from dba_ind_statistics s
join indexes i on i.object_owner = s.owner
and i.object_name = s.index_name
order by sql_id
, owner
, table_name
, index_name nulls first
, partition_position nulls first
/
define 1= 'c33dcq0rbj6gg' set feedback off heading on timi on pages 200 lines 500 echo off VERIFY OFF col INST for 9999 col EXECS for 99999999 col CHILD for 99999 col BIND_SENSE for a10 col BIND_AWARE for a10 col SHAREABLE for a10 col USE_FEEDBACK_STATS for a18 col OPTIMIZER_STATS for a16 col BIND_EQ_FAILURE for a16 col Reason for a40 col SQL_PLAN_BASELINE for a30 col SQL_PATCH for a30 col SQL_PROFILE for a64 col IS_OBSOLETE for a11 col FLASHBACK_CURSOR for a16 col IS_SHAREABLE for a12 SELECT INST, sql_id, child_number, is_obsolete as "IS_OBSOLETE", flashback_cursor as "FLASHBACK_CURSOR", is_shareable as "IS_SHAREABLE", LISTAGG(Reason, '; ') WITHIN GROUP (ORDER BY to_number(rid)) AS Reason FROM (select sc.sql_id, sc.child_number, sc.child_address, sc.inst_id as INST, is_obsolete, flashback_cursor, is_shareable, xt.rid || ' |' || xt.Reasons || ' |' || xt.Details as Reason, xt.rid from gv$sql_shared_cursor sc, gv$sql s, xmltable('/Reasonz/ChildNode' passing xmltype('<Reasonz>'||sc.reason||'</Reasonz>') columns RID varchar2(60) path 'ID', Reasons varchar2(60) path 'reason', Details varchar2(60) path 'details') xt where dbms_lob.substr(reason, 256) <> ' ' and '&1' = sc.sql_id and s.sql_id = sc.sql_id and s.child_address = sc.child_address and 1 = sc.inst_id) GROUP BY INST, sql_id, is_obsolete, flashback_cursor, is_shareable, child_number, child_address /INST SQL_ID CHILD_NUMBER IS_OBSOLETE FLASHBACK_CURSOR IS_SHAREABLE REASON ----- ------------- ------------ ----------- ---------------- ------------ ---------------------------------------- 1 c33dcq0rbj6gg 0 N N Y 39 |Bind mismatch(33) |High SQL Version Counts - Script to determine reason(s) (Doc ID 438755.1) select * from table(version_rpt('c33dcq0rbj6gg'));===========================================sql text search !!!!!!!!!!!!!!!!!!!!!!!!!!http://kerryosborne.oracle-guy.com/2009/04/16/hidden-sql-why-cant-i-find-my-sql-text/ set pagesize 300 select CON_ID,SQL_ID,count(*) from v$open_cursor group by CON_ID,SQL_ID --having count(*) >3000 order by count(*); CON_ID SQL_ID COUNT(*) ---------- ------------- ---------- 3 7qmnt62p9kkxu 64703 define sql_text='' define sql_id='9zg9qd9bm4spu' set verify off set pagesize 999 col username format a13 col prog format a22 col sql_text format a50 col sid format 999 col child_number format 99999 heading CHILD col ocategory format a10 col avg_etime format 9,999,999.99 col etime format 9,999,999.99 set linesize 400 select sql_id, child_number, plan_hash_value plan_hash, executions execs, elapsed_time/1000000 etime, (elapsed_time/1000000)/decode(nvl(executions,0),0,1,executions) avg_etime, u.username, sql_text from gv$sql s, dba_users u where 1=1 -- and upper(sql_text) like upper(nvl('&sql_text',sql_text)) and sql_text not like '%from v$sql where sql_text like nvl(%' and sql_id like nvl('&sql_id',sql_id) and u.user_id = s.parsing_user_id / set lines 300 col sql_text for a70 select sql_text from gv$sqltext where 1=1 -- sql_text like nvl('&sql_text','%') and sql_id like nvl('&sql_id',sql_id) order by piece /=============================================table_4_9_fbd5_0_0_0table_4_9_fbd5_0_0_0 Enter value for hex_value: fbd5 select owner, object_name, object_type from dba_objects where object_id = (select to_number('&hex_value','XXXXXX') from dual); Enter value for hex_value: fbd5 select distinct * from v$open_cursor where rownum < 10 and sql_id like '%fat%'; select username, sid, serial#, status, sql_id, prev_sql_id, event, seconds_in_wait from v$session where 1=1 -- username like nvl('&username',username) and sid like nvl('&sid',sid) and sql_id='7vtb7h3kpfat0' order by username, sid, serial#; set pagesize 300 select CON_ID,SQL_ID,count(*) from v$open_cursor group by CON_ID,SQL_ID having count(*) >3000 order by count(*); define sql_id='5dqz0hqtp9fru' select inst_id,kgllkuse, kgllksnm, user_name, kglhdpar, kglnahsh, kgllksqlid, kglnaobj, kgllkest, decode(kgllkexc, 0, to_number(NULL), kgllkexc), kgllkctp from x$kgllk where kglhdnsp = 0 and kglhdpar != kgllkhdl and kgllksqlid like nvl('&sql_id',kgllksqlid);https://github.com/tanelpoder/tpt-oracle/blob/master/kgllk.sql set linesize 500 col hold_mode head HOLD_MODE for a10 col req_mode head REQ_MODE for a10 col object_owner head OBJECT_OWNER for a15 col namespace for a30 word_wrap col kgllk_state head 0xSTATE for A8 col sid for 99999999999 SELECT * FROM ( SELECT s.sid , KGLLKSNM rsid -- , KGLLKADR -- , KGLLKUSE -- , KGLLKSES , decode(l.kgllkmod, 0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive', to_char(l.kgllkmod)) hold_mode , decode(l.kgllkreq, 0, 'None', 1, 'Null', 2, 'Share', 3, 'Exclusive', to_char(l.kgllkreq)) req_mode -- , LPAD('0x'||TRIM(TO_CHAR(l.kgllkflg,'XXXXX')),8) kgllk_state -- , decode(l.kgllkflg, 0, 1, 'BROKEN', 2, 'BREAKABLE', l.kgllkflg) kgllk_state -- 11g stuff -- , kgllkest -- , kgllkexc -- , KGLLKFLG -- , KGLLKSPN -- , KGLLKHTB , KGLNAHSH , KGLLKSQLID -- , KGLHDPAR -- , KGLHDNSP -- , n.kglsttyp , n.kglstdsc namespace , USER_NAME object_owner , KGLNAOBJ object_name , TO_CHAR(l.kgllkflg,'XXXXX') kgllk_state , KGLLKHDL , KGLLKPNC , KGLLKPNS , KGLLKCNT -- , KGLLKCTP -- cursor type FROM x$kgllk l , v$session s , x$kglst n WHERE s.saddr(+) = l.kgllkuse AND l.kglhdnsp = n.indx --AND kgllkhdl = hextoraw(upper(lpad('&1',vsize(l.kgllkhdl)*2,'0'))) ) WHERE 1=1 and KGLLKSQLID='cgk2h2fsy3zsk' /define sql_id='XXXXXXX' SET LONG 1000000 LONGCHUNKSIZE 1000000 LINESIZE 1000 PAGESIZE 0 TRIM ON TRIMSPOOL ON ECHO OFF FEEDBACK OFF --SPOOL /u04/reports/report_sql_monitor.txt; SELECT DBMS_SQLTUNE.report_sql_monitor( sql_id => '&&sql_id', type => 'TEXT', report_level => 'ALL') AS report FROM dual; SET lines 300 pagesize 100 col PLAN FOR a200 SELECT RPAD('(' || p.ID || ' ' || NVL(p.parent_id,'0') || ')',8) || '|' || RPAD(LPAD (' ', 2*p.DEPTH) || p.operation || ' ' || p.options,40,'.') || NVL2(p.object_owner||p.object_name, '(' || p.object_owner|| '.' || p.object_name || ') ', '') || 'Cost:' || p.COST || ' ' || NVL2(p.bytes||p.CARDINALITY,'(' || p.bytes || ' bytes, ' || p.CARDINALITY || ' rows)','') || ' ' || NVL2(p.partition_id || p.partition_start || p.partition_stop,'PId:' || p.partition_id || ' PStart:' || p.partition_start || ' PStop:' || p.partition_stop,'') || 'io cost=' || p.io_cost || ',cpu_cost=' || p.cpu_cost AS PLAN FROM dba_hist_sql_plan p WHERE p.sql_id='&&sql_id' -- AND plan_hash_value='554605205' ORDER BY p.id, p.parent_id;-- sql hint set linesize 300 pagesize 300 col HINT for a100 define sql_id='7ycnuk1r48000' define child_no=0 select extractvalue(value(t),'.') hint from table( select xmlsequence( extract(xmltype(other_xml),'/other_xml/outline_data/hint') ) from gv$sql_plan where sql_id = '&sql_id' and child_number = &child_no and other_xml is not null ) t / col OUTLINE_HINTS for a100 define sql_id='cp1g5w1dgc000' select distinct extractvalue(value(d), '/hint') as outline_hints from xmltable('/*/outline_data/hint' passing ( select xmltype(other_xml) as xmlval from dba_hist_sql_plan where 1=1 and sql_id = '&sql_id' --and plan_hash_value = &plan_hash_value and other_xml is not null and rownum<2 ) ) d;define sql_id='94qn6y14kw01g' define child_number=0 set echo off feed off head off BEGIN DBMS_SQLDIAG.DUMP_TRACE(p_sql_id => '&&sql_id', p_child_number => &&child_number, p_component => 'Compiler', p_file_id => 'Optimizer_Trace' ); END; / undef sql_id undef child_number col VALUE for a100 select value from v$diag_info where name = 'Default Trace File'; to check location set linesize 400 col ALERT_LOG for a100 col PATH for a100 select di.value ||'/'|| 'alert_' || i.instance_name || '.log' ALERT_LOG from v$diag_info di, v$instance i where di.name = 'Diag Trace';http://anuj-singh.blogspot.com/2018/01/tracing-sql-statement-execution.htmlset trimspool on lines 400 long 10000 longchunk 10000 pages 999 longchunk 100000 long 10000 time on undefine SQL_ID undefine PLAN_HASH_VALUE undefine SQL_TEXT Select distinct dbid, sql_id, plan_hash_value, timestamp from dba_hist_sql_plan where sql_id='88z8ja83k19xx' order by dbid, timestamp / undefine PLAN_HASH_VALUE -- accept PLAN_HASH_VALUE prompt 'Please enter PLAN_HASH_VALUE to show Statistics for: ' col iowait_delta format 9999999.99 heading iowaitdelta(ms) col iowait_total format 9999999.99 heading iowaittotal(ms) col ELAPSED_TIME_TOTAL format 9999999.99 heading elapsdtimetotal(ms) col ELAPSED_TIME_DELTA format 9999999.99 heading elapsdtimedelta(ms) col PLAN_HASH_VALUE heading plan_hashvalue col CONCURRENCY_WAIT_TOTAL format 9999999.99 heading concwaittotal(ms) col CONCURRENCY_WAIT_delta format 9999999.99 heading concwaitdelta(ms) col CLUSTER_WAIT_DELTA format 9999999.99 heading clustwaitdelta(ms) col CLUSTER_WAIT_TOTAL format 9999999.99 heading clustwaittotal(ms) col APWAIT_TOTAL format 9999 heading applwaittimetotal(micro) col APWAIT_DELTA format 9999 heading applwaittimedelta(micro) col PLSEXEC_TIME_TOTAL format 9999 heading plsqlexectimetotal(micro) col PLSEXEC_TIME_DELTA format 9999 heading plsqlexectimedelta(micro) col JAVAEXEC_TIME_DELTA format 9999 heading javaexectimedelta(micro) col JAVAEXEC_TIME_TOTAL format 9999 heading javaexectimetotal(micro) col optimizer_cost format 9999 heading optcostcol optimizer_mode format a10 heading optimmode col kept_versions format 999 heading keptvers col invalidations_total format 999 heading invalidtot col invalidations_delta format 999 heading invaliddlt col parse_calls_total format 99999 heading parsecallstotal col parse_calls_delta format 99999 heading parsecallsdelta col executions_total format 999999 heading exectotal col executions_delta format 999999 heading execdelta col fetches_total format 9999999 heading fetchestotal col fetches_delta format 9999999 heading fetchesdelta col end_of_fetch_count_total format 9999 heading endoffetchcalltotal col end_of_fetch_count_delta format 9999 heading endoffetchcalldelta col buffer_gets_total format 99999999 heading buffergetstotal col buffer_gets_delta format 99999999 heading buffergetsdelta col disk_reads_total format 999999 heading diskreadstotal col disk_reads_delta format 9999999 heading diskreadsdelta col rows_processed_total format 9999999 heading rowsprocessedtotal col rows_processed_delta format 9999999 heading rowsprocesseddelta col rows_ex format 999999 heading rowsexeccol snap_id format 99999 heading snapid col ela_ex format 9999999.99 heading elapsedperexecution col cwt_ex format 9999999.99 heading cwtperexecution col instance_number format 99 heading inID select sql_id, plan_hash_value,dba_hist_sqlstat.snap_id, to_char(dba_hist_snapshot.BEGIN_INTERVAL_TIME,'dd-mm_hh24:mi') snap_beg,dba_hist_sqlstat.instance_number,invalidations_delta, parse_calls_delta,executions_delta,fetches_delta,buffer_gets_delta, disk_reads_delta,rows_processed_delta,elapsed_time_delta/1000 elapsed_time_delta,iowait_delta/1000 iowait_delta,clwait_delta/1000 cluster_wait_delta,ccwait_delta/1000 concurrency_wait_delta,optimizer_mode, optimizer_cost, substr(optimizer_mode,1,3) opt, case when executions_delta = 0 then NULL when rows_processed_delta = 0 then NULL else(rows_processed_delta/executions_delta)end rows_ex, case when executions_delta = 0 then NULL when clwait_delta = 0 then NULL else(clwait_delta/executions_delta)/1000 end cwt_ex, case when executions_delta = 0 then NULL when elapsed_time_delta = 0 then NULL else(elapsed_time_delta/executions_delta)/1000 end ela_ex from dba_hist_sqlstat, dba_hist_snapshot where sql_id='88z8ja83k19xx' and plan_hash_value='762764171' and dba_hist_sqlstat.snap_id=dba_hist_snapshot.snap_id and dba_hist_sqlstat.instance_number=dba_hist_snapshot.instance_number order by dba_hist_sqlstat.instance_number,dba_hist_sqlstat.snap_id / set lines 100 pages 100 undefine SQL_ID undefine PLAN_HASH_VALUE undefine SQL_TEXTselect * from table(dbms_xplan.display_awr('60aq76cdwUUU',124844,FORMAT => 'TYPICAL -ROWS -BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE')); select * from table(dbms_xplan.display_awr('60aq76cdwUUU',7311153,FORMAT => 'TYPICAL -ROWS -BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE'));Sql plan change !!https://anuj-singh.blogspot.com/2024/=====define 1='9px0f4mv0tn0t' DEFINE 3="timestamp'2025-04-15 08:00:00'" DEFINE 4="timestamp'2025-04-15 09:00:00'" SET LINESIZE 999 PAGESIZE 5000 TRIMOUT ON TRIMSPOOL ON COL asqlmon_operation HEADING Plan_Operation FORMAT A72 COL asqlmon_predicates HEAD PREDICATES FOR a100 word_wrap COL obj_alias_qbc_name FOR a40 COL options FOR a30 COL asqlmon_plan_hash_value HEAD PLAN_HASH_VALUE FOR 99999999999 COL asqlmon_sql_id HEAD SQL_ID COL asqlmon_sql_child HEAD CHILD FOR 999999 COL pct_child HEAD "Activity %" FOR A8 COL pct_child_vis HEAD "Visual" FOR A12 COL asqlmon_id HEAD "ID" FOR 9999 COL asqlmon_parent_id HEAD "PID" FOR 9999 BREAK ON asqlmon_sql_id SKIP 1 ON asqlmon_sql_child SKIP 1 ON asqlmon_plan_hash_value SKIP 1 ON asqlmon_operation WITH sample_times AS ( select * from dual ), sq AS ( SELECT count(*) samples , ash.sql_id , ash.sql_child_number , ash.sql_plan_hash_value , NVL(ash.sql_plan_line_id,1) sql_plan_line_id -- this is because simple "planless" operations like single-row insert , ash.sql_plan_operation , ash.sql_plan_options , ash.session_state , ash.event -- , AVG(ash.p3) avg_p3 -- p3 is sometimes useful for listing block counts for IO wait events FROM v$active_session_history ash WHERE 1=1 --AND ash.sql_id LIKE '&1' --AND ash.sql_child_number LIKE '&2' AND ash.sample_time BETWEEN &3 AND &4 GROUP BY ash.sql_id , ash.sql_child_number , ash.sql_plan_hash_value , NVL(ash.sql_plan_line_id,1) , ash.sql_plan_operation , ash.sql_plan_options , ash.session_state , ash.event ) SELECT -- plan.sql_id asqlmon_sql_id plan.child_number asqlmon_sql_child , plan.plan_hash_value asqlmon_plan_hash_value , sq.samples seconds , LPAD(TO_CHAR(ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 100, 1), 999.9)||' %',8) pct_child , '|'||RPAD( NVL( LPAD('#', ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 10), '#'), ' '), 10,' ')||'|' pct_child_vis --, LPAD(plan.id,4)||CASE WHEN parent_id IS NULL THEN ' ' ELSE ' <- ' END||LPAD(plan.parent_id,4) asqlmon_plan_id , plan.id asqlmon_id , plan.parent_id asqlmon_parent_id , LPAD(' ', depth, ' ') || plan.operation ||' '|| plan.options || NVL2(plan.object_name, ' ['||plan.object_name ||']', null) asqlmon_operation , sq.session_state , sq.event -- , sq.avg_p3 , plan.object_alias || CASE WHEN plan.qblock_name IS NOT NULL THEN ' ['|| plan.qblock_name || ']' END obj_alias_qbc_name -- , CASE WHEN plan.access_predicates IS NOT NULL THEN '[A:] '|| SUBSTR(plan.access_predicates,1,1990) END || CASE WHEN plan.filter_predicates IS NOT NULL THEN ' [F:] ' || SUBSTR(plan.filter_predicates,1,1990) END asqlmon_predicates -- , plan.projection FROM v$sql_plan plan , sq WHERE 1=1 AND sq.sql_id(+) = plan.sql_id AND sq.sql_child_number(+) = plan.child_number AND sq.sql_plan_line_id(+) = plan.id AND sq.sql_plan_hash_value(+) = plan.plan_hash_value AND plan.sql_id LIKE '&1' --AND plan.child_number LIKE '&2' ORDER BY plan.child_number , plan.plan_hash_value , plan.id /define 1='4v56ck8pab6xx' alter session set nls_date_format='dd-mm-yyyy hh24:mi'; set linesize 500 pagesize 300 select LAST_ACTIVE_TIME,st.inst_id as "INST", st.con_id, st.executions as execs, st.sql_id, st.plan_hash_value as plan, round(st.elapsed_time/decode(st.executions,0,1,st.executions)) as ela_per_exec, round(st.cpu_time/decode(st.executions,0,1,st.executions)) as cpu_per_exec, round(st.buffer_gets/decode(st.executions,0,1,st.executions)) as gets_per_exec, round(st.concurrency_wait_time/decode(st.executions,0,1,st.executions)) as conc_per_exec, round(st.cluster_wait_time/decode(st.executions,0,1,st.executions)) as clu_per_exec, round(st.user_io_wait_time/decode(st.executions,0,1,st.executions)) as uio_per_exec, round(st.physical_read_bytes/decode(st.executions,0,1,st.executions)/1024/1024) as read_mb_per_exec, round(st.physical_read_requests/decode(st.executions,0,1,st.executions)) as reads_per_exec, round(st.disk_reads/decode(st.executions,0,1,st.executions)/1024/1024) as disk_reads_per_exec, round(st.physical_write_bytes/decode(st.executions,0,1,st.executions)/1024/1024) as writes_mb_per_exec, round(st.physical_write_requests/decode(st.executions,0,1,st.executions)) as writes_per_exec, round(st.direct_writes/decode(st.executions,0,1,st.executions)) as direct_writes_per_exec, round(st.rows_processed/decode(st.executions,0,1,st.executions)) as rows_per_exec, round(st.px_servers_executions/decode(st.executions,0,1,st.executions)) as px_per_exec from gv$sqlstats st where sql_id in ('&&1') /define sql_id=' ' col apwpx format 99999.999 head 'AppWms|PerX' col bgpx format 999999999 head 'BGets|PerX' col conw format 99999.999 head 'Conwms|PerX' col cpx format 99999999.999 head 'CPUms|PerX' col drpx format 999999999999.99 head 'DReads|PerX' col fetchx format 99999 head 'Fetchs|PerX' col sortx format 999 head 'Sorts|PerX' col elpx format 99999999.999 head 'Elapms|PerX' col exec format 999999999999 head 'Execs' col iowpx format 99999999.999 head 'IOWms|PerX' col latime format a11 head 'Last Active' col lltime format a09 head 'Last Load' col maxsnapid format 999999 head 'Max|SnapId' col m format a01 trunc col minsnapid format 999999 head 'Min|SnapId' col module format a14 trunc head 'Module' col o format a01 head 'O' trunc col opt_mode format a08 trunc head 'Opt|Mode' trunc col parse_usr format a08 head 'ParsUser' trunc col phash format 9999999999 head 'PlanHash' col phashp format a12 head 'PlanHash P' col rwpx format 9999999999.99 head 'RwsP|PerX' col s_cn format a07 head 'S:Child' trunc col sql_id format a15 head 'SQL Id' col sqltext format a12 trunc head 'Sql Text' col ue format 999 col cpct format 999 head 'CPU|Pct' trunc col ipct format 999 head 'IO|Pct' trunc col btime format a11 head 'Begin Time' col smem format 99999 head 'ShrMem|KB' col FORCE_MATCHING_SIGNATURE for 999999999999999999999999999999 break on sql_id on opt_mode on parse_usr skip 1 prompt prompt ===== from dba_hist_sqlstat ===== select parsing_schema_name parse_usr ,plan_hash_value phash ,optimizer_mode opt_mode ,min(snap_id) minsnapid ,max(snap_id) maxsnapid ,avg(sharable_mem/1024) smem ,sum(decode(executions_delta,0,1,executions_delta)) exec ,sum(cpu_time_delta)/sum(decode(executions_delta,0,1,executions_delta))/1000 cpx ,sum(elapsed_time_delta)/sum(decode(executions_delta,0,1,executions_delta))/1000 elpx ,(sum(cpu_time_delta)/sum(decode(elapsed_time_delta,0,1,elapsed_time_delta)))*100 cpct ,sum(buffer_gets_delta)/sum(decode(executions_delta,0,1,executions_delta)) bgpx ,sum(iowait_delta)/sum(decode(executions_delta,0,1,executions_delta))/1000 iowpx ,(sum(iowait_delta)/sum(decode(elapsed_time_delta,0,1,elapsed_time_delta)))*100 ipct ,sum(disk_reads_delta)/sum(decode(executions_delta,0,1,executions_delta)) drpx ,sum(apwait_delta)/sum(decode(executions_delta,0,1,executions_delta))/1000 apwpx ,sum(ccwait_delta)/sum(decode(executions_delta,0,1,executions_delta))/1000 conw ,sum(rows_processed_delta)/sum(decode(executions_delta,0,1,executions_delta)) rwpx ,sum(sorts_delta)/sum(decode(executions_delta,0,1,executions_delta)) sortx ,sum(end_of_fetch_count_delta)/sum(decode(executions_delta,0,1,executions_delta)) fetchx ,module module ,sql_id ,FORCE_MATCHING_SIGNATURE ,SQL_PROFILE from dba_hist_sqlstat ss where sql_id = '&&sql_id' group by sql_id ,parsing_schema_name ,plan_hash_value ,optimizer_mode ,module ,FORCE_MATCHING_SIGNATURE ,SQL_PROFILE having sum(decode(executions_delta,0,1,executions_delta)) >0 order by min(ss.snap_id),parsing_schema_name ; define sql_id=' ' set linesize 500 pagesize 300 col PARSE_USR for A14 col SQLTEXT for a50 col SQL_PROFILE for a27 col FORCE_MATCHING_SIGNATURE for 9999999999999999999999999 break on parse_usr prompt ========== from v$sql ========== select con_id, parsing_schema_name parse_usr ,lpad(plan_hash_value,10,' ')||' '|| case when sql_profile is not null then 'P' when sql_plan_baseline is not null then 'B' else ' ' end phashp --,is_bind_sensitive||is_bind_aware||is_shareable pas --,optimizer_mode m --,is_obsolete o ,users_executing ue ,substr(object_status,1,1)||':'||lpad(child_number,5,' ') s_cn --,to_char(to_date(last_load_time,'YYYY-MM-DD/HH24:MI:SS'),'MMDD HH24MI') lltime ,to_char(last_active_time,'MMDD HH24:MI') latime ,(sysdate-last_active_time)*1440 min_ago ,decode(executions,0,1,executions) exec ,cpu_time/decode(executions,0,1,executions)/1000 cpx ,elapsed_time/decode(executions,0,1,executions)/1000 elpx ,(cpu_time/elapsed_time)*100 cpct ,buffer_gets/decode(executions,0,1,executions) bgpx ,user_io_wait_time/decode(executions,0,1,executions)/1000 iowpx ,(user_io_wait_time/elapsed_time)*100 ipct ,disk_reads/decode(executions,0,1,executions) drpx ,application_wait_time/decode(executions,0,1,executions)/1000 apwpx ,concurrency_wait_time/decode(executions,0,1,executions)/1000 conw ,rows_processed/decode(executions,0,1,executions) rwpx ,sorts/decode(executions,0,1,executions) sortx ,fetches/decode(executions,0,1,executions) fetchx ,module module ,SQL_PROFILE ,FORCE_MATCHING_SIGNATURE ,replace(sql_text,chr(13)) sqltext from v$sql where sql_id = '&&sql_id' --and last_active_time > sysdate-3 order by parsing_schema_name ,last_active_time ;stale_stats stats for SQL define sql_id=' ' set pagesize 100 linesize 300 set trims off tab off verify off column table_name format a50 column index_name format a50 column object_type format a40 column owner format a40 with plan_tables as ( select distinct object_name,object_owner, object_type from v$sql_plan where object_type like 'TABLE%' and sql_id = '&sql_id') select t.object_owner owner, t.object_name table_name, t.object_type object_type, decode(stale_stats,'NO','OK',NULL, 'NO STATS!', 'STALE!') staleness from dba_tab_statistics s, plan_tables t where s.table_name = t.object_name and s.owner = t.object_owner and s.partition_name is null and s.subpartition_name is null order by t.object_owner, t.object_name; set linesize 300 PROMPT ========== PROMPT Indexes PROMPT ========== with plan_indexes as ( select distinct object_name,object_owner, object_type from v$sql_plan where object_type like 'INDEX%' and sql_id = '&sql_id') select i.object_owner owner, i.object_name index_name, i.object_type object_type, decode(stale_stats,'NO','OK',NULL, 'NO STATS!', 'STALE!') staleness, TABLE_NAME from dba_ind_statistics s, plan_indexes i where s.index_name = i.object_name and s.owner = i.object_owner and s.partition_name is null and s.subpartition_name is null order by i.object_owner, i.object_name;--from History define sql_id='' set pagesize 100 linesize 300 set trims off tab off verify off column table_name format a30 column index_name format a27 column object_type format a10 column owner format a15 with plan_tables as ( select distinct object_name,object_owner, object_type from DBA_HIST_SQL_PLAN where object_type like 'TABLE%' and sql_id = '&sql_id') select t.object_owner owner, t.object_name table_name, t.object_type object_type, decode(stale_stats,'NO','OK',NULL, 'NO STATS!', 'STALE!') staleness from dba_tab_statistics s, plan_tables t where s.table_name = t.object_name and s.owner = t.object_owner and s.partition_name is null and s.subpartition_name is null order by t.object_owner, t.object_name;set linesize 300 pagesize 0 SELECT 'exec dbms_stats.gather_'||object_type||'_stats('''||object_owner||''','''||object_name||''',degree => DBMS_STATS.DEFAULT_DEGREE,estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE);' from (select distinct p.object_owner, p.object_type, p.object_name, s.stale_stats, i.stale_stats from gv$sql_plan p left outer join dba_tab_statistics s on s.owner=p.object_owner and p.object_name=s.table_name and p.object_type='TABLE' left outer join dba_ind_statistics i on i.owner=p.object_owner and p.object_name=i.index_name and p.object_type='INDEX' where 1=1 and p.sql_id = ('&sql_id') --and p.object_owner='' and (s.stale_stats='YES' or i.stale_stats='YES')); set pagesize 100*************************************************************************************** set linesize 300 pagesize 300 col SQL_OPNAME for a20 SELECT /*+ parallel(8) */ top_level_sql_id, sql_id, sql_opname, COUNT(*) FROM dba_hist_active_sess_history WHERE 1=1 -- top_level_sql_id = 'top_level_sql_id' AND snap_id > (select max(snap_id) -1 from dba_hist_snapshot ) GROUP BY top_level_sql_id, sql_id, sql_opname ORDER BY COUNT(*) DESC;==================================================sql text==============================define SQL_ID='dkgw64jygcmtf' set serveroutput on whenever sqlerror continue SQL.SQLCODE; DECLARE myReport CLOB; l_offset number := 1; ------------------------------- procedure printCLOB (p_clob in out nocopy clob) is i_offset number := 1; i_amount integer; i_clob_length number := dbms_lob.getlength(p_clob); lc_buffer varchar2(32767); begin if ( dbms_lob.isopen(p_clob) != 1 ) then dbms_lob.open(p_clob, 0); end if; DBMS_OUTPUT.ENABLE (buffer_size => NULL); LOOP BEGIN i_amount := 32676 ; DBMS_LOB.READ ( lob_loc => p_clob, amount => i_amount, offset => i_offset, buffer => lc_buffer); dbms_output.put_line(lc_buffer ); i_offset := i_offset + i_amount; exception when no_data_found then exit; end; END LOOP; dbms_lob.close(p_clob); exception when others then dbms_output.put_line('Error : '||sqlerrm); end printCLOB; --------------------------- BEGIN dbms_output.put_line( chr(10) || chr(10) || 'Looking for sql text for &SQL_ID : ' || chr(10) || chr(10) ); BEGIN SELECT sql_fulltext INTO myReport FROM gv$sqlarea WHERE sql_id = '&SQL_ID' AND ROWNUM = 1; EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('Not found in Cursor Cache'); END; if ( dbms_lob.getlength( myReport ) != 0 ) Then dbms_output.put_line('Found &SQL_ID in cursor cache.'); ELSE BEGIN select sql_text into myReport from dba_hist_sqltext where sql_id = '&SQL_ID' and rownum = 1; EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('Not found in AWR'); END; IF ( dbms_lob.getlength( myReport ) != 0 ) Then dbms_output.put_line('Found &SQL_ID in AWR.'); END IF; END IF; IF ( dbms_lob.getlength( myReport ) != 0 ) THEN DBMS_OUTPUT.PUT_LINE( '>>' ); printCLOB( myReport ); DBMS_OUTPUT.PUT_LINE( '>>' ); ELSE dbms_output.put_line('The sql text for &SQL_ID could not be found'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(Substr(SQLERRM,1,255)); raise; END; ================= Looking for sql text for dkgw64jygcmtf : Found dkgw64jygcmtf in cursor cache. >> select /*+ monitor parallel(2) */ count(*) from dba_segments >> PL/SQL procedure successfully completed.********set head off verify off echo off pages 1500 linesize 100 feedback off alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS'; select 'SQL_ID.........................................: '|| SQL_ID , 'OBJECT_STATUS..................................: '|| OBJECT_STATUS , 'LITERAL_HASH_VALUE.............................: '|| LITERAL_HASH_VALUE , 'LAST_LOAD_TIME.................................: '||LAST_LOAD_TIME , 'IS_OBSOLETE....................................: '||IS_OBSOLETE , 'IS_BIND_SENSITIVE..............................: '||IS_BIND_SENSITIVE , 'IS_BIND_AWARE .................................: '||IS_BIND_AWARE , 'IS_SHAREABLE...................................: '||IS_SHAREABLE , 'CHILD_LATCH....................................: '||CHILD_LATCH , 'SQL_PROFILE....................................: '||SQL_PROFILE , 'SQL_PATCH......................................: '||SQL_PATCH , 'SQL_PLAN_BASELINE..............................: '||SQL_PLAN_BASELINE , 'LAST_ACTIVE_TIME ..............................: '||LAST_ACTIVE_TIME , 'IS_REOPTIMIZABLE...............................: '||nvl(IS_REOPTIMIZABLE ,'x') , 'IS_RESOLVED_ADAPTIVE_PLAN......................: '||IS_RESOLVED_ADAPTIVE_PLAN , 'DDL_NO_INVALIDATE..............................: '||DDL_NO_INVALIDATE , 'IS_ROLLING_INVALID.............................: '||IS_ROLLING_INVALID , 'RESULT_CACHE...................................: '||RESULT_CACHE, 'AVOIDED_EXECUTIONS.............................: '||AVOIDED_EXECUTIONS, 'SQL_TEXT.......................................: '||SQL_TEXT from gv$sql where sql_id='&sql_id';SET LINESIZE 300 PAGESIZE 300 COLUMN sql_text FORMAT a50 SELECT 'SELECT * FROM table(version_rpt(''' || a.sql_id || '''));' AS version_query, a.cursors, a.sql_id, b.sql_text FROM ( SELECT COUNT(*) AS cursors, ssc.sql_id FROM v$sql_shared_cursor ssc GROUP BY ssc.sql_id ) a JOIN ( SELECT sa.sql_id, sa.sql_text FROM v$sqlarea sa WHERE PARSING_SCHEMA_NAME = 'xx' -- AND sql_id = '4dc6768sk8z2' ) b ON a.sql_id = b.sql_id WHERE a.cursors > 100 ORDER BY a.cursors DESC; ********************************define sql_id='XXXXXXX' define 2='%' col PLAN_TABLE_OUTPUT for a150 select * from table(dbms_xplan.display_cursor('&sql_id',CASE WHEN '&2' = '%' THEN null ELSE '&2' END,'ALLSTATS LAST +COST +ROWS +ADAPTIVE +PEEKED_BINDS +PARTITION')); select * from table(dbms_xplan.display_cursor('&sql_id',CASE WHEN '&2' = '%' THEN null ELSE '&2' END,'ALLSTATS LAST +COST +ROWS +PEEKED_BINDS +PARTITION')); alter session set "_rowsource_execution_statistics" = TRUE; col PLAN_TABLE_OUTPUT for a200 SELECT * FROM TABLE(dbms_xplan.display_cursor(sql_id => 'xxxxxxxx', cursor_child_no => 0, FORMAT => 'ADVANCED +ROWS +BYTES +COST +PARALLEL +PARTITION +IOSTATS +MEMSTATS +ALIAS +PEEKED_BINDS +OUTLINE +PREDICATE -PROJECTION +REMOTE +NOTE'));define 1='' define 2='' define sql_id='XXXXXXXX' set linesize 300 pagesize 300 column xms_child_number heading Ch|ld format 9 print column xms_id heading Op|ID format 999 column xms_id2 heading Op|ID format a6 column xms_pred heading Pr|ed format a2 column xms_optimizer heading Optimizer|Mode format a10 column xms_plan_step heading Operation for a55 column xms_object_name heading Objcect|Name for a30 column xms_opt_cost heading Optimizer|Cost for 99999999999999 column xms_opt_card heading "Optim rows|from step" for 99999999999999 column xms_opt_bytes heading "Optim bytes|from step" for 99999999999999 column xms_predicate_info heading "Predicate Information (identified by operation id):" format a100 word_wrap break on xms_child_number skip 1 select child_number xms_child_number, case when access_predicates is not null then 'A' else ' ' end || case when filter_predicates is not null then 'F' else ' ' end xms_pred, id xms_id, lpad(' ',depth*1,' ')||operation || ' ' || options xms_plan_step, object_name xms_object_name, -- search_columns, cost xms_opt_cost, cardinality xms_opt_card, bytes xms_opt_bytes, optimizer xms_optimizer -- other_tag, -- other, -- distribution, -- access_predicates, -- filter_predicates from v$sql_plan where 1=1 and sql_id='&sql_id' -- hash_value in (&1) --and to_char(child_number) like '&2' -- to_char is just used for convenient filtering using % for all children / select * from ( select child_number xms_child_number, lpad(id, 5, ' ') xms_id2, ' - access('|| substr(access_predicates,1,3989) || ')' xms_predicate_info from v$sql_plan where 1=1 -- hash_value in (&1) --and to_char(child_number) like '&2' and sql_id='&sql_id' and access_predicates is not null union all select child_number, lpad(id, 5, ' ') xms_id2, ' - filter('|| substr(filter_predicates,1,3989) || ')' xms_predicate_info from v$sql_plan where 1=1 and sql_id='&sql_id' -- hash_value in (&1) -- and to_char(child_number) like '&2' and filter_predicates is not null ) order by xms_child_number, xms_id2 /************************************define sql_id='xxxxxxxxxxxx' set pagesize 300 linesize 300 set trims off set tab off set verify off column table_name format a50 column index_name format a50 column object_type format a40 column owner format a40 col OWNER for a12 --accept sql_id prompt 'Enter the SQL ID: ' PROMPT ========== PROMPT Tables PROMPT ========== with plan_tables as ( select distinct object_name,object_owner, object_type from v$sql_plan where object_type like 'TABLE%' and sql_id = '&sql_id') select t.object_owner owner, t.object_name table_name, t.object_type object_type, decode(stale_stats,'NO','OK',NULL, 'NO STATS!', 'STALE!') staleness from dba_tab_statistics s, plan_tables t where s.table_name = t.object_name and s.owner = t.object_owner and s.partition_name is null and s.subpartition_name is null order by t.object_owner, t.object_name; PROMPT ========== PROMPT Indexes PROMPT ========== with plan_indexes as ( select distinct object_name,object_owner, object_type from v$sql_plan where object_type like 'INDEX%' and sql_id = '&sql_id') select i.object_owner owner, i.object_name index_name, i.object_type object_type, decode(stale_stats,'NO','OK',NULL, 'NO STATS!', 'STALE!') staleness from dba_ind_statistics s, plan_indexes i where s.index_name = i.object_name and s.owner = i.object_owner and s.partition_name is null and s.subpartition_name is null order by i.object_owner, i.object_name;************************************set linesize 300 pagesize 300 def xbi_sql_id='XXXXX' SELECT * FROM TABLE(SELECT DBMS_XPLAN.DISPLAY_CURSOR(sql_id,sql_child_number,'+PEEKED_BINDS +PARALLEL +PARTITION ALLSTATS LAST') FROM v$session WHERE 1=1 and sql_id IN ('&xbi_sql_id')) --and sid= ; from https://github.com/tanelpoder/tpt-oracle/blob/master/xbi.sql -- Copyright 2018 Tanel Poder. All rights reserved. More info at http://tanelpoder.com -- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions. -------------------------------------------------------------------------------- -- -- File name: xb (eXplain Better) -- -- Purpose: Explain a SQL statements execution plan with execution -- profile directly from library cache - for the last -- SQL executed in current session (see also xbi.sql) -- -- Author: Tanel Poder -- Copyright: (c) https://blog.tanelpoder.com -- -- Usage: 1) alter session set statistics_level = all; -- 2) Run the statement you want to explain -- 3) @xb.sql -- -- Other: You can add a GATHER_PLAN_STATISTICS hint to the statement instead -- if you dont want to use "alter session set statistics_level" -- for some reason (this hint works on Oracle 10.2 and higher) -- -- TODO: Noteworthy outstanding items are: -- * formatting, decide what columns to show by default -- * clone to an @xbx.sql (eXtended version) with wider output and stuff like plan outline hints shown etc -- currently you can just comment/uncomment sections in this script -- * clone to @xbad.sql for showing full adaptive plans -- * add an option to gather/report metrics of all PX slaves -- -------------------------------------------------------------------------------- def xbi_sql_id='5u4sk8mw75cas' def xbi_sql_child_number='' def xbi_sql_addr='' prompt -- xbi.sql: eXplain Better v1.01 for sql_id=&xbi_sql_id child=&xbi_sql_child_number - by Tanel Poder (https://blog.tanelpoder.com) set verify off pagesize 5000 tab off lines 999 column xbi_child_number heading "Ch|ld" format 999 column xbi_sql_id heading "SQL_ID" for a13 column xbi_sql_child_number heading "CHLD" for 9999 column xbi_sql_addr heading "ADDRESS" column xbi_sql_id_text heading "" column xbi_seconds_ago heading "First Load Time" column xbi_id heading "Op|ID" for 9999 justify right column xbi_parent_id heading "Par.|ID" for 9999 justify right column xbi_id2 heading "Op|ID" for a5 justify right column xbi_pred heading "Pred|#Col" for a5 justify right column xbi_pos heading "#Sib|ling" for 9999 column xbi_optimizer heading "Optimizer|Mode" format a10 column xbi_plan_step heading "Operation" for a55 column xbi_plan_line heading "Row Source" for a72 column xbi_qblock_name heading "Query Block|name" for a20 PRINT column xbi_object_name heading "Object|Name" for a30 column xbi_object_node heading "Object|Node" for a10 column xbi_opt_cost heading "Optimizer|Cost" for 9999999999 column xbi_opt_card heading "Est. rows|per Start" for 999999999999 column xbi_opt_card_times_starts heading "Est. rows|total" for 999999999999 column xbi_opt_card_misestimate heading "Opt. Card.|misestimate" for a15 justify right column xbi_opt_bytes heading "Estimated|output bytes" for 999999999999 column xbi_predicate_info heading "Predicate Information (identified by operation id):" format a100 word_wrap column xbi_cpu_cost heading "CPU|Cost" for 9999999 column xbi_io_cost heading "IO|Cost" for 9999999 column xbi_last_output_rows heading "Real #rows|returned" for 9999999999 column xbi_last_starts heading "Rowsource|starts" for 999999999 column xbi_last_rows_start heading "#Rows ret/|per start" for 999999999 column xbi_last_cr_buffer_gets heading "Consistent|gets" for 999999999 column xbi_last_cr_buffer_gets_row heading "Consistent|gets/row" for 999999999 column xbi_last_cu_buffer_gets heading "Current|gets" for 999999999 column xbi_last_cu_buffer_gets_row heading "Current|gets/row" for 999999999 column xbi_last_disk_reads heading "Physical|read blks" for 999999999 column xbi_last_disk_writes heading "Physical|write blks" for 999999999 column xbi_last_elapsed_time_ms heading "cumulative ms|spent in branch" for 9,999,999.99 noprint column xbi_self_elapsed_time_ms heading "ms spent in|this operation" for 9,999,999.99 column xbi_self_cr_buffer_gets heading "Consistent|gets" for 999999999 column xbi_self_cr_buffer_gets_row heading "Consistent|gets/row" for 999999999 column xbi_self_cu_buffer_gets heading "Current|gets" for 999999999 column xbi_self_cu_buffer_gets_row heading "Current|gets/row" for 999999999 column xbi_self_disk_reads heading "Physical|read blks" for 999999999 column xbi_self_disk_writes heading "Physical|write blks" for 999999999 column xbi_last_memory_used heading "Memory|used (MB)" for 9,999,999.99 column xbi_last_execution heading "Workarea|Passes" for a13 column xbi_sql_plan_hash_value heading "Plan Hash Value" for 9999999999 column xbi_plan_hash_value_text noprint column xbi_outline_hints heading "Outline Hints" for a120 word_wrap column xbi_notes heading "Plan|Notes" for a120 word_wrap column xbi_sql_id heading "SQL_ID" for a13 new_value xbi_sql_id column xbi_sql_child_number heading "CHLD" for 9999 new_value xbi_sql_child_number column xbi_sql_addr heading "ADDRESS" new_value xbi_sql_addr set feedback off select 'Cursor: ' xbi_sql_id_text, sql.sql_id xbi_sql_id, sql.child_number xbi_sql_child_number, sql.address xbi_sql_addr, ' PLAN_HASH_VALUE: ' xbi_plan_hash_value_text, sql.plan_hash_value xbi_sql_plan_hash_value, 'Statement first parsed at: '|| sql.first_load_time ||' - '|| round( (sysdate - to_date(sql.first_load_time,'YYYY-MM-DD/HH24:MI:SS'))*86400 ) || ' seconds ago' xbi_seconds_ago from v$sql sql, all_users usr where sql.parsing_user_id = usr.user_id --and (sql.sql_id,sql.child_number) = (('&xbi_sql_id', TO_NUMBER('&xbi_sql_child_number'))) and sql.sql_id='&xbi_sql_id' and is_obsolete = 'N' / WITH sq AS ( SELECT sp.id, sp.parent_id, sp.operation, sp.options, sp.object_owner, sp.object_name , ss.last_elapsed_time, ss.last_cr_buffer_gets, ss.last_cu_buffer_gets, ss.last_disk_reads, ss.last_disk_writes FROM v$sql_plan_statistics_all ss INNER JOIN v$sql_plan sp ON ( sp.sql_id=ss.sql_id AND sp.child_number=ss.child_number AND sp.address=ss.address AND sp.id=ss.id ) AND sp.sql_id='&xbi_sql_id' -- AND sp.child_number = TO_NUMBER('&xbi_sql_child_number') -- AND sp.address = hextoraw('&xbi_sql_addr') ), deltas AS ( SELECT par.id , par.last_elapsed_time - SUM(chi.last_elapsed_time ) self_elapsed_time , par.last_cr_buffer_gets - SUM(chi.last_cr_buffer_gets) self_cr_buffer_gets , par.last_cu_buffer_gets - SUM(chi.last_cu_buffer_gets) self_cu_buffer_gets , par.last_disk_reads - SUM(chi.last_disk_reads ) self_disk_reads , par.last_disk_writes - SUM(chi.last_disk_writes ) self_disk_writes FROM sq par LEFT OUTER JOIN sq chi ON chi.parent_id = par.id GROUP BY par.id , par.last_elapsed_time, par.last_cr_buffer_gets, par.last_cu_buffer_gets, par.last_disk_reads, par.last_disk_writes ), combined AS ( SELECT sq.id, sq.parent_id, sq.operation, sq.options , sq.object_owner, sq.object_name , sq.last_elapsed_time , sq.last_cr_buffer_gets , sq.last_cu_buffer_gets , sq.last_disk_reads , sq.last_disk_writes , NVL(deltas.self_elapsed_time , sq.last_elapsed_time) self_elapsed_time , NVL(deltas.self_cr_buffer_gets , sq.last_cr_buffer_gets) self_cr_buffer_gets , NVL(deltas.self_cu_buffer_gets , sq.last_cu_buffer_gets) self_cu_buffer_gets , NVL(deltas.self_disk_reads , sq.last_disk_reads) self_disk_reads , NVL(deltas.self_disk_writes , sq.last_disk_writes) self_disk_writes FROM sq, deltas WHERE sq.id = deltas.id ), adaptive_display_map AS ( SELECT TO_NUMBER(EXTRACT(column_value, '/row/@op' )) id , TO_NUMBER(EXTRACT(column_value, '/row/@par')) parent_id , TO_NUMBER(EXTRACT(column_value, '/row/@dis')) display_id , TO_NUMBER(EXTRACT(column_value, '/row/@dep')) depth , TO_NUMBER(EXTRACT(column_value, '/row/@skp')) skip FROM v$sql_plan , TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(other_xml),'/*/display_map/row'))) WHERE sql_id = '&xbi_sql_id' --AND child_number = TO_NUMBER('&xbi_sql_child_number') -- AND address = hextoraw('&xbi_sql_addr') AND other_xml IS NOT NULL ) select LPAD( CASE WHEN p.filter_predicates IS NOT NULL THEN 'F' ELSE ' ' END || CASE WHEN p.access_predicates IS NOT NULL THEN CASE WHEN p.options LIKE 'STORAGE %' THEN 'S' ELSE 'A' END ELSE '' END || CASE p.search_columns WHEN 0 THEN NULL ELSE '#'||TO_CHAR(p.search_columns) END , 5) xbi_pred, NVL(CASE WHEN adm.skip IS NULL THEN NULL WHEN adm.skip = 0 THEN adm.display_id WHEN adm.skip = 1 THEN NULL END , p.id) xbi_id, nvl(adm2.parent_id, p.parent_id) xbi_parent_id, CASE WHEN p.id != 0 THEN p.position END xbi_pos, LPAD(' ',NVL(adm.depth,p.depth),' ')|| p.operation || ' ' || p.options ||' ' ||nvl2(p.object_name, '['||p.object_name||']', null) xbi_plan_line, CASE WHEN p.id = 0 THEN '>>> Plan totals >>>' ELSE p.qblock_name END xbi_qblock_name, -- p.object_node xbi_object_node, -- p.object_owner, -- p.object_name, -- p.object_alias, -- p.distribution xbi_distribution, -- lpad(decode(p.id,0,'T ','')||trim(to_char(round(decode(p.id,0,c.last_elapsed_time,c.self_elapsed_time) /1000,2),'9,999,999.00')), 14) xbi_self_elapsed_time_ms, round(decode(p.id,0,c.last_elapsed_time,c.self_elapsed_time) /1000,2) xbi_self_elapsed_time_ms, decode(p.id,0,c.last_cr_buffer_gets,c.self_cr_buffer_gets) xbi_self_cr_buffer_gets, ps.last_starts xbi_last_starts, ps.last_output_rows xbi_last_output_rows, p.cardinality * ps.last_starts xbi_opt_card_times_starts, regexp_replace(lpad(to_char(round( CASE WHEN (NULLIF(ps.last_output_rows / NULLIF(p.cardinality * ps.last_starts, 0),0)) > 1 THEN -(NULLIF(ps.last_output_rows / NULLIF(p.cardinality * ps.last_starts, 0),0)) WHEN (NULLIF(ps.last_output_rows / NULLIF(p.cardinality * ps.last_starts, 0),0)) < 1 THEN 1/(NULLIF(ps.last_output_rows / NULLIF(p.cardinality * ps.last_starts, 0),0)) WHEN (NULLIF(ps.last_output_rows / NULLIF(p.cardinality * ps.last_starts, 0),0)) = 1 THEN 1 ELSE null END ,0))||'x',15),'^ *x$') xbi_opt_card_misestimate, -- c.self_cr_buffer_gets xbi_self_cr_buffer_gets, -- c.self_cr_buffer_gets / DECODE(ps.last_output_rows,0,1,ps.last_output_rows) xbi_self_cr_buffer_gets_row, decode(p.id,0,c.last_cu_buffer_gets,c.self_cu_buffer_gets) xbi_self_cu_buffer_gets, -- c.self_cu_buffer_gets / DECODE(ps.last_output_rows,0,1,ps.last_output_rows) xbi_self_cu_buffer_gets_row, decode(p.id,0,c.last_disk_reads,c.self_disk_reads) xbi_self_disk_reads, decode(p.id,0,c.last_disk_writes,c.self_disk_writes) xbi_self_disk_writes, round(ps.last_elapsed_time/1000,2) xbi_last_elapsed_time_ms, -- ps.last_cr_buffer_gets xbi_last_cr_buffer_gets, -- ps.last_cr_buffer_gets / DECODE(ps.last_output_rows,0,1,ps.last_output_rows) xbi_last_cr_buffer_gets_row, -- ps.last_cu_buffer_gets xbi_last_cu_buffer_gets, -- ps.last_cu_buffer_gets / DECODE(ps.last_output_rows,0,1,ps.last_output_rows) xbi_last_cu_buffer_gets_row, -- ps.last_disk_reads xbi_last_disk_reads, -- ps.last_disk_writes xbi_last_disk_writes, ps.last_memory_used/1048576 xbi_last_memory_used, ps.last_execution xbi_last_execution, p.cost xbi_opt_cost -- p.bytes xbi_opt_bytes, -- p.cpu_cost xbi_cpu_cost, -- p.io_cost xbi_io_cost, -- p.other_tag, -- p.other, -- p.access_predicates, -- p.filter_predicates, from v$sql_plan p , v$sql_plan_statistics_all ps , combined c , adaptive_display_map adm , adaptive_display_map adm2 where p.address = ps.address (+) and p.sql_id = ps.sql_id (+) and p.plan_hash_value = ps.plan_hash_value (+) and p.child_number = ps.child_number (+) and p.id = ps.id (+) and p.id = adm.id (+) and adm.id = adm2.id (+) and (adm.id IS NULL or adm.skip = 0) and p.sql_id = '&xbi_sql_id' --and p.address = hextoraw('&xbi_sql_addr') --and p.child_number = TO_NUMBER(&xbi_sql_child_number) and ps.id = c.id (+) order by p.id asc / WITH adaptive_display_map AS ( SELECT TO_NUMBER(EXTRACT(column_value, '/row/@op' )) id , TO_NUMBER(EXTRACT(column_value, '/row/@dis')) display_id , TO_NUMBER(EXTRACT(column_value, '/row/@skp')) skip FROM v$sql_plan , TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(other_xml),'/*/display_map/row'))) WHERE sql_id = '&xbi_sql_id' -- AND child_number = TO_NUMBER('&xbi_sql_child_number') -- AND address = hextoraw('&xbi_sql_addr') AND other_xml IS NOT NULL AND TO_NUMBER(EXTRACT(column_value, '/row/@skp')) != 1 ) select xbi_id2, xbi_qblock_name, substr(dummy,1,0)||'-' " ", -- there's an ugly reason (bug) for this hack xbi_predicate_info from ( select sp.sql_id xbi_sql_id, LPAD(NVL(CASE WHEN adm.skip IS NULL THEN NULL WHEN adm.skip = 0 THEN adm.display_id WHEN adm.skip = 1 THEN NULL END , sp.id), 5, ' ') xbi_id2, sp.filter_predicates dummy, -- looks like there's a bug in 11.2.0.3 where both pred cols have to be selected CASE WHEN sp.options LIKE 'STORAGE %' THEN 'storage' ELSE 'access' END||'('|| substr(sp.access_predicates,1,3989) || ')' xbi_predicate_info, sp.qblock_name xbi_qblock_name from v$sql_plan sp , adaptive_display_map adm where sp.sql_id = '&xbi_sql_id' -- and sp.child_number = TO_NUMBER(&xbi_sql_child_number) AND sp.address = HEXTORAW('&xbi_sql_addr') and sp.access_predicates is not null and sp.id = adm.id (+) union all select sp.sql_id xbi_sql_id, --lpad(sp.id, 5, ' ') xbi_id2, LPAD(NVL(CASE WHEN adm.skip IS NULL THEN NULL WHEN adm.skip = 0 THEN adm.display_id WHEN adm.skip = 1 THEN NULL END , sp.id), 5, ' ') xbi_id2, sp.access_predicates dummy, 'filter('|| substr(sp.filter_predicates,1,3989) || ')' xbi_predicate_info, sp.qblock_name xbi_qblock_name from v$sql_plan sp , adaptive_display_map adm where sp.sql_id = '&xbi_sql_id' --and sp.child_number = TO_NUMBER(&xbi_sql_child_number) --and sp.address = HEXTORAW('&xbi_sql_addr') and sp.filter_predicates is not null and sp.id = adm.id (+) ) order by xbi_id2 asc, xbi_predicate_info asc / -- this query can return ORA-600 in Oracle 10.2 due to Bug 5497611 - OERI[qctVCO:csform] from Xquery using XMLType constructor (Doc ID 5497611.8) WITH sq AS ( SELECT other_xml FROM v$sql_plan p WHERE p.sql_id = '&xbi_sql_id' --AND p.child_number = &xbi_sql_child_number --AND p.address = hextoraw('&xbi_sql_addr') AND p.other_xml IS NOT NULL -- (the other_xml is not guaranteed to always be on plan line 1) ) SELECT ' *' " ", 'Cardinality feedback = yes' xbi_notes FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "cardinality_feedback"]') = 'yes' UNION ALL SELECT ' *', 'SQL Stored Outline used = ' ||extractvalue(xmltype(sq.other_xml), '/*/info[@type = "outline"]') FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "outline"]') IS NOT NULL UNION ALL SELECT ' *', 'SQL Patch used = ' ||extractvalue(xmltype(sq.other_xml), '/*/info[@type = "sql_patch"]') FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "sql_patch"]') IS NOT NULL UNION ALL SELECT ' *', 'SQL Profile used = ' ||extractvalue(xmltype(sq.other_xml), '/*/info[@type = "sql_profile"]') FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "sql_profile"]') IS NOT NULL UNION ALL SELECT ' *', 'SQL Plan Baseline used = ' ||extractvalue(xmltype(sq.other_xml), '/*/info[@type = "baseline"]') FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "baseline"]') IS NOT NULL UNION ALL SELECT ' *', 'Adaptive Plan = ' ||extractvalue(xmltype(sq.other_xml), '/*/info[@type = "adaptive_plan"]') FROM sq WHERE extractvalue(xmltype(sq.other_xml), '/*/info[@type = "adaptive_plan"]') IS NOT NULL / -- === Outline Hints === WITH sq AS ( SELECT other_xml FROM v$sql_plan p WHERE p.sql_id = '&xbi_sql_id' -- AND p.child_number = &xbi_sql_child_number -- AND p.address = hextoraw('&xbi_sql_addr') AND p.other_xml IS NOT NULL ) SELECT SUBSTR(EXTRACTVALUE(VALUE(d), '/hint'),1,4000) xbi_outline_hints FROM sq , TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(sq.other_xml), '/*/outline_data/hint'))) D / set feedback on PROMPTalter session set nls_date_format='DD-MM-YYYY HH24:MI:SS'; set lines 200 pages 200 SELECT CAST(begin_interval_time AS DATE) sample_time , sql_id , plan_hash_value , executions_delta executions , rows_processed_delta rows_processed , ROUND(rows_processed_delta / NULLIF(executions_delta,0)) rows_per_exec , ROUND(buffer_gets_delta / NULLIF(executions_delta,0)) lios_per_exec , ROUND(disk_reads_delta / NULLIF(executions_delta,0)) blkrd_per_exec , ROUND(cpu_time_delta / NULLIF(executions_delta,0)/1000) cpu_ms_per_exec , ROUND(elapsed_time_delta / NULLIF(executions_delta,0)/1000) ela_ms_per_exec , ROUND(iowait_delta / NULLIF(executions_delta,0)/1000) iow_ms_per_exec , ROUND(clwait_delta / NULLIF(executions_delta,0)/1000) clw_ms_per_exec , ROUND(apwait_delta / NULLIF(executions_delta,0)/1000) apw_ms_per_exec , ROUND(ccwait_delta / NULLIF(executions_delta,0)/1000) ccw_ms_per_exec FROM dba_hist_snapshot NATURAL JOIN dba_hist_sqlstat WHERE begin_interval_time > SYSDATE - 2 AND sql_id = '&1' and executions_delta > 0 ORDER BY sample_time /(SELECT name, isdefault, VALUE FROM v$sql_optimizer_env WHERE sql_id = '9dv58dbg6tt2t' AND child_number = 1) MINUS (SELECT name, isdefault, VALUE FROM v$sql_optimizer_env WHERE sql_id = '9dv58dbg6tt2t' AND child_number = 0);

6 comments:
sql report
http://anuj-singh.blogspot.com/2021/02/sql-report.html
http://anuj-singh.blogspot.com/2017/09/sql-information-from-memory.html
http://anuj-singh.blogspot.com/2014/01/what-is-sql-doing.html
set long 5000 lines 500
col sql_text for a17 wrap
select sql_id,sql_text from dba_hist_sqltext where sql_id = 'XXXXXXXX';
select sql_id,sql_text from dba_hist_sqltext where sql_text like 'INSERT%XXXXXXXXXX%';
define sql_id='8cnh50qfgwg73'
VAR sql_text CLOB;
VAR other_xml CLOB;
EXEC :sql_text := NULL;
EXEC :other_xml := NULL;
BEGIN
IF :sql_text IS NULL OR NVL(DBMS_LOB.GETLENGTH(:sql_text), 0) = 0 THEN
SELECT REPLACE(sql_text, CHR(00), ' ')
INTO :sql_text
FROM dba_hist_sqltext
WHERE sql_id = TRIM('&sql_id')
AND sql_text IS NOT NULL
AND ROWNUM = 1;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('getting sql_text from awr: '||SQLERRM);
:sql_text := NULL;
END;
/
print sql_text
define sql_id='8cnh50qfgwg73'
VAR sql_text CLOB;
VAR other_xml CLOB;
EXEC :sql_text := NULL;
EXEC :other_xml := NULL;
DECLARE
l_sql_text VARCHAR2(32767);
BEGIN
FOR i IN (SELECT DISTINCT piece, sql_text
FROM gv$sqltext_with_newlines
WHERE sql_id = TRIM('&sql_id')
ORDER BY 1, 2)
LOOP
IF :sql_text IS NULL THEN
DBMS_LOB.CREATETEMPORARY(:sql_text, TRUE);
DBMS_LOB.OPEN(:sql_text, DBMS_LOB.LOB_READWRITE);
END IF;
l_sql_text := REPLACE(i.sql_text, CHR(00), ' ');
DBMS_LOB.WRITEAPPEND(:sql_text, LENGTH(l_sql_text), l_sql_text);
END LOOP;
IF :sql_text IS NOT NULL THEN
DBMS_LOB.CLOSE(:sql_text);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('getting sql_text from memory: '||SQLERRM);
:sql_text := NULL;
END;
/
print sql_text
set lines 500 set pages 300
col table_name for a40
col owner for a30
select distinct owner, table_name, STALE_STATS, last_analyzed, stattype_locked
from dba_tab_statistics
where (owner, table_name) in
(select distinct owner, table_name
from dba_tables
where ( table_name)
in ( select object_name
from gv$sql_plan
where upper(sql_id) = upper('&sql_id') and object_name is not null))
/
Post a Comment