统计信息历史的备份、还原功能

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;
RETENTION
----------
31

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.

Views
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$';
STATS_UPDATE_TIME
-------------------------------------------------
05-MAY-07 06.58.48.005969 AM +02:00
11-MAY-07 10.01.00.898243 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
parameters:

• 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博客 ” ,链接:http://blog.itpub.net/22034023/viewspace-689175/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22034023/viewspace-689175/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值