您的位置:首页 > 数据库 > Oracle

Oracle统计信息的备份还原

2011-05-25 17:19 281 查看
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);


Example:

实际应用中,可以把以前的统计信息拿回来使用真是很给力的功能。

A

begin
  dbms_stats.gather_table_stats(ownname => 'SYS', tabname =>
'TAB1', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
end;

B

select histogram,num_buckets,column_name from user_tab_col_statistics
where table_name='TAB1' and column_name='B'

C

select * FROM dba_tab_stats_history where owner='SYS' and table_name='TAB1'

D

BEGIN
   DBMS_STATS.restore_table_stats (
      ownname           => 'SYS',
      tabname           => 'TAB1',
      as_of_timestamp   => TO_DATE ('2011-5-25 5:11:01 PM',
                                    'YYYY-MM-DD HH:MI:SS PM'),
      no_invalidate     => FALSE
   );
END;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: