데이터베이스
어려움
이론
실행 계획 분석
EXPLAIN 분석으로 쿼리 최적화
40분
85점
#3755
문제 설명
주어진 쿼리의 실행 계획을 분석하고 최적화 방안을 제시하세요.
테이블 정보
orders: 5,000만 건
- id (PK), customer_id (FK), status, total, created_at
- INDEX: idx_customer (customer_id)
- INDEX: idx_created (created_at)
order_items: 2억 건
- id (PK), order_id (FK), product_id (FK), quantity, price
- INDEX: idx_order (order_id)
products: 10만 건
- id (PK), name, category_id, price
- INDEX: idx_category (category_id)
분석할 쿼리
-- 쿼리 1: 실행 시간 45초
EXPLAIN ANALYZE
SELECT c.name, COUNT(*) as order_count, SUM(o.total) as total_amount
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.created_at > NOW() - INTERVAL 30 DAY
GROUP BY c.id
HAVING SUM(o.total) > 1000000
ORDER BY total_amount DESC
LIMIT 100;
-- 실행 계획:
-- Limit (cost=1234567.89..1234567.99)
-- -> Sort (cost=1234567.89..1234567.89)
-- -> HashAggregate (cost=1234000.00..1234500.00)
-- -> Nested Loop (cost=0.00..1230000.00)
-- -> Seq Scan on customers (cost=0.00..10000.00)
-- -> Index Scan on orders (cost=0.00..1220000.00)
-- Filter: created_at > ...
분석 항목
- 실행 계획 해석
- 병목 지점 식별
- 인덱스 추가/수정 제안
- 쿼리 리팩토링
- 예상 개선 효과