3.1 ANALYZE와 statistics target
ANALYZE는 table sample에서 distinct value, null fraction, most common values, histogram, correlation을 계산합니다. Autovacuum이 보통 자동 실행하지만 대량 적재나 분포 변화 직후에는 수동 분석이 필요할 수 있습니다.
ANALYZE orders;
SELECT attname, null_frac, n_distinct, correlation,
most_common_vals, most_common_freqs
FROM pg_stats
WHERE schemaname = 'public' AND tablename = 'orders';
Statistics target을 올리면 더 큰 sample과 자세한 통계를 사용하지만 analyze 시간과 catalog 공간, planning 비용이 증가할 수 있습니다.
ALTER TABLE orders
ALTER COLUMN customer_id SET STATISTICS 500;
ANALYZE orders (customer_id);
Global default_statistics_target을 무작정 올리기보다 estimate가 나쁜 핵심 column부터 조정합니다.
검증
- 변경 전
EXPLAIN (ANALYZE)를 저장합니다. - Target과
ANALYZE를 적용합니다. - Estimated rows가 actual에 가까워졌는지 봅니다.
- Plan과 latency, planning time을 비교합니다.
- 다른 parameter에서도 회귀가 없는지 확인합니다.