C.W.K.
Stream
Lesson 04 of 10 · published

EXPLAIN QUERY PLAN — 진실 읽기

~14 min · explain, query-plan, performance

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

추측 멈추고 엔진한테 물어봐

아무 query 앞에 EXPLAIN QUERY PLAN 붙이면 SQLite 가 실제로 뭐 할지 알려줘. Operation 트리 출력; 봐야 할 단어:

  • SCAN — full scan. 작은 테이블엔 싸고 큰 테이블엔 비쌈.
  • SEARCH — 인덱스 lookup (좋음).
  • USING INDEX X — 어떤 인덱스 골랐나.
  • USING COVERING INDEX X — query 가 필요한 모든 컬럼이 인덱스 안 — 실제 테이블 안 만짐 (excellent).
  • USE TEMP B-TREE FOR ORDER BY — SQLite 가 정렬에 인덱스 못 씀, 임시로 만듦. 정렬 인덱스 빠진 신호.
Tip: EXPLAIN QUERY PLAN 이 muscle memory 여야 함. 최적화 전 explain. 후 다시 explain — plan 바뀌었는지 확인. Plan 안 바뀐 'fix' 는 fix 안 한 거.

Code

Plan 읽기·sql
EXPLAIN QUERY PLAN
SELECT * FROM messages WHERE conversation_id = 1 ORDER BY created_at DESC LIMIT 20;

-- 유용한 인덱스 없이:
-- 0|0|0|SCAN messages
-- 0|0|0|USE TEMP B-TREE FOR ORDER BY

-- CREATE INDEX idx_msg_conv_created ON messages(conversation_id, created_at DESC) 후:
-- 0|0|0|SEARCH messages USING INDEX idx_msg_conv_created (conversation_id=?)
Plan-driven 최적화 workflow·bash
# 1. 느린 query 식별
sqlite3 myapp.db 'EXPLAIN QUERY PLAN SELECT ...'

# 2. 인덱스 추가/조정
sqlite3 myapp.db 'CREATE INDEX idx_... ON ...'

# 3. 다시 explain — plan 바뀐 거 확인
sqlite3 myapp.db 'EXPLAIN QUERY PLAN SELECT ...'

# 4. Wall-clock 개선 확인
sqlite3 myapp.db '.timer on' 'SELECT ...'

External links

Exercise

앱에서 느려 보이는 query 3 개. 각각 EXPLAIN QUERY PLAN. SCAN vs SEARCH, ORDER BY 가 TEMP B-TREE 쓰나, composite 인덱스 쓰였나 기록. 각각 plan 바꿀 인덱스 제안 + 시도.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.