DBAtlas · Atlas de operações de banco de dados

Restaurar estatísticas a um ponto no passado

OracleEstatísticas

Comando

-- 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);

O que faz

O DBMS_STATS salva as estatísticas anteriores no SYSAUX antes de toda coleta (31 dias por padrão). As procedures RESTORE_* devolvem as estatísticas de tabela/schema ao estado do horário informado; com no_invalidate=>FALSE os cursores são reanalisados imediatamente.

Quando usar

Como o rollback mais rápido quando uma regressão de plano segue a coleta noturna de estatísticas e para testar se uma mudança foi causada pelas estatísticas.

Atenção

Não exige licença. Estatísticas bloqueadas não podem ser restauradas (desbloqueie antes). A próxima coleta automática traz novas estatísticas de volta após o restore; para uma correção permanente defina preferências da tabela, bloqueie as estatísticas ou use um SQL plan baseline. Uma retenção longa faz o SYSAUX crescer (tabelas WRI$_OPTSTAT_*).

Abrir no DBAtlas →

Relacionados