stackpack

  • Get all snapshot

select * from STATS$snapshot;

  • Check database instance

    1
    2
    select * from STATS$DATABASE_INSTANCE; – in case database restarted, an error will be thrown as ORA-20200: The instance was shutdown between snapshots 981 and 982
    select * from v$instance;
  • Run stackpack

    1
    2
    3
    4
    sqlplus perfstat/password@“(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xxrac)))”
    @$ORACLE_HOME/rdbms/admin/spreport.sql
    OR
    @?/rdbms/admin/spreport.sql
  • Purge stackpack

    1
    2
    3
    @$ORACLE_HOME/rdbms/admin/sppurge.sql
    OR
    @?/rdbms/admin/sppurge.sql
  • Oracle jobs

    1
    2
    3
    select * from user_jobs; SELECT job, next_date, next_sec, failures, broken, SUBSTR(what,1,40) DESCRIPTION FROM dba_jobs; 
    EXEC dbms_job.broken(24, false);
    EXEC dbms_job.run(24, false); – reset job when failure 16 attemps to run
  • Oracle job for schedule run sppurge

——————– Check the snap present before 20 days ——————–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
select count(*) from stats$snapshot where snap_time < sysdate-20

----Create the procedure which will purge statspack snapshot after 20 days gap---------
create or replace procedure statspackpurge is
var_lo_snap number;
var_hi_snap number;
var_db_id number;
var_instance_no number;
noofsnapshot number;
n_count number ;
begin

n_count := 0;

select count(*) into n_count from stats$snapshot where snap_time < sysdate-20;

if n_count > 0 then

select min(s.snap_id) , max(s.snap_id),max(di.dbid),max(di.instance_number) into var_lo_snap, var_hi_snap,var_db_id,var_instance_no
from stats$snapshot s
, stats$database_instance di
where s.dbid = di.dbid
and s.instance_number = di.instance_number
and di.startup_time = s.startup_time
and s.snap_time < sysdate-20;

noofsnapshot := statspack.purge( i_begin_snap => var_lo_snap
, i_end_snap => var_hi_snap
, i_snap_range => true
, i_extended_purge => false
, i_dbid => var_db_id
, i_instance_number => var_instance_no);

dbms_output.Put_line('snapshot deleted'||to_char(noofsnapshot));

end if;
end;
/

——————– check the job already exits in the database ——————–

1
SELECT  job, next_date, next_sec, failures, broken, SUBSTR(what,1,40) DESCRIPTION FROM dba_jobs;

——————– scheduled the job at mid night ——————–

1
2
3
4
5
6
7
8
9
declare
my_job number;
begin
dbms_job.submit(job => my_job,
what => 'statspackpurge;',
next_date => trunc(sysdate)+1,
interval => 'trunc(sysdate)+1');
end;
/

————– Another way———————————————————-

1
2
3
4
5
6
variable v_jobno number;
begin
dbms_job.submit(:v_jobno,'statspack.purge(i_num_days=>15,i_extended_purge=>TRUE);',trunc(sysdate)+1+2/24,'SYSDATE',TRUE);
commit;
end;
/
  • Reference

Stackpack
http://www.oracle-base.com/articles/8i/statspack-8i.php
http://docs.oracle.com/cd/B10500_01/server.920/a96533/statspac.htm

Oracle job
http://www.orafaq.com/wiki/DBMS_JOB
http://www.dba-oracle.com/tips_oracle_statspack_purge_utility.htm
http://myoracleworld.hobby-electronics.net/DB-statspack.html
http://dbaworks-sunny.blogspot.hk/2012/12/statspack-purge-script-for-scheduling.html
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_job.htm#i1000769