Transfer Statistics from one Database to Another

GOAL

This article describes the steps required to transfer the table/index statistics from one database to another.
Discussion will cover how to accomplish this via table level, schema level, and database level.

SOLUTION

1. Create the STATS table

SQL> connect user/pwd
SQL> EXEC DBMS_STATS.CREATE_STAT_TABLE(ownname=> 'user',stattab=>'STATS');

2. Export the statistics to the STATS table

Retrieving the statistics can be achieved using EXPORT_XXXX_STATS procedure:

  • Exporting the statistics of all the objects in the database
    EXEC DBMS_STATS.EXPORT_DATABASE_STATS(stattab => 'STATS');
      
    Make sure you have logged in as SYS or SYSTEM to export database level statistics

  • Exporting the statistics of all the objects in a schema
    EXEC DBMS_STATS.EXPORT_SCHEMA_STATS(ownname => 'user', stattab => 'STATS');
      
    You need to have DBA privileges to export the statistics of other schemas.

  • Exporting the statistics of tables
    EXEC DBMS_STATS.EXPORT_TABLE_STATS(<'username' or NULL>,'TAB1',NULL,'STATS');
      

3. Export the STATS table using export(exp) or datapump(expdp)

exp system/manager tables=STATS file=stats.dmp owner=<user>log=stats.log

4. Transfer the dump file to the destination database

If you are using ftp to transfer the files make sure that you are transferring the file with binary option to avoid the dump file being corrupted.

5. Import the STATS table to the destination database

imp system/manager tables=STATS file=stats.dmp full=Y

6. Import the statistics into the data dictionary

The statistics can be imported using IMPORT_XXXX_STATS procedure:

  • Importing the statistics for all the objects in the database
      
    EXEC DBMS_STATS.IMPORT_DATABASE_STATS(stattab => 'STATS');
      
  • Importing the statistics to all the objects in a schema
    EXEC DBMS_STATS.IMPORT_SCHEMA_STATS(ownname => user, stattab => 'STATS');
      

  • Importing the statistics to tables 
     
    EXEC DBMS_STATS.IMPORT_TABLE_STATS(ownname => user, tabname => 'TAB1', stattab => 'STATS');
      

7. Changing the schema name

Statistics cannot be directly exported from one schema and imported into a different schema.The schema names much match exactly.
If the target database schema is different from the source database schema, the schema name can be changed by updating column C5 of the STATS table:

update table STATS  set c5 = '<target schema>'
where c5 = '<Source schema>'
and statid = <Stat Id used while exporting these stats>;

8. Upgrading statistics table

When importing statistics from an earlier version into a later version of Oracle, the structure of the statistics table may have changed.
Try executing DBMS_STATS.UPGRADE_STAT_TABLE to resolve this

Note:
When exporting and importing database objects, Datapump Export and Import utilities will also export and import optimizer statistics along with the tables even when a column has system-generated names.

Importing statistics will overwrite any previous statistics that existed for the table having statistics imported. For example, if previously there were histogram statistics and you import statistics that do not include histograms, there will no longer be any histogram information.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值