zoukankan      html  css  js  c++  java
  • Oracle统计信息的备份还原

    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;

  • 相关阅读:
    MDK软件2020年到期和谐方法
    TinyMCE插件CodeSample前后端应用
    KEIL MDK编译后的代码量和RAM使用详解
    pdf密码解除工具
    Keil c中自定义带可变参数的printf函数
    关于"云服务器被检测到对外攻击已阻断该服务器对其它服务器端口的访问"的解决措施
    在 __CC_ARM 编译器环境下,使用$Sub$$ 与 $Super$$ 的“补丁”功能
    thinkphp前台html格式化输出日期
    ELINK编程器典型场景之序列号烧写
    数据在数组中存储的顺序:小端 OR 大端模式 详解
  • 原文地址:https://www.cnblogs.com/tracy/p/2056874.html
Copyright © 2011-2022 走看看