@@header

/*
*
*  Author    : Vishal Gupta
*  Purpose   : Display load profile from AWR
*              All figures delta between two consecutive snapshots
*  Parameter : 1 - InstanceNumber (Use % for all instances)
*              2 - From Timestamp (In YYYY-MM-DD HH24:MI:SS format)
*              3 - To Timestamp   (In YYYY-MM-DD HH24:MI:SS format)
*  
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  04-Apr-12  Vishal Gupta  Intial version
*  
*/


/************************************
*  INPUT PARAMETERS
************************************/
UNDEFINE INST_ID
UNDEFINE FROM_TIMESTAMP
UNDEFINE TO_TIMESTAMP

DEFINE INST_ID="&&1"
DEFINE FROM_TIMESTAMP="&2"
DEFINE TO_TIMESTAMP="&3"

COLUMN  _FROM_TIMESTAMP      NEW_VALUE FROM_TIMESTAMP     NOPRINT
COLUMN  _TO_TIMESTAMP        NEW_VALUE TO_TIMESTAMP       NOPRINT
COLUMN  _INST_ID             NEW_VALUE INST_ID            NOPRINT

set term off
SELECT DECODE('&&INST_ID','','%','&&INST_ID')  "_INST_ID"
     , DECODE('&&FROM_TIMESTAMP','',to_char(sysdate - 1,'YYYY-MM-DD HH24:MI:SS'),'&&FROM_TIMESTAMP')  "_FROM_TIMESTAMP"
     , DECODE('&&TO_TIMESTAMP','',to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'),'&&TO_TIMESTAMP')  "_TO_TIMESTAMP"
FROM DUAL;
set term on


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

DEFINE SNAP_TIME_FORMAT="YYYY-MM-DD HH24:MI:SS"

DEFINE COUNT_FORMAT="9,999,999"
--DEFINE COUNT_DIVIDER="1"
--DEFINE COUNT_HEADING="#"
DEFINE COUNT_DIVIDER="1000"
DEFINE COUNT_HEADING="'000s"
--DEFINE COUNT_DIVIDER="1000000"
--DEFINE COUNT_HEADING="'000,000s"

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=9,999
--DEFINE TIME_DIVIDER="1"
--DEFINE TIME_HEADING="sec"
DEFINE TIME_DIVIDER="60"
DEFINE TIME_HEADING="min"



PROMPT 
PROMPT ####################################################################################
PROMPT #   L O A D   P R O F I L E   T R E N D
PROMPT #  
PROMPT #   From AWR Repository
PROMPT #   - For Instance ID - &INST_ID
PROMPT #   - From Time       - &&FROM_TIMESTAMP (YYYY-MM-DD HH24:MI:SS) 
PROMPT #   - To Time         - &&TO_TIMESTAMP (YYYY-MM-DD HH24:MI:SS)
PROMPT ####################################################################################


COLUMN end_interval_time   HEADING "Snap Time|(&&SNAP_TIME_FORMAT)"           FORMAT a22
COLUMN cpu_time            HEADING "CPU|Time|(&TIME_HEADING)"                 FORMAT &&TIME_FORMAT
COLUMN redo_size           HEADING "RedoSize|(&BYTES_HEADING)"                FORMAT &&BYTES_FORMAT
COLUMN logical_reads       HEADING "Logical|Reads|(&COUNT_HEADING)"           FORMAT &&COUNT_FORMAT
COLUMN block_changes       HEADING "Block|Changes|(&COUNT_HEADING)"           FORMAT &&COUNT_FORMAT
COLUMN phy_read_bytes      HEADING "Physical|Read|(&BYTES_HEADING)"           FORMAT &&BYTES_FORMAT
COLUMN phy_write_bytes     HEADING "Physical|Write|(&BYTES_HEADING)"          FORMAT &&BYTES_FORMAT
COLUMN phy_reads           HEADING "Physical|Reads|(&COUNT_HEADING)"          FORMAT &&COUNT_FORMAT
COLUMN phy_writes          HEADING "Physical|Writes|(&COUNT_HEADING)"         FORMAT &&COUNT_FORMAT
COLUMN user_calls          HEADING "User|Calls|(&COUNT_HEADING)"              FORMAT &&COUNT_FORMAT
COLUMN parses              HEADING "Parses|(&COUNT_HEADING)"                  FORMAT &&COUNT_FORMAT
COLUMN hard_parses_reads   HEADING "Hard|Parses|(&COUNT_HEADING)"             FORMAT &&COUNT_FORMAT
COLUMN sorts_mem           HEADING "Sorts|Memory|(&COUNT_HEADING)"            FORMAT &&COUNT_FORMAT
COLUMN sorts_disk          HEADING "Sorts|Disk|(&COUNT_HEADING)"              FORMAT &&COUNT_FORMAT
COLUMN logons              HEADING "Logons|(&COUNT_HEADING)"                  FORMAT &&COUNT_FORMAT
COLUMN commits             HEADING "Commits|(&COUNT_HEADING)"                 FORMAT &&COUNT_FORMAT
COLUMN rollbacks           HEADING "Rollbacks|(&COUNT_HEADING)"               FORMAT &&COUNT_FORMAT

WITH snap
AS
 (SELECT s.dbid
       , s.instance_number
       , s.snap_id
       , LAG (s.snap_id) OVER (PARTITION BY s.dbid, s.instance_number ORDER BY s.dbid, s.instance_number, s.snap_id) prev_snap_id
       , s.end_interval_time
   FROM  dba_hist_snapshot s
       , v$database d
  WHERE s.dbid = d.dbid
    AND s.instance_number LIKE '&&INST_ID'
    AND s.end_interval_time BETWEEN TO_TIMESTAMP('&&FROM_TIMESTAMP','YYYY-MM-DD HH24:MI:SS') 
                                AND TO_TIMESTAMP('&&TO_TIMESTAMP','YYYY-MM-DD HH24:MI:SS')
 )
SELECT /*+ USE_NL(s ss ss_prev) */ 
      TO_CHAR(s.end_interval_time,'&&SNAP_TIME_FORMAT') end_interval_time
    , ROUND(SUM(DECODE(ss.stat_name, 'CPU used by this session', ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/100/&TIME_DIVIDER))))  cpu_time
    , ROUND(SUM(DECODE(ss.stat_name, 'redo size'            , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&BYTES_DIVIDER))))        redo_size
    , ROUND(SUM(DECODE(ss.stat_name, 'session logical reads', ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        logical_reads
    , ROUND(SUM(DECODE(ss.stat_name, 'db block changes'     , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        block_changes
    , ROUND(SUM(DECODE(ss.stat_name, 'physical read total bytes' , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&BYTES_DIVIDER))))   phy_read_bytes
    , ROUND(SUM(DECODE(ss.stat_name, 'physical write total bytes', ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&BYTES_DIVIDER))))   phy_write_bytes
    , ROUND(SUM(DECODE(ss.stat_name, 'physical reads'       , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        phy_reads
    , ROUND(SUM(DECODE(ss.stat_name, 'physical writes'      , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        phy_writes
    , ROUND(SUM(DECODE(ss.stat_name, 'user calls'           , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        user_calls
    , ROUND(SUM(DECODE(ss.stat_name, 'parse count (total)'  , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        parses
    , ROUND(SUM(DECODE(ss.stat_name, 'parse count (hard)'   , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        hard_parses_reads
    , ROUND(SUM(DECODE(ss.stat_name, 'sorts (memory)'       , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        sorts_mem
    , ROUND(SUM(DECODE(ss.stat_name, 'sorts (disk)'         , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        sorts_disk
    , ROUND(SUM(DECODE(ss.stat_name, 'logons cumulative'    , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        logons
    , ROUND(SUM(DECODE(ss.stat_name, 'user commits'         , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        commits
    , ROUND(SUM(DECODE(ss.stat_name, 'user rollbacks'       , ROUND ((ss.value - LEAST(ss_prev.value,ss.value))/&COUNT_DIVIDER))))        rollbacks
FROM snap s
   , dba_hist_sysstat ss
   , dba_hist_sysstat ss_prev
where s.prev_snap_id IS NOT NULL
and s.dbid = ss_prev.dbid
and s.instance_number = ss_prev.instance_number
and s.prev_snap_id = ss_prev.snap_id
and s.dbid = ss.dbid
and s.instance_number = ss.instance_number
and s.snap_id = ss.snap_id
and ss_prev.stat_name = ss.stat_name
and ss.stat_name in (
                 'redo size'
                ,'session logical reads'
                ,'db block changes'
                ,'physical reads'
                ,'physical read total bytes'
                ,'physical read bytes'
                ,'physical writes'
                ,'physical write total bytes'
                ,'physical write bytes'
                ,'user calls'
                ,'parse count (total)'
                ,'parse count (hard)'
                ,'sorts (memory)'
                ,'sorts (disk)'
                ,'logons cumulative'
                ,'execute count'
                ,'user commits'
                ,'user rollbacks'
                ,'CPU used by this session'
                )
GROUP BY TO_CHAR(s.end_interval_time,'&&SNAP_TIME_FORMAT') 
ORDER BY TO_CHAR(s.end_interval_time,'&&SNAP_TIME_FORMAT') asc
/


@@footer
