首页 > Linux操作系统 > Linux操作系统 > 统计信息历史的备份、还原功能


原创 Linux操作系统 作者:wei-xh 时间:2011-03-11 15:54:48 0 删除 编辑

As of Oracle Database 10g, whenever system statistics or object statistics are gathered through
the package dbms_stats, instead of simply overwriting current statistics with the new statistics,
the current statistics are saved in other data dictionary tables that keep a history of all changes
occurring within a retention period. The purpose is to be able to restore old statistics in case
that new statistics lead to inefficient execution plans.
Up to Oracle9i, you can also implement such a history by taking advantage of a backup
table. Nevertheless, it is a good thing that the package dbms_stats automatically takes care of it.

Retention Period and Purging
Statistics are kept in the history for an interval specified by a retention period. The default value is
31 days. You can display the current value by calling the function get_stats_history_retention in
the package dbms_stats, as shown here:
SQL> SELECT dbms_stats.get_stats_history_retention() AS retention
2 FROM dual;

To change the retention period, the package dbms_stats provides the procedure alter_stats_
history_retention. Here is an example where the call sets the retention period to 14 days:
dbms_stats.alter_stats_history_retention(retention => 14)

Note that with the procedure alter_stats_history_retention, the following values have a
special meaning:
• NULL sets the retention period to the default value.
• 0 disables the history.
• -1 disables the purging of the history.

When the initialization parameter statistics_level is set to typical (the default value) or
all, statistics older than the retention period are automatically purged. Whenever manual
purging is necessary, the package dbms_stats provides the procedure purge_stats. The following
call purges all statistics placed in the history more than 14 days ago:
dbms_stats.purge_stats(before_timestamp => systimestamp-14)
To execute the procedures alter_stats_history_retention and purge_stats, you need to
have the system privilege analyze any dictionary.

If you are interested in knowing when object statistics for a given table were modified, the data
dictionary view user_tab_stats_history provides all the necessary information. Of course, there
are dba and all versions of that view as well.
Here is an example. With the following query, it is possible to display when the object
statistics of the table tab$ in the schema sys where modified:
SQL> SELECT stats_update_time
2 FROM dba_tab_stats_history
3 WHERE wner = 'SYS' and table_name = 'TAB$';
05-MAY-07 AM +02:00
11-MAY-07 PM +02:00

Restoring Statistics
Whenever it may be necessary, statistics can be restored from the history. For that purpose, the
package dbms_stats provides the following procedures:
• restore_database_stats restores object statistics for the whole database.
• restore_dictionary_stats restores object statistics for the data dictionary.
• restore_fixed_objects_stats restores object statistics for fixed tables.
• restore_system_stats restores system statistics.
• restore_schema_stats restores object statistics for a single schema.
• restore_table_stats restores object statistics for a single table.
In addition to the parameters specifying the target (for example, the schema and table
names for the procedure restore_table_stats), all these procedures provide the following

• as_of_timestamp restores the statistics that were in use at a specific time.
• force specifies whether locked statistics should be overwritten. Note that locks on statistics
are part of the history. This means the information about whether statistics are locked or
not is also restored with a restore. The default value is FALSE.
• no_invalidate specifies whether cursors depending on the overwritten statistics are
invalidated. This parameter accepts the values TRUE, FALSE, and dbms_stats.auto_
invalidate. The default value is dbms_stats.auto_invalidate.
The following call restores the object statistics of the schema SH to the values that were in
use one day ago. Since the parameter force is set to TRUE, the restore is done even if statistics
are currently locked.
exec dbms_stats.restore_table_stats(ownname         => 'APOLLO',
                                    tabname         => 'ORD_ORDER',
                                    as_of_timestamp => SYSTIMESTAMP - 1,
                                    no_invalidate   => FALSE);

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
Oracle ACE组成员,DBGeeK用户组发起人。曾在DTCC、ORACLE技术嘉年华、Gdevops等公开场合做过数据库技术专题分享,2017年应Oracle邀请在世界最大的数据库会议OOW上做技术分享。组织翻译了《拨云见日,解密Oracle ASM内核》一书。


  • 博文量
  • 访问量