@@header

/*
*
*  Author        : Vishal Gupta
*  Purpose       : Display Session's SQL IDs from ASH
*  Compatibility : 10.1 and above
*  Parameters    : 1 - SID
*                  2 - INST_ID         (optional, default to 1)
*                  3 - Serial#         (Default %)
*                  4 - Number of hours (Default 24)
*                  5 - SQL_ID          (Default %)
*                  6 - WhereClause     
*                  
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  02-JUL-15  Vishal Gupta  Added logic to sum up temp/page space for all parallel slaves
*  21-JAN-14  Vishal Gupta  Added SQL_ID and whereclause as input parameter
*  28-JUN-12  Vishal Gupta  Created
*
*/


/************************************
*  INPUT PARAMETERS
************************************/
UNDEFINE SID
UNDEFINE INST_ID
UNDEFINE SERIAL
UNDEFINE HOURS
UNDEFINE SQL_ID
UNDEFINE WHERECLAUSE
UNDEFINE WHERECLAUSE2
UNDEFINE HAVINGCLAUSE

DEFINE SID="&&1"
DEFINE INST_ID="&&2"
DEFINE SERIAL="&&3"
DEFINE HOURS="&&4"
DEFINE SQL_ID="&&5"
DEFINE WHERECLAUSE="&&6"
DEFINE WHERECLAUSE2="&&7"
DEFINE HAVINGCLAUSE="&&8"

COLUMN  _SID          NEW_VALUE  SID         NOPRINT
COLUMN  _INST_ID      NEW_VALUE  INST_ID     NOPRINT
COLUMN  _SERIAL       NEW_VALUE  SERIAL      NOPRINT
COLUMN  _HOURS        NEW_VALUE  HOURS       NOPRINT
COLUMN  _SQL_ID       NEW_VALUE  SQL_ID       NOPRINT

set term off
SELECT DECODE(UPPER('&&SID'),'','%',UPPER('&&SID'))         "_SID"
     , DECODE(UPPER('&&INST_ID'),'','1',UPPER('&&INST_ID')) "_INST_ID"
     , DECODE(UPPER('&&SERIAL'),'','%',UPPER('&&SERIAL'))   "_SERIAL"
     , DECODE(UPPER('&&HOURS'),'','24',UPPER('&&HOURS'))    "_HOURS"
     , DECODE('&&SQL_ID','','%','&&SQL_ID')                 "_SQL_ID"
FROM DUAL;
set term on
/***********************************/


/************************************
*  CONFIGURATION PARAMETERS
************************************/


DEFINE COUNT_FORMAT=99,999
--DEFINE COUNT_DIVIDER="1"
--DEFINE COUNT_HEADING="#"
DEFINE COUNT_DIVIDER="1000"
DEFINE COUNT_HEADING="#1000"


DEFINE BYTES_FORMAT=99,999
--DEFINE BYTES_DIVIDER="1024"
--DEFINE BYTES_HEADING="KB"
--DEFINE BYTES_DIVIDER="1024/1024"
--DEFINE BYTES_HEADING="MB"
DEFINE BYTES_DIVIDER="1024/1024/1024"
DEFINE BYTES_HEADING="GB"

/************************************/



PROMPT ***************************************************
PROMPT * ASH SQL Ids 
PROMPT *
PROMPT * Input Parameter:
PROMPT *   SID             = "&&SID"
PROMPT *   Instance ID     = "&&INST_ID"
PROMPT *   Serial#         = "&&SERIAL"
PROMPT *   Number of Hours = "&&HOURS"
PROMPT *   SQL Id          = "&&SQL_ID"
PROMPT *   Where Clause    = "&&WHERECLAUSE"
PROMPT *   Where Clause2   = "&&WHERECLAUSE2"
PROMPT *   Having Clause   = "&&HAVINGCLAUSE"
PROMPT ***************************************************

