본문으로 건너뛰기

2.1 Plan tree와 cost

Plan은 아래 child node가 row를 만들고 위 parent node가 소비하는 tree입니다. 들여쓰기로 parent-child 관계를 먼저 읽습니다.

EXPLAIN
SELECT c.region, sum(o.amount)
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= current_date - 7
GROUP BY c.region;

Node의 괄호에는 startup cost, total cost, estimated rows, row width가 표시됩니다.

cost=12.34..567.89 rows=100 width=24

Cost는 millisecond가 아니라 planner의 상대 단위입니다. seq_page_cost, random_page_cost, CPU cost와 row estimate를 이용해 후보 plan을 비교합니다.

읽는 순서

  1. 가장 안쪽 scan node에서 시작합니다.
  2. Filter와 Index Cond를 구분합니다.
  3. 예상 row가 각 parent에서 어떻게 변하는지 봅니다.
  4. Join, sort, aggregate의 입력량을 계산합니다.
  5. 최상단 total cost와 output row를 확인합니다.

Index Cond는 index가 후보 row를 줄이는 조건이고 Filter는 가져온 뒤 제거하는 조건일 수 있습니다. Rows Removed by Filter가 크면 index 정의나 query 조건을 검토합니다.

참고: Using EXPLAIN