DBAtlas · Database operations atlas

Restore statistics to a point in the past

OracleStatistics

Command

-- 1) Retention and the oldest point you can return to:
SELECT DBMS_STATS.GET_STATS_HISTORY_RETENTION    AS retention_days,
       DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY AS oldest_available
FROM   dual;

-- 2) Statistics history of the table:
SELECT table_name, partition_name,
       TO_CHAR(stats_update_time,'YYYY-MM-DD HH24:MI:SS TZH:TZM') AS stats_update_time
FROM   dba_tab_stats_history
WHERE  owner = 'CUSTOMER_APP' AND table_name = 'SALES'
ORDER  BY stats_update_time DESC;

-- 3) Return to the point before the plan regressed:
BEGIN
  DBMS_STATS.RESTORE_TABLE_STATS(
    ownname         => 'CUSTOMER_APP',
    tabname         => 'SALES',
    as_of_timestamp => TIMESTAMP '2026-10-07 22:00:00 +03:00',
    no_invalidate   => FALSE);
END;
/
-- Whole schema: DBMS_STATS.RESTORE_SCHEMA_STATS('CUSTOMER_APP', <timestamp>)

-- Change the retention (days):
-- EXEC DBMS_STATS.ALTER_STATS_HISTORY_RETENTION(31);

What it does

DBMS_STATS saves the previous statistics in SYSAUX before every gather (31 days by default). The RESTORE_* procedures return table/schema statistics to their state at the given time; with no_invalidate=>FALSE cursors are re-parsed immediately.

When to use it

As the fastest rollback when a plan regression follows the nightly statistics gathering, and to test whether a change was caused by statistics.

Caution

No license required. Locked statistics cannot be restored (unlock first). The next automatic gather brings new statistics back after the restore; for a permanent fix set table preferences, lock the statistics, or use a SQL plan baseline. A long retention grows SYSAUX (WRI$_OPTSTAT_* tables).

Open in DBAtlas →

Related