COLUMN session_id                 HEADING "SID"                                    FORMAT 99999
COLUMN instance_number            HEADING "I#"                                     FORMAT 99
COLUMN inst_id                    HEADING "I#"                                     FORMAT 99
COLUMN "session_serial#"          HEADING "Serial#"                                FORMAT 999999
COLUMN FORCE_MATCHING_SIGNATURE   HEADING "Force|Matching|Signature"               FORMAT 99999999999999999999999
COLUMN sql_plan_hash_value        HEADING "Plan|Hash|Value"                        FORMAT 9999999999 
COLUMN sql_exec_start                                                              FORMAT a18
COLUMN sql_exec_end                                                                FORMAT a18
COLUMN duration                   HEADING "Duration|+D HH:MM:SS"                   FORMAT a12
COLUMN sql_opname                 HEADING "SQL|Operation"                          FORMAT a10 TRUNCATE
COLUMN sql_child_number           HEADING "SQL|Ch#"                                FORMAT 999
COLUMN current_dop                HEADING "DOP"                                    FORMAT 999
COLUMN phyread_requests           HEADING "Phy|Read|Reqs|(&&COUNT_HEADING)"        FORMAT 999999
COLUMN phywrite_requests          HEADING "Phy|Write|Reqs|(&&COUNT_HEADING)"       FORMAT 999999
COLUMN phyread                    HEADING "Phy|Read|(&&BYTES_HEADING)"             FORMAT 9999
COLUMN phywrite                   HEADING "Phy|Write|(&&BYTES_HEADING)"            FORMAT 9999
COLUMN interconnect_io            HEADING "Inter|Connect|IO|(&&BYTES_HEADING)"     FORMAT 9999
COLUMN pga_allocated              HEADING "PGA|(&&BYTES_HEADING)"                  FORMAT 999.00
COLUMN temp_space_allocated       HEADING "Temp|Space|(&&BYTES_HEADING)"           FORMAT 9999.00

