Tuesday, 20 September 2016

How To Take Stats Backup

 

1. Connect To User "DATA_LMS"

SQL> Conn DATA_LMS/DATA_LMS
-->

2. First It Will Create A Table Table Name "MY_STATS_TABLE" And Owner Of The Table "

SQL>
begin
  dbms_stats.CREATE_STAT_TABLE( ownname=>DATA_LMS
                             , stattab=>'MY_STATS_TABLE'
                              );
end;
/
3. Check The Table Got Created Or Not

SQL>  desc MY_STATS_TABLE
 Name                                  Null?    Type
 ------------------------------------- -------- ----------------
 STATID                                         VARCHAR2(30 CHAR)
 TYPE                                           CHAR(1 CHAR)
 VERSION                                        NUMBER
 FLAGS                                          NUMBER
 C1                                             VARCHAR2(30 CHAR)
 C2                                             VARCHAR2(30 CHAR)
 C3                                             VARCHAR2(30 CHAR)
 C4                                             VARCHAR2(30 CHAR)
 C5                                             VARCHAR2(30 CHAR)
 N1                                             NUMBER
 N2                                             NUMBER
 N3                                             NUMBER
 N4                                             NUMBER
 N5                                             NUMBER
 N6                                             NUMBER
 N7                                             NUMBER
 N8                                             NUMBER
 N9                                             NUMBER
 N10                                            NUMBER
 N11                                            NUMBER
 N12                                            NUMBER
 D1                                             DATE
 R1                                             RAW(32)
 R2                                             RAW(32)
 CH1                                            VARCHAR2(1000 CHAR)
 CL1                                            CLOB

4. Take Export Of Stats

SQL> begin
  dbms_stats.export_schema_stats( ownname=>DATA_LMS
                                , stattab=>'MY_STATS_TABLE'
                                , statid=>'CURRENT_STATS'
                                );
end;
/

5. If we look at the contents of MY_STATS_TABLE, we will see rows for each different statistic type (T=Table, I=Index, C=Column, S=System).

SQL> select statid, type, count(*)
from my_stats_table
group by statid, type
/
Note:
 If Currents Stats Are Not Satisfiying then we can revert the old Stats as Follows
--> Delete Current Stats
Table, column and index statistics can be deleted using the relevant delete procedures.
EXEC DBMS_STATS.delete_database_stats;--> For Complete DB
EXEC DBMS_STATS.delete_schema_stats('SCOTT');--> For Schema
EXEC DBMS_STATS.delete_table_stats('SCOTT', 'EMP');--> For Table
EXEC DBMS_STATS.delete_column_stats('SCOTT', 'EMP', 'EMPNO');--> For Column
EXEC DBMS_STATS.delete_index_stats('SCOTT', 'EMP_PK');--> For Index
EXEC DBMS_STATS.delete_dictionary_stats;--> For Dictionary stats
6. Import Old Stats:
begin
  dbms_stats.import_schema_stats( ownname=>user(Username)
                                , stattab=>'MY_STATS_TABLE(Table_Name)'
                                , statid=>'CURRENT_STATS'
                                );
end;
/
Note: If We Want To Restore In Other DB Take Export Of The Stats Table "MY_STATS_TABLE" And Import, But Schema Name Should Same.

######################################################  All The Best ######################################################

No comments:

Post a Comment