从10G开始,若采用dbms_stats进行schema或table的分析,则会自动保存统计信息于dba_tab_stats_history。默认保存31天。且可用DBMS_STATS.RESTORE….相关的过程来恢复以前的统计信息。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
1. 相关视图:
在DBA_OPTSTAT_OPERATIONS 视图中保存了schema和database级统计信息的更改记录:
SQL> select * from DBA_OPTSTAT_OPERATIONS;
OPERATION TARGET START_TIME END_TIME
------------------------------ ---------- ----------------------------------- -----------------------------------
gather_database_stats(auto) 29-JAN-09 10.00.04.011759 PM -05:00 29-JAN-09 10.01.22.576193 PM -05:00
delete_database_stats 19-JAN-09 03.21.23.632460 PM -05:00 19-JAN-09 03.22.47.958903 PM -05:00
gather_database_stats 19-JAN-09 04.09.51.652273 PM -05:00 19-JAN-09 04.14.57.856575 PM -05:00
gather_database_stats(auto) 05-JAN-09 10.00.06.830142 PM -05:00 05-JAN-09 10.02.18.803738 PM -05:00
gather_database_stats(auto) 10-JAN-09 03.58.33.397264 PM -05:00 10-JAN-09 04.00.52.469106 PM -05:00
gather_database_stats(auto) 31-JAN-09 08.34.31.334593 PM -05:00 31-JAN-09 08.35.11.779387 PM -05:00
*_TAB_STATS_HISTORY views (ALL, DBA, or USER)视图中保存了表的统计信息修改记录:
SQL> select owner,table_name,stats_update_time from dba_tab_stats_history
2 where wner='SCOTT' and table_name='TEST';
OWNER TABLE_NAME STATS_UPDATE_TIME
---------- ------------------------------ ----------------------------------------
SCOTT TEST 05-JAN-09 10.00.56.089516 PM -05:00
SCOTT TEST 19-JAN-09 03.21.28.733443 PM -05:00
SCOTT TEST 19-JAN-09 04.10.27.977296 PM -05:00
2.查看、修改统计信息保存时间:
SQL> select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;
GET_STATS_HISTORY_RETENTION
---------------------------
31
SQL> execute DBMS_STATS.ALTER_STATS_HISTORY_RETENTION (10);
PL/SQL procedure successfully completed.
SQL> select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;
GET_STATS_HISTORY_RETENTION
---------------------------
10
3.恢复表的统计信息
SQL> select owner,table_name,stats_update_time from dba_tab_stats_history
2 where wner='SCOTT' and table_name='TEST';
OWNER TABLE_NAME STATS_UPDATE_TIME
---------- ------------------------------ ----------------------------------------
SCOTT TEST 05-JAN-09 10.00.56.089516 PM -05:00
SCOTT TEST 19-JAN-09 03.21.28.733443 PM -05:00
SCOTT TEST 19-JAN-09 04.10.27.977296 PM -05:00
SQL> alter session set nls_date_format='yyyymmdd hh24:mi:ss';
Session altered.
SQL> select owner,table_name,last_analyzed from dba_tables
2 where table_name='TEST' and wner='SCOTT';
OWNER TABLE_NAME LAST_ANALYZED
---------- ------------------------------ -----------------
SCOTT TEST 20090119 16:10:27
SQL> execute dbms_stats.restore_table_stats('SCOTT','TEST','05-JAN-09 10.00.56.089516 PM -05:00');
PL/SQL procedure successfully completed.
SQL> select owner,table_name,last_analyzed from dba_tables
2 where table_name='TEST' and wner='SCOTT';
OWNER TABLE_NAME LAST_ANALYZED
---------- ------------------------------ -----------------
SCOTT TEST 20090105 22:00:56
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-545048/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10972173/viewspace-545048/