How to Backup/Export Oracle Optimizer Statistics into Table
Exporting and Importing Statistics
Caveat: Always use import/export and use imp/exp utility on schema user who owns tables. I have wasted a week where I was exporting as DBA for XYZ user and then importing into different system under different username.
Statistics can be exported and imported from the data dictionary to user-owned tables. This enables to create multiple versions of statistics for the same schema. One can also copy statistics from one database to another database.
You may want to copy statistics from a production database to a scaled-down test database to look at SQL execution plans.
Note:
Exporting and importing statistics is a distinct concept from the EXP and IMP utilities of the database.
The DBMS_STATS export and import packages do utilize IMP and EXP dump files.
Before exporting statistics, you first need to create a table for holding the statistics.
This statistics table is created using the procedure DBMS_STATS.CREATE_STAT_TABLE.
After this table is created, then you can export statistics from the data dictionary into your statistics table using the DBMS_STATS.EXPORT_*_STATS procedures.
The statistics can then be imported using the DBMS_STATS.IMPORT_*_STATS procedures.
Note that the optimizer does not use statistics stored in a user-owned table. The only statistics used by the optimizer are the statistics stored in the data dictionary.
In order to have the optimizer use the statistics in a user-owned tables, you must import those statistics into the data dictionary using the statistics import procedures.
In order to move statistics from one database to another, you must first export the statistics on the first database, then copy the statistics table to the second database, using the EXP and IMP utilities or other mechanisms, and finally import the statistics into the second database.
The EXP and IMP utilities export and import optimizer statistics from the database along with the table. One exception is that statistics are not exported with the data if a table has columns with system-generated names.
Restoring Statistics Versus Importing or Exporting Statistics
The functionality for restoring statistics is similar in some respects to the functionality of importing and exporting statistics.
In general, you should use the restore capability when:
- You want to recover older versions of the statistics. For example, to restore the optimizer behavior to an earlier date.
- You want the database to manage the retention and purging of statistics histories.
You should use EXPORT/IMPORT_*_STATS procedures when:
- You want to experiment with multiple sets of statistics and change the values back and forth.
- You want to move the statistics from one database to another database. For example, moving statistics from a production system to a test system.
- You want to preserve a known set of statistics for a longer period of time than the desired retention date for restoring statistics.
1. Create the Statistics table.exec DBMS_STATS.CREATE_STAT_TABLE(ownname =>'SCHEMA_NAME' ,stat_tab => 'STATS_TABLE' , tblspace => 'STATS_TABLESPACE'); >>>>>>>> For 10G exec DBMS_STATS.CREATE_STAT_TABLE(ownname =>'SYSTEM',stat_tab => 'STATS_TABLE'); >>>>>>>> For 9i and earlier begin DBMS_STATS.CREATE_STAT_TABLE('dba_admin','STATS_TABLE'); end; 2. Export statistics to statistics table EXEC DBMS_STATS.EXPORT_SCHEMA_STATS('ORIGINAL_SCHEMA' ,'STATS_TABLE',NULL,'SYSTEM'); 3. Import statistics into the data dictionary. exec DBMS_STATS.IMPORT_SCHEMA_STATS('NEW_SCHEMA','STATS_TABLE',NULL,'SYSTEM'); 4. Drop the statistics table. exec DBMS_STATS.DROP_STAT_TABLE('SYSTEM','STATS_TABLE');FOR 9i begin DBMS_STATS.CREATE_STAT_TABLE('dba_admin','STATISTICS_TABLE_060307'); end; begin DBMS_STATS.EXPORT_SCHEMA_STATS('SAPBP2' ,'STATISTICS_TABLE_060307',NULL,'DBA_ADMIN'); end; SQL> exec DBMS_STATS.IMPORT_SCHEMA_STATS('SAGAR','STATISTICS_TABLE_060307',NULL,'SAGAR'); PL/SQL procedure successfully completed. Monitor export Process >>>>>>>> select count(*) from &STATS_NAME Stats table can grow exponentially so look at table size while export is active. select sum(bytes)/1000000 from dba_extents where segment_name='&TABLE_NAME' Sample statistics at SAP BW System of size 4.2 Tera bytes Time Elapsed for Export : 40 Mins Total stats Table Size : 2GB Time Elapsed for Import :How to Validate that Stats are reflected after exp/imp select TABLE_NAME, NUM_ROWS, BLOCKS, EMPTY_BLOCKS, AVG_SPACE, CHAIN_CNT, AVG_ROW_LEN from dba_tables where owner='&USER' At both Databases and see they are very similar.
Leave a Reply
You must be logged in to post a comment.
If you need to grant execution plan access use…
exec sp_MSforeachdb ‘USE [?]; GRANT SHOWPLAN TO readOnlyAccess,readWriteAccess,readExecuteAccess,alterExecuteWriteAccess’