Friday, February 11, 2011

Capacity planning: Automate the table growth statistics

Note: In this scenario we are considering tables with row count greater than 10000

Step 1:
Create a table for storing table growth statistics
create table db_table_growth_all
as select *
from (
select    DB_TIME,
          OWNER,
          SEGMENT_NAME,
          SEGMENT_TYPE,
          Total_Space,
          Rows_Count
  from (select sysdate DB_TIME,
          a.OWNER,
          a.SEGMENT_NAME,
          a.SEGMENT_TYPE,
          round((a.BYTES / 1048576), 2) Total_Space,
          nvl(b.num_rows, 1) Rows_Count
     from (select * from dba_segments) a, (select * from dba_tables) b
    where a.segment_name = b.table_name
      and a.segment_type = 'TABLE'
      and nvl(b.num_rows, 1) > 10000
      and round((a.BYTES / 1048576), 2) > 0))
     
Step 2:
Insert all records that meets criteria into the growth table
insert into db_table_growth_all
select *
from (
select sysdate DB_TIME,
          a.OWNER,
          a.SEGMENT_NAME,
          a.SEGMENT_TYPE,
          round((a.BYTES / 1048576), 2) Total_Space,
          nvl(b.num_rows, 1) Rows_Count
     from (select * from dba_segments) a, (select * from dba_tables) b
    where a.segment_name = b.table_name
      and a.segment_type = 'TABLE'
      and nvl(b.num_rows, 1) > 10000
      and round((a.BYTES / 1048576), 2) > 0
);
COMMIT;
Step 3:
Create a view for gathering subsequent details
create view v_db_table_growth_all
as select *
from
(
select sysdate DB_TIME,
          a.OWNER,
          a.SEGMENT_NAME,
          a.SEGMENT_TYPE,
          round((a.BYTES / 1048576), 2) Total_Space,
          nvl(b.num_rows, 1) Rows_Count
     from (select * from dba_segments) a, (select * from dba_tables) b
    where a.segment_name = b.table_name
      and a.segment_type = 'TABLE'
      and nvl(b.num_rows, 1) > 10000
      and round((a.BYTES / 1048576), 2) > 0
)
Step 4:
Insert the ongoing growth into the statistics table
insert into db_table_growth_all
select *
from v_db_table_growth_all;
COMMIT;
Step 5: Alter the nls_date_format in order to setup the job

alter session set nls_date_format ='dd-mon-yyyy hh24:mi:ss';

Step 6:
Setup a Job to run once
declare
  jobno number;
begin
  dbms_job.submit(jobno,
                  'begin insert into db_table_growth_all select * from v_db_table_growth_all;commit;end;',
                  sysdate,
                  'SYSDATE+1',
                  TRUE);
  commit;
end;
Step 7:
Execute the job setup in step 6
execute dbms_job.run(4518)

No comments:

Post a Comment