Restaurar estadísticas a un punto del pasado
OracleEstadísticasComando
-- 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);Qué hace
DBMS_STATS guarda las estadísticas anteriores en SYSAUX antes de cada recopilación (31 días por defecto). Los procedimientos RESTORE_* devuelven las estadísticas de la tabla/esquema a su estado en la hora indicada; con no_invalidate=>FALSE los cursores se vuelven a parsear de inmediato.
Cuándo usarlo
Como el rollback más rápido cuando una regresión de plan sigue a la recopilación nocturna de estadísticas, y para comprobar si un cambio fue causado por las estadísticas.
Precaución
No requiere licencia. Las estadísticas bloqueadas no se pueden restaurar (desbloquee primero). La siguiente recopilación automática vuelve a traer estadísticas nuevas tras la restauración; para una solución permanente defina preferencias de tabla, bloquee las estadísticas o use un SQL plan baseline. Una retención larga hace crecer SYSAUX (tablas WRI$_OPTSTAT_*).