-- Get the SQL Statements from ASH
SELECT 
                             NVL(ash.qc_session_id,ash.session_id)                                        session_id 
                           , NVL(ash.qc_instance_id,ash.inst_id)                                          inst_id     
                           , NVL(ash.qc_session_serial#,ash.session_serial#)                              session_serial#
                           , TO_CHAR(NVL(ash.sql_exec_start,MIN(ash.sample_time)),'DD-MON-YY HH24:MI:SS') sql_exec_start
                           , TO_CHAR(max(ash.sample_time) ,'DD-MON-YY HH24:MI:SS')                        sql_exec_end
                           , SUBSTR(REPLACE(max(ash.sample_time) - NVL(ash.sql_exec_start,MIN(ash.sample_time)),'00000000','')
                                    ,1,INSTR(REPLACE(max(ash.sample_time) - NVL(ash.sql_exec_start,MIN(ash.sample_time)),'00000000',''),'.')-1       
                                    ) duration
&&_IF_ORA_11gR2_OR_HIGHER  , ash.sql_opname
                           --, ash.sql_exec_id
                           , ash.sql_id
                           , ash.sql_child_number
                           , ash.sql_plan_hash_value
&&_IF_ORA_11202_OR_HIGHER  , max(current_dop)    current_dop
                           , ash.force_matching_signature
                           , ash.top_level_sql_id
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.delta_read_io_requests)/&&COUNT_DIVIDER)        phyread_requests
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.delta_write_io_requests)/&&COUNT_DIVIDER)       phywrite_requests
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.delta_read_io_bytes)/&&BYTES_DIVIDER)           phyread
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.delta_write_io_bytes)/&&BYTES_DIVIDER)          phywrite
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.delta_interconnect_io_bytes)/&&BYTES_DIVIDER)   interconnect_io
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.pga_allocated)/&&BYTES_DIVIDER,2)               pga_allocated
&&_IF_ORA_11gR1_OR_HIGHER  , ROUND(SUM(ash.temp_space_allocated)/&&BYTES_DIVIDER,2)        temp_space_allocated
FROM
(
                     SELECT /*+ parallel(ash, 10) LEADING(ash) */ 
                             ash.session_id
                           , ash.inst_id
                           , ash.session_serial#
                           , ash.qc_session_id
                           , ash.qc_instance_id
                           , ash.qc_session_serial#
                           , NVL(ash.sql_exec_start,MIN(ash.sample_time)) sql_exec_start
                           , max(ash.sample_time)                         sample_time
&&_IF_ORA_11gR2_OR_HIGHER  , ash.sql_opname
                           , ash.sql_exec_id
                           , ash.sql_id
                           , ash.sql_child_number
                           , ash.sql_plan_hash_value
&&_IF_ORA_11202_OR_HIGHER  , max(trunc(ash.px_flags / 2097152))    current_dop
                           , ash.force_matching_signature
                           , NVL(ash_parent.top_level_sql_id,ash.top_level_sql_id)      top_level_sql_id
&&_IF_ORA_11gR1_OR_HIGHER  , SUM(ash.delta_read_io_requests)                            delta_read_io_requests
&&_IF_ORA_11gR1_OR_HIGHER  , SUM(ash.delta_write_io_requests)                           delta_write_io_requests
&&_IF_ORA_11gR1_OR_HIGHER  , SUM(ash.delta_read_io_bytes)                               delta_read_io_bytes
&&_IF_ORA_11gR1_OR_HIGHER  , SUM(ash.delta_write_io_bytes)                              delta_write_io_bytes
&&_IF_ORA_11gR1_OR_HIGHER  , SUM(ash.delta_interconnect_io_bytes)                       delta_interconnect_io_bytes
&&_IF_ORA_11gR1_OR_HIGHER  , MAX(ash.pga_allocated)                                     pga_allocated
&&_IF_ORA_11gR1_OR_HIGHER  , MAX(ash.temp_space_allocated)                              temp_space_allocated
                      FROM gv$active_session_history ash
                            LEFT OUTER JOIN gv$active_session_history ash_parent 
                                        ON ash_parent.inst_id                   = ash.qc_instance_id
                                       AND ash_parent.session_id                = ash.qc_session_id
                                       AND ash_parent.session_serial#           = ash.qc_session_serial#
                                       AND CAST(ash_parent.sample_time as DATE) = ash.sql_exec_start
                                       AND ash_parent.sample_time               > sysdate - (&&HOURS/24)
                     WHERE ash.sql_id                                       IS NOT NULL
                           AND NVL(ash.qc_instance_id,ash.inst_id)              LIKE '&&INST_ID'  
                           AND NVL(ash.qc_session_id,ash.session_id)            LIKE '&&SID' 
                           AND NVL(ash.qc_session_serial#,ash.session_serial#)  LIKE '&&SERIAL'
                           AND NVL(ash.sql_id,' ')                              LIKE '&&SQL_ID'
                           AND ash.sample_time                                  > sysdate - (&&HOURS/24)
                           AND ash.sql_exec_id IS NOT NULL
                           &&WHERECLAUSE &&WHERECLAUSE2
                 GROUP BY    ash.session_id
                           , ash.inst_id
                           , ash.session_serial#
                           , ash.qc_session_id
                           , ash.qc_instance_id
                           , ash.qc_session_serial#
                           , ash.sql_exec_id
                           , ash.sql_exec_start
                           , ash.sql_id
                           , ash.sql_child_number
                           , ash.sql_plan_hash_value
                           , ash.force_matching_signature
&&_IF_ORA_11gR2_OR_HIGHER  , ash.sql_opname
                           , NVL(ash_parent.top_level_sql_id,ash.top_level_sql_id)
) ash
WHERE 1=1              
                 GROUP BY    NVL(ash.qc_session_id,ash.session_id)
                           , NVL(ash.qc_instance_id,ash.inst_id)   
                           , NVL(ash.qc_session_serial#,ash.session_serial#)
                           --, ash.sql_exec_id
                           , ash.sql_exec_start
                           , ash.sql_id
                           , ash.sql_child_number
                           , ash.sql_plan_hash_value
                           , ash.force_matching_signature
&&_IF_ORA_11gR2_OR_HIGHER  , ash.sql_opname
                           , ash.top_level_sql_id
               &&HAVINGCLAUSE            
               --having  (SUM(ash.delta_read_io_bytes)+SUM(ash.delta_interconnect_io_bytes) ) / power(1024,3) >  20    -- Physical Read + Write GBs
               --having  (SUM(ash.delta_read_io_bytes) ) / power(1024,3) > 100                                       -- Physical Write GBs
               --having  (SUM(ash.delta_interconnect_io_bytes) ) / power(1024,3) > 100                               -- Interconnect IO GBs
               --having  (SUM(ash.pga_allocated) )               / power(1024,3) > 2                                 -- PGA GBs
               --having  (SUM(ash.temp_space_allocated) )        / power(1024,3) > 10                                -- Temp Space  GBs
                 ORDER BY --max(ash.sample_time) asc
                        --, 
                          max(ash.sample_time) asc
                        , NVL(ash.sql_exec_start,MIN(ash.sample_time)) ASC   
;


@@footer
