|
|||||||||||||
|
|
The next part of the script does nothing more than ask you to look back at the output from the previous SQL and select a beginning snapshot ID and an ending snapshot ID. These values will be kept in the variable beg_snap_id and end_snap_id respectively. \prompt prompt prompt Enter Begining snapshot ID prompt ^^^^^^^^^^^^^^^^^^^^^^^^^^ prompt &&beg_snap_id prompt prompt Enter Ending snapshot ID prompt ^^^^^^^^^^^^^^^^^^^^^^^^^^ prompt &&end_snap_id I always like to give my reports a unique name so the next part of the script will provide that granularity. Notice that I preface with the instance_name and have the beg_snap_id and end_snap_id in the name. Also, I put my results in a lower directory named LST. Please name your report something meaningful. set termout off column rpt new_value rpt select instance_name||'_wrh_sample_'||&&beg_snap_id||'_'||&&end_snap_id||'.LST' rpt from v$instance; set termout on prompt prompt prompt ^^^^^^^^^^^^^ prompt Report Name : ../LST/&&rpt prompt ^^^^^^^^^^^^^ spool ../LST/&&rpt Now pull some statistics out of the Workload Repository History tables—for the beginning snap ID. We are all familiar with the V$SYSSTAT view, and the Workload Repository History has one very similar named WRH$_SYSSTAT. Join this table with WRH$_STAT_NAME for the statistic names and we can easily extract any collected statistic for our snapshot ID. In this example, I am pulling out the values for physical reads and writes for the beginning snapshot ID. These are stored in the variable tbr1 and tbw1 respectively. Probably one of the funniest and confusing parts of this script is the ‘SUM(DECODE’ part. All this does is, as rows are processed/scanned, add the value to the variable if the statistic name is equal to the name in the DECODE. There is only one row for each statistic and I could have written out a simple SELECT..FROM..WHERE for each statistic name, but I like having just one SQL statement and scan the table once as opposed to once for every statistic. column db_bounce new_value db_bounce column tbr1 new_value tbr1 column tbw1 new_value tbw1 set termout off SELECT sum(decode(stat_name,'physical read total bytes',value,0)) tbr1, sum(decode(stat_name,'physical write total bytes',value,0)) tbw1 FROM wrh$_sysstat, wrh$_stat_name WHERE wrh$_sysstat.stat_id = wrh$_stat_name.stat_id AND wrh$_sysstat.snap_id = &&beg_snap_id ; Check the bounce flag and see if we should zero out the beginning variables. Later in this script, since Oracle accumulates statistics over time and zeroes them out if a bounce occurs, we need to zero out the beginning variables so as not to subtract the beginning variables from the ending variables and come up with a negative or irrelevant number. select decode(startup_time,begin_interval_time,1,0) db_bounce from dba_hist_snapshot where snap_id = &&end_snap_id; SELECT decode(&&db_bounce,1,0,&&tbr1) tbr1, decode(&&db_bounce,1,0,&&tbw1) tbw1 FROM dual; set termout on This is just to print out the beginning variables to the screen—nice and pretty like. prompt prompt ^^^^^^^^^^^^ prompt First Sample prompt ^^^^^^^^^^^^ prompt Total Bytes Read : &&tbr1 prompt Total Bytes Written : &&tbw1 prompt Now it is time to get the values for our same statistics at the time of the second snapshot. This time the script asks for the end_snap_id and relies upon the user to enter in an ID from the very first output of all snapshot IDs earlier in the script. This SQL is exactly the same as the one above for the beg_snap_id. column tbr2 new_value tbr2 column tbw2 new_value tbw2 set termout off SELECT sum(decode(stat_name,'physical read total bytes',value,0)) tbr2, sum(decode(stat_name,'physical write total bytes',value,0)) tbw2 FROM wrh$_sysstat, wrh$_stat_name WHERE wrh$_sysstat.stat_id = wrh$_stat_name.stat_id AND wrh$_sysstat.snap_id = &&end_snap_id; set termout on The ending variables extracted need no check against the bounce of the database so we can just print them out on the screen. prompt prompt ^^^^^^^^^^^^^ prompt Second Sample prompt ^^^^^^^^^^^^^ prompt Total Bytes Read : &&tbr2 prompt Total Bytes Written : &&tbw2 Now we just subtract the beginning statistical values from the ending values to get the amount between the two snapshots. Then print the results to the screen. prompt ^^^^^^^^^ prompt Results : prompt ^^^^^^^^^ column tr new_value tr column tw new_value tw set termout off SELECT ROUND(&&tbr2-&&tbr1) tr, ROUND(&&tbw2-&&tbw1) tw FROM dual; set termout on prompt Total Bytes Read = &&tr prompt Total Bytes Written = &&tw A little house cleaning to clear out the variables we used. spool off undefine num_days_back undefine beg_snap_id undefine end_snap_id undefine db_bounce This may seem like a lot of work to subtract a few statistical values, but the beauty of this is that you can extend this simple, ok semi-simple script, to do a lot of work around the selected snapshot IDs. Most of which you could not do otherwise by any cleaver single SQL statement. Moreover, the most important part is that the existing AWR reports do not report on every type of statistical information you need to tune your database. I have often times found myself punching a calculator to find some metric. No longer do I, or you, have to do this. Create a script such as this, put in your own statistic names, and do the calculations at the end. Happy Reporting.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |