Beruflich Dokumente
Kultur Dokumente
on Windows:
SQL>
SQL>
connect / as sysdba
@%ORACLE_HOME%\rdbms\admin\spcreate
Check each of the three output files produced (spcusr.lis, spctab.lis, spcpkg.lis) by the installation to ensure no errors were encountered, before
continuing on to the next step.
Errors during installation
To correctly install Statspack after an error, first run the de-install script, then the install script. Both scripts must be run from SQL*Plus.
e.g. Start SQL*Plus, connect as a user with SYSDBA privilege, then:
SQL> @spdrop
SQL> @spcreate
Note: In an RAC environment, you must connect to the instance you wish to collect data for.
This will store the current values for the performance statistics in the STATSPACK tables, and can be used as a baseline snapshot for comparison
with another snapshot taken at a later time.
For better performance analysis, set the init.ora parameter timed_statistics to true; this way, Statspack data collected will include important timing
information. The timed_statistics parameter is also dynamically changable using the 'alter system' command. Timing data is important and is
PDFmyURL converts any url to pdf!
sqlplus perfstat
SQL> EXEC STATSPACK.MODIFY_STATSPACK_PARAMETER(i_snap_level=>5, i_buffer_gets_th=>100000, i_modify_parameter=>'true');
SQL> exec statspack.snap;
Where 'SYSDATE+(1/48)' will result in the statistics being gathered each1/48 hours (i.e. every half hour).
To force the job to run immediately,
execute dbms_job.run(<job_number>);
execute dbms_job.remove(<job_number>);
$ cd $ORACLE_HOME/rdbms/admin
SQL> connect perfstat/perfstat
SQL> @spreport
(also sprepsql.sql)
e.g. on Windows
SQL>
SQL>
connect perfstat/perfstat
@%ORACLE_HOME%\rdbms\admin\spreport
(also sprepsql.sql)
or
execute dbms_stats.gather_schema_stats('PERFSTAT');
statements.
In a level 5 snapshot, note that the time required for the snapshot to complete is dependant on the shared_pool_size and on the number of SQL
statements in the shared pool at the time the snapshot is taken: the larger the shared pool, the longer the time taken to complete the snapshot.
SQL 'Thresholds'
The SQL statements gathered by Statspack are those which exceed one of four predefined threshold parameters:
- number of executions of the SQL statement (default 100)
- number of disk reads performed by the SQL statement (default 1,000)
- number of parse calls performed by the SQL statement (default 1,000)
- number of buffer gets performed by the SQL statement (default 10,000)
- size of sharable memory used by the SQL statement (default 1m)
- version count for the SQL statement (default 20)
The values of each of these threshold parameters are used when deciding which SQL statements to collect - if a SQL statement's resource
usage exceeds any one of the above threshold values, it is captured during the snapshot.
The SQL threshold levels used are either those stored in the table stats$statspack_parameter, or by the thresholds specified when the snapshot
is taken.
Levels >= 10 Additional statistics: Parent and Child latches
This level includes all statistics gathered in the lower levels, and additionally gathers Parent and Child Latch information. Data gathered at this
level can sometimes cause the snapshot to take longer to complete i.e. this level can be resource intensive, and should only be used when advised
by Oracle personnel.
Truncating all data
If you wish to truncate all performance data indiscriminantly, it is possible to do this using sptrunc.sql This script truncates all statistics data
gathered.
Removing the package
To deinstall the package, connect as a user with SYSDBA privilege and run the following script from SQL*Plus: spdrop
e.g. on Unix
$ cd $ORACLE_HOME/rdbms/admin
SQL> connect perfstat/perfstat
SQL> @spdrop.sql
e.g. on Windows
SQL>
SQL>
connect perfstat/perfstat
@%ORACLE_HOME%\rdbms\admin\spdrop.sql
Check each of two output files produced (spdtab.lis, spdusr.lis) to ensure the package was completely deinstalled.
Purging/removing unnecessary data
It is possible to purge unnecessary data from the PERFSTAT schema using sppurge.sql. This script deletes snapshots which fall between the
begin and end range of Snapshot Id's specified.
Purging may require the use of a large rollback segment, as all data relating each Snapshot Id to be purged will be deleted. To avoid rollback
segment extension errors, explicitly use a large rollback segment. This can be done by executing the 'set transaction use rollback segment..'
command before running the sppurge.sql script
Automatic Purge of Old Statspack Reports - Method 1
Because STATSPACK uses FK referential integrity constraints with the ON CASCADE DELETE option, then I can delete from the stats$snapshot
table and all associated data is removed.
So I create a little procedure:
create PROCEDURE "PERFSTAT"."PURGE_SNAPS" as begin
--Delete Yesterdays data
delete from perfstat.stats$snapshot
where snap_time < trunc(sysdate-1);
commit;
end;
/
I know that I could execute the delete statement directly from the DB job, but with that procedure in place, I can use it to execute it manually.
,
where
and
and
and
and
and
stats$database_instance di
s.dbid
= :dbid
di.dbid
= :dbid
s.instance_number
= :inst_num
di.instance_number = :inst_num
di.startup_time
= s.startup_time
s.snap_time < sysdate-14;
right before:
and save that script as something ELSE (not sppurge.sql), then running that script will automatically delete all snapshots older then two weeks old.
You can add an EXIT at the end of this new script and then use OEM or your OS job scheduler to schedule this script to run every friday.
Save the script (below) as sp_purge.ksh to each Unix machine on which the Oracle instance may be located.
If the Unix tmp directory is anything other than /tmp then you will need to modify the script accordingly.
If your oratab file is located in any directory other than /var/opt/oracle then you will need to update the script accordingly.
Set execute privilege on the script: chmod u+x sp_purge.ksh
Establish a cron job to call the script. The script requires three parameters:
The name of the database in which to purge snapshots.
The maximum number of snapshots to retain.
The email recipient for success messages.
-----------------------CUT----------CUT----------CUT------------------------#!/bin/ksh
#
Script Name: sp_purge.ksh
#
This script is designed to purge StatsPack snapshots.
#
#
Parameter $1 is the name of the database.
#
Parameter $2 is the maximum number of snapshots to retain.
#
Parameter $3 is the mail recipient for success messages.
#
To succeed, this script must be run on the machine on which the
#
instance is running.
#
Example for calling this script:
#
#
sp_purge.ksh prod 30 username@mycompany.com
#
Script History:
#
#
Who
Date
Action
#
--------------- ------------ -------------------------------------------#
Mark J. Rogers 22-Sep-2003 Script creation.
#
#
#
tmp_dir=/tmp
# Validate the parameters.
if [[ $# -ne 3 ]]; then
echo ""
echo "*** ERROR: You must specify these parameters: "
echo ""
echo "
1: the name of the database"
echo "
2: the maximum # of snapshots to retain"
echo "
3: the mail recipient for success messages"
echo ""
exit 1
fi
grep "^${1}:" /var/opt/oracle/oratab >> /dev/null
PDFmyURL converts any url to pdf!
0 ]]; then
ERROR: Instance $1 is not running on machine `uname -n` "
on `date`."
The instance must be running on the current machine for this"
script to function properly."
Exiting..."
NUMBER
NUMBER
NUMBER
BEGIN
/* Assign values to these variables. */
:P_SNAPS_TO_RETAIN := ${2};
:P_LOSNAPID := -1;
:P_HISNAPID := -1;
END;
/
-- Identify the snapshot ids to purge, if any.
PDFmyURL converts any url to pdf!
DECLARE
V_LOSNAPID
V_HISNAPID
V_COUNT
V_COUNTER
V_DBID
V_INSTANCE_NUMBER
V_SNAPS_TO_RETAIN
BEGIN
select d.dbid, i.instance_number
INTO v_DBID, V_INSTANCE_NUMBER
from v\$database d, v\$instance i;
select count(snap_id)
into v_count
from perfstat.stats\$snapshot
where dbid = V_DBID AND
instance_number = V_INSTANCE_NUMBER;
IF V_COUNT <= V_SNAPS_TO_RETAIN THEN
-- We do NOT need to perform a purge.
DBMS_OUTPUT.PUT_LINE ('NOTE: There are only ' ||
to_char(v_count) || ' snapshots currently saved.');
ELSE
-- We DO need to perform a purge.
DBMS_OUTPUT.PUT_LINE ('There are currently ' ||
to_char(v_count) || ' snapshots saved.');
-- Obtain the low snapshot id to be purged.
select min(snap_id)
into V_LOSNAPID
from perfstat.stats\$snapshot
where dbid = V_DBID AND
instance_number = V_INSTANCE_NUMBER;
-- Obtain the high snapshot id to be purged.
FOR V_HISNAPID_REC IN
(SELECT SNAP_ID
FROM perfstat.stats\$snapshot
WHERE dbid = V_DBID AND
instance_number = V_INSTANCE_NUMBER
ORDER BY SNAP_ID DESC)
LOOP
V_COUNTER := V_COUNTER + 1;
IF V_COUNTER > V_SNAPS_TO_RETAIN THEN
V_HISNAPID := V_HISNAPID_REC.SNAP_ID;
EXIT;
-- Exit this LOOP and proceed to the next statement.
END IF;
PDFmyURL converts any url to pdf!
END LOOP;
:P_LOSNAPID := V_LOSNAPID;
:P_HISNAPID := V_HISNAPID;
END IF;
END;
/
prompt
-- Generate the specific purge script.
set linesize 60
spool $tmp_script
begin
IF (:P_LOSNAPID <> -1) THEN
/* Build the script to purge the StatsPack snapshots. */
dbms_output.put_line('#!/bin/ksh');
dbms_output.put_line('#THIS IS THE SCRIPT TO ACTUALLY PERFORM THE PURGE');
dbms_output.put_line('trap '' exit \$? '' ERR');
dbms_output.put_line('sqlplus -s << SP_EOF2');
dbms_output.put_line('/ as sysdba');
dbms_output.put_line('whenever sqlerror exit failure rollback');
dbms_output.put_line('whenever oserror exit failure rollback');
dbms_output.put_line('@ \$ORACLE_HOME/rdbms/admin/sppurge.sql');
dbms_output.put_line(:P_LOSNAPID);
dbms_output.put_line(:P_HISNAPID);
dbms_output.put_line('-- the following are needed again');
dbms_output.put_line('whenever sqlerror exit failure rollback');
dbms_output.put_line('whenever oserror exit failure rollback');
dbms_output.put_line('commit;');
dbms_output.put_line('exit');
dbms_output.put_line('SP_EOF2');
dbms_output.put_line('exit \$?');
END IF;
end;
/
spool off
exit
EOF_SP
if [[ !
echo
echo
echo
exit
fi
""
"The ${script_name} script appears to have completed "
" successfully on `date`."
""
mailx \
-s "sp_purge.ksh in $ORACLE_SID on `uname -n` completed successfully" \
${3} \
< $tmp_output
# End of script sp_purge.ksh.
-----------------------CUT----------CUT----------CUT-------------------------