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

상관관계가 있는 컬럼에 확장 통계 생성(CREATE STATISTICS)

PostgreSQL유지보수

명령어

-- city and district are correlated: the planner assumes independence and underestimates rows
CREATE STATISTICS customer_city_district_stx (dependencies, ndistinct, mcv) ON city, district FROM customer;
ANALYZE customer;

-- Collected statistics
SELECT statistics_name, attnames, kinds, n_distinct, dependencies
FROM pg_stats_ext
WHERE tablename = 'customer';

-- Verify: estimated rows should now be close to actual rows
EXPLAIN (ANALYZE, TIMING OFF)
SELECT * FROM customer WHERE city = 'Ankara' AND district = 'Cankaya';

하는 일

플래너에 컬럼 간 상관관계(functional dependency, 결합 distinct 수, 결합 most-common value)를 알려 주어 다중 컬럼 WHERE 및 GROUP BY 조건의 행 추정을 바로잡는다.

사용 시점

다중 컬럼 필터에서 EXPLAIN ANALYZE의 추정 행 수가 실제보다 크게 낮고(예: 12 vs. 570) 그 결과 잘못된 조인 방식/인덱스가 선택될 때.

주의

ANALYZE 후에만 적용된다. 컬럼 조합 수가 statistics target(기본값 100)보다 훨씬 많으면 mcv 목록이 대부분을 포함하지 못하며, dependencies로 바로잡을 수 있는 추정을 mcv가 바로잡지 못할 수 있다. EXPLAIN으로 확인하고 필요하면 정의에서 mcv를 빼거나 ALTER STATISTICS customer_city_district_stx SET STATISTICS 1000;으로 target을 높인다. dependencies는 등호/IN 조건에만, ndistinct는 GROUP BY 추정에 사용된다. 정의마다 ANALYZE가 길어지므로 오추정이 입증된 컬럼 그룹에만 추가한다.

DBAtlas에서 열기 →

관련 항목