@@header

/*
*
*  Author     : Vishal Gupta
*  Purpose    : Display SQLStats per execution from AWR
*  Version    : 10.1 and above
*  Parameters : 1 - SQL_ID          (Use % as wildcard) 
*               2 - PLAN_HASH_VALUE (Use % as wildcard)
*               3 - Number of Days  (Default 7 days) 
*               
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  08-Mar-12  Vishal Gupta  Intial version
*  20-Mar-12  Vishal Gupta  Added PLAN_HASH_VALUE in output
*
*
*/


/************************************
*  INPUT PARAMETERS
************************************/
UNDEFINE sql_id
UNDEFINE plan_hash_value
UNDEFINE days
UNDEFINE force_matching_signature
UNDEFINE whereclause

DEFINE sql_id="&&1"
DEFINE plan_hash_value="&&2"
DEFINE days="&&3"
DEFINE whereclause="&&4"
DEFINE force_matching_signature=""


COLUMN  _plan_hash_value          NEW_VALUE  plan_hash_value            NOPRINT
COLUMN  _days                     NEW_VALUE  days                       NOPRINT
COLUMN  _force_matching_signature NEW_VALUE  force_matching_signature   NOPRINT

set term off
SELECT DECODE('&&plan_hash_value','','%','&&plan_hash_value')   "_plan_hash_value"
     , DECODE('&&days','','7','&&days')   "_days"
     , DECODE('&&sql_id','%','%','')   "_force_matching_signature"
FROM DUAL;
set term on


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

DEFINE COUNT_SMALL_FORMAT=9,999
DEFINE COUNT_SMALL_DIVIDER="1"
DEFINE COUNT_SMALL_HEADING="#"
--DEFINE COUNT_DIVIDER="1000"
--DEFINE COUNT_HEADING="#1000"



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

DEFINE BYTES_FORMAT="999,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"

DEFINE TIME_FORMAT=9999

--DEFINE TIME_DIVIDER="0.001"
--DEFINE TIME_HEADING="msec"
DEFINE TIME_DIVIDER="1"
DEFINE TIME_HEADING="sec"
--DEFINE TIME_DIVIDER="60"
--DEFINE TIME_HEADING="min"


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

set term off
COLUMN  _force_matching_signature          NEW_VALUE  force_matching_signature            NOPRINT
select to_char(ss2.force_matching_signature) "_force_matching_signature" 
  from dba_hist_sqlstat ss2 
  where ss2.sql_id = '&&sql_id' and rownum = 1;
set term on
/************************************/


Prompt 
Prompt ******************************************************************
Prompt * AWR SQL Executions Statistics  (All Figures are per execution)
Prompt *   (Force Matching Signature = '&&force_matching_signature')
Prompt * 
PROMPT * Input Parameters 
PROMPT *    - SQL Id                   = '&&sql_id'
PROMPT *    - Plan Hash Value          = '&&plan_hash_value'
PROMPT *    - Days                     = '&&days'
PROMPT *    - WhereClause              = '&&whereclause'
Prompt ******************************************************************

COLUMN seperator                       HEADING "!|!|!|!"                                          FORMAT a1
COLUMN end_interval_time               HEADING "Snap Time"                                        FORMAT a15
COLUMN instance_number                 HEADING "I#"                                               FORMAT 99
COLUMN SQL_ID                          HEADING "SQLId"                                            FORMAT a13
COLUMN plan_hash_value                 HEADING "Plan|Hash|Value"                                  FORMAT 999999999999  
COLUMN invalidations_total             HEADING "Invalid|(&&COUNT_HEADING)|Per|Exec"                FORMAT 9,999
COLUMN iowait_total                    HEADING "IO|Wait|(&&TIME_HEADING)|Per|Exec"                 FORMAT &&TIME_FORMAT     
COLUMN apwait_total                    HEADING "Appl|Wait|(&&TIME_HEADING)|Per|Exec"               FORMAT &&TIME_FORMAT     
COLUMN ccwait_total                    HEADING "Conc|Wait|(&&TIME_HEADING)|Per|Exec"               FORMAT &&TIME_FORMAT     
COLUMN clwait_total                    HEADING "Cluster|Wait|(&&TIME_HEADING)|Per|Exec"            FORMAT &&TIME_FORMAT           
COLUMN plsexec_time_total              HEADING "PLsexec|Time|(&&TIME_HEADING)|Per|Exec"            FORMAT &&TIME_FORMAT     
COLUMN cpu_time_total                  HEADING "CPU|Time|(&&TIME_HEADING)|Per|Exec"                FORMAT &&TIME_FORMAT     
COLUMN elapsed_time_total              HEADING "Elapsed|Time|(&&TIME_HEADING)|Per|Exec"            FORMAT &&TIME_FORMAT     
COLUMN executions_delta                HEADING "Delta|Exec|(&&COUNT_HEADING)"                      FORMAT 999,999
COLUMN executions_total                HEADING "Total|Exec|(&&COUNT_HEADING)"                      FORMAT 999,999
COLUMN px_servers_execs_total          HEADING "Par'l|Serv|Exec|(&COUNT_SMALL_HEADING)|Per|Exec"  FORMAT 999
--COLUMN px_servers_execs_total          HEADING "Par'l|Server|Exec|(&COUNT_SMALL_HEADING)|Per|Exec"      FORMAT &COUNT_SMALL_FORMAT
COLUMN rows_processed_total            HEADING "Rows|(&&COUNT_HEADING)|Per|Exec"                   FORMAT &&COUNT_FORMAT 
COLUMN sorts_total                     HEADING "Sorts|(&&COUNT_SMALL_HEADING)|Per|Exec"            FORMAT &&COUNT_SMALL_FORMAT 
COLUMN buffer_gets_total               HEADING "Buffer|Gets|(&&COUNT_HEADING)|Per|Exec"            FORMAT 9,999,999
COLUMN direct_writes_total             HEADING "Direct|WriteReq|(&&COUNT_HEADING)|Per|Exec"        FORMAT &&COUNT_FORMAT
COLUMN io_interconnect_bytes_total     HEADING "IO|Inter|Connect|(&&BYTES_HEADING)|Per|Exec"       FORMAT &&BYTES_FORMAT 
COLUMN physical_read_bytes_total       HEADING "Phy|Read|(&&BYTES_HEADING)|Per|Exec"               FORMAT &&BYTES_FORMAT     
COLUMN physical_write_bytes_total      HEADING "Phy|Write|(&&BYTES_HEADING)|Per|Exec"              FORMAT &&BYTES_FORMAT     
COLUMN optimized_physical_reads_total  HEADING "Optimized|Phy|ReadReq|(&&COUNT_HEADING)|Per|Exec"  FORMAT &&COUNT_FORMAT
COLUMN io_offload_elig_bytes_total     HEADING "IO|Offload|Elig|(&&BYTES_HEADING)|Per|Exec"        FORMAT &&BYTES_FORMAT     
COLUMN io_offload_return_bytes_total   HEADING "IO|Offload|Return|(&&BYTES_HEADING)|Per|Exec"      FORMAT &&BYTES_FORMAT     
COLUMN sql_profile                     HEADING "sql_profile"                                      FORMAT a30



                            SELECT TO_CHAR(ss.end_interval_time,'DD-MON-YY HH24:MI')                                    end_interval_time
                                 , ss.instance_number      
                                 , ss.sql_id
                                 , ss.plan_hash_value  
                                 ,  '|'                                                                                   seperator
                                 , ROUND(ss.iowait_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)          iowait_total
                                 , ROUND(ss.apwait_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)          apwait_total
                                 , ROUND(ss.ccwait_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)          ccwait_total
                                 , ROUND(ss.clwait_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)          clwait_total
                                 , ROUND(ss.plsexec_time_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)    plsexec_time_total
                                 , ROUND(ss.cpu_time_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)        cpu_time_total
                                 , ROUND(ss.elapsed_time_total/1000000/GREATEST(ss.executions_total,1)/&&TIME_DIVIDER)    elapsed_time_total
                                 ,  '|'                                                                                   seperator
                                 , ss.executions_delta/&&COUNT_DIVIDER                                                    executions_delta     
                                 , ss.executions_total/&&COUNT_DIVIDER                                                    executions_total     
&&_IF_ORA_10gR2_OR_HIGHER        , ss.px_servers_execs_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER              px_servers_execs_total     
                            --     , ss.invalidations_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER               invalidations_total     
                                 , ss.rows_processed_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER                rows_processed_total
                                 , ss.sorts_total/GREATEST(ss.executions_total,1)/&&COUNT_SMALL_DIVIDER                   sorts_total
                                 , ss.buffer_gets_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER                   buffer_gets_total 
                                 , ROUND(ss.direct_writes_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER)  direct_writes_total
                                 ,  '|'                                                                                   seperator
&&_IF_ORA_11gR2_OR_HIGHER        , ss.io_interconnect_bytes_total/GREATEST(ss.executions_total,1)/&&BYTES_DIVIDER         io_interconnect_bytes_total
&&_IF_ORA_11gR2_OR_HIGHER        , ss.physical_read_bytes_total/GREATEST(ss.executions_total,1)/&&BYTES_DIVIDER           physical_read_bytes_total
&&_IF_ORA_11gR2_OR_HIGHER        , ss.physical_write_bytes_total/GREATEST(ss.executions_total,1)/&&BYTES_DIVIDER          physical_write_bytes_total
&&_IF_ORA_11gR2_OR_HIGHER        , ss.optimized_physical_reads_total/GREATEST(ss.executions_total,1)/&&COUNT_DIVIDER      optimized_physical_reads_total
&&_IF_ORA_11gR2_OR_HIGHER        , ss.io_offload_elig_bytes_total/GREATEST(ss.executions_total,1)/&&BYTES_DIVIDER         io_offload_elig_bytes_total
&&_IF_ORA_11gR2_OR_HIGHER        , ss.io_offload_return_bytes_total/GREATEST(ss.executions_total,1)/&&BYTES_DIVIDER       io_offload_return_bytes_total
                                 ,  '|'                                                                                   seperator
                                 , ss.sql_profile
                            FROM 
                            ( 
                              SELECT s.end_interval_time
                                   , ss.*
                                   , LEAD (ss.snap_id) OVER (ORDER BY ss.dbid, ss.instance_number, ss.snap_id) next_snap_id
                              from dba_hist_sqlstat   ss 
                                 , dba_hist_snapshot  s
                                 , v$database d
                              where ss.instance_number = s.instance_number
                                AND ss.dbid = s.dbid
                                AND ss.snap_id = s.snap_id
                                AND ss.dbid = d.dbid
                                AND s.end_interval_time >  SYSDATE - &&days 
                                AND ss.sql_id LIKE CASE ss.force_matching_signature 
                                                     WHEN 0 THEN '&&sql_id'
                                                     ELSE '%'
                                                     END
                                AND ss.force_matching_signature LIKE '&&force_matching_signature'
                                AND ss.plan_hash_value          LIKE '&&plan_hash_value'
                                &&whereclause
                            ) ss                               
                            WHERE (ss.executions_delta > 0 OR ss.snap_id + 1 <> ss.next_snap_id)
                            ORDER BY TO_CHAR(ss.end_interval_time,'YYYY-MM-DD HH24:MI') 
                                   , ss.instance_number
                            ;

@@footer
