PostgreSQL에서 쿼리 성능 개선하기
Amy McCarty
Instructor
| 시스템 | 프런트엔드 단계 | 백엔드 프로세스 | |
|---|---|---|---|
| 1 | 파서 | 쿼리를 데이터베이스로 전송 | 구문 검사. 시스템 규칙에 따라 SQL을 기계 친화적 구문으로 변환. |
| 2 | 플래너 & 옵티마이저 | 쿼리 작업 평가·최적화 | DB 통계를 사용해 쿼리 계획 수립. 비용 계산 후 최적 계획 선택. |
| 3 | 실행기 | 쿼리 결과 반환 | 쿼리 계획을 따라 실행. |
SQL 구조 변화에 반응
SELECT * FROM pg_class
WHERE relname = 'mytable'
-- sample of output columns
| relname | relhasindex |
SELECT * FROM pg_stats
WHERE tablename = 'mytable'
-- sample of output columns
null_frac | avg_width | n_distinct |
EXPLAIN
SELECT * FROM cheeses
Seq Scan on cheeses
(cost=0.00..10.50 rows=5725 width=296)
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
..10.50 : 총 비용
총 비용 = 시작 + 실행 비용
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
EXPLAIN
SELECT * FROM cheeses WHERE species IN ('goat','sheep')
Seq Scan on cheeses (cost=0.00..378.90 rows=3 width=118)
-> Filter: (species = ANY ('{"goat","sheep"}'::text[]))
EXPLAIN
SELECT * FROM cheeses WHERE species IN ('goat','sheep') -- index on species column
Bitmap Index Scan using species_idx on cheeses (cost=0.29..12.66 rows=3 width=118)
Index Cond: (species = ANY ('{"goat","sheep"}'::text[]))
PostgreSQL에서 쿼리 성능 개선하기