Many DBA's have experienced a case where statistics have been regenerated only to find that those statistics cause terrible things to happen to their execution plans. This can be caused in many ways. It could be that the statistics were generated using an incorrect method. Sometimes what happens is that a table was empty when the statistics were collected, which causes full table scans to suddenly appear. Prior to Oracle 11g, it was common to export your statistics before you regenerated them. This would provide you the ability to re-import those statistics into the data dictionary should the new statistics cause problems. This was a manual process that could be very time consuming. Oracle 11g allows you to restore previous versions of statistics. Restoring Old Statistics Oracle also provides historical views that will provide the ability to review when statistics were run, and which statistics you can restore. You can use the related timestamps recorded in these views and one of the several procedures in the DBMS_STATS PL/SQL package to restore statistics based on a specific timestamp. The procedures available within DBMS_STATS that you can use to restore various statistics include: DBMS_STATS.RESTORE_DICTIONARY_STATS - Used to restore data dictionary stats DBMS_STATS.RESTORE_FIXED_OBJECTS_STATS - Used to restore fixed object stats DBMS_STATS.RESTORE_SCHEMA_STATS - Used to restore schema stats DBMS_STATS.RESTORE_SYSTEM_STATS - Used to restore system stats DBMS_STATS.RESTORE_TABLE_STATS - Used to restore table stats An example is probably worth looking at. First, you can use the view DBA_OPSTAT_OPERATIONS to determine when you last gathered statistics: SELECT * FROM dba_optstat_operations; OPERATION TARGET START_TIME ----------------------------- ------------------------------------ END_TIME ------------------------------------ gather_database_stats(auto) 19-AUG-07 06.00.08.477333 AM -06:00 19-AUG-07 06.02.28.607562 AM -06:00 Having determined that you gathered statistics on the 19th of August, you can restore the QUEST schema statistics back to that point in time: exec dbms_stats.restore_schema_stats('QUEST', - '19-AUG-07 06.00.08.477333 AM -06:00'); Maintaining the Historical Statistics Repository The repository for historical statistics can grow large. Oracle 11g manages the growth of the repository by purging old statistics on a regular basis. By default statistics are purged that are older than 31 days. As a result, you will not be able to rollback to a set of statistics older than 31 days. Retention can be adjusted using the procedure DBMS_STATS.ALTER_STATS_HISTORY_RETENTION . To manually purge statistics you can use the DBMS_STATS.PURGE_STATS procedure. There are two other functions in DBMS_STATS related to the statistics repository that might come in handy: DBMS_STATS.GET_STATS_HISTORY_RETENTION - This function returns the current retention value DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY - This function returns the oldest timestamp that can be used to restore statistics Data Dictionary and Historical Statistics Views Two views are introduced in Oracle 11g that you can use to view historical statistics. These views can be very helpful when you are trying to determine exactly which statistics you want to recover. The views are: DBA_OPTSTAT_OPERATIONS - This view contains a history of statistics-related operations performed at the schema and database level *_TAB_STATS_HISTORY - This view contains a history of table statistics modifications
↧