DBAtlas · 데이터베이스 운영 아틀라스

통계를 과거 시점으로 복원

Oracle통계

명령어

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

하는 일

DBMS_STATS는 수집할 때마다 이전 통계를 SYSAUX에 저장한다(기본 31일). RESTORE_* 프로시저는 테이블/스키마 통계를 지정한 시점의 상태로 되돌린다. no_invalidate=>FALSE이면 커서가 즉시 다시 파싱된다.

사용 시점

야간 통계 수집 후 실행 계획 회귀가 발생했을 때 가장 빠른 롤백 수단으로, 변경이 통계 때문인지 테스트하기 위해.

주의

라이선스가 필요 없다. 잠긴 통계는 복원할 수 없다(먼저 잠금 해제). 다음 자동 수집이 복원 후 새 통계를 다시 가져오므로 영구 해결에는 테이블 선호 설정, 통계 잠금 또는 SQL plan baseline을 사용한다. 보존 기간이 길면 SYSAUX가 커진다(WRI$_OPTSTAT_* 테이블).

DBAtlas에서 열기 →

관련 항목