인덱싱과 쿼리 최적화
데이터베이스 인덱스의 작동 방식과 사용 시점을 이해하고, 쓰기 성능을 심각하게 저하시키지 않으면서 읽기 쿼리를 빠르게 최적화하는 방법을 학습해 보세요.
인덱싱과 쿼리 최적화은(는) CoddyKit의 무료 System Design Basics for Backend Developers 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 System Design Basics for Backend Developers 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. System Design Basics for Backend Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Indexes Matter
Without an index, finding a row means scanning every row — a full table scan. As tables grow into millions of rows, this becomes painfully slow.
An index is a separate data structure that lets the database jump straight to matching rows.
The B-Tree Index
Most relational indexes use a B-tree: a balanced tree that keeps keys sorted. Lookups, range scans, and ordering all become logarithmic instead of linear.
- Fast equality lookups (
WHERE id = 5) - Fast range queries (
WHERE age > 30) - Supports
ORDER BYwithout re-sorting
Creating an Index
You create an index on the column(s) you frequently filter or sort by.
Here we index the email column so login lookups are instant.
CREATE INDEX idx_users_email
ON users (email);
SELECT * FROM users
WHERE email = 'a@example.com';Composite Indexes
A composite index covers multiple columns. Column order matters: the index helps queries that filter on a left-prefix of the columns.
An index on (country, city) helps WHERE country = ? and WHERE country = ? AND city = ?, but not WHERE city = ? alone.
CREATE INDEX idx_loc
ON customers (country, city);Covering Indexes
If an index contains every column a query needs, the database answers from the index alone and never touches the table. This is a covering index.
It is one of the most powerful read optimizations available.
The Write Cost
Indexes are not free. Every INSERT, UPDATE, or DELETE must also update each affected index.
- More indexes = slower writes
- More indexes = more storage
Index for the queries you actually run, not speculatively.
Reading EXPLAIN
Use EXPLAIN (or EXPLAIN ANALYZE) to see the query plan. Look for Index Scan (good) versus Seq Scan (full table scan).
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE customer_id = 42;Selectivity
An index helps most when the column is highly selective — it filters down to a tiny fraction of rows. Indexing a boolean is_active with a 50/50 split is nearly useless; the planner may ignore it.
Avoiding Index-Defeating Queries
Wrapping an indexed column in a function or doing a leading wildcard defeats the index.
WHERE LOWER(email) = ?— index onemailunusedWHERE name LIKE '%son'— leading wildcard, no index
Store data in the form you query, or use a functional index.
-- Defeats the index:
SELECT * FROM users WHERE LOWER(email) = 'a@x.com';
-- Better: store email already lowercasedIndexing and Sharding Together
In a sharded system each shard maintains its own indexes. A query that includes the shard key hits one shard and one index; a query without it must fan out to every shard. Design indexes and shard keys together.
A Practical Workflow
Optimize iteratively: find slow queries from logs, run EXPLAIN, add a targeted (often composite or covering) index, re-measure, and drop indexes that are never used.
Quick Check
Test your understanding of indexing.
Recap
You learned how to make reads fast with indexes:
- B-tree indexes power equality, range, and ordering
- Composite indexes follow the left-prefix rule
- Covering indexes answer queries without touching the table
- Indexes cost write speed and storage — index deliberately
- Use EXPLAIN and watch for index-defeating patterns
자주 묻는 질문
“인덱싱과 쿼리 최적화” 강의는 무료인가요?
네 — “인덱싱과 쿼리 최적화” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 System Design Basics for Backend Developers 강의 전체를 잠금 해제할 수 있습니다. System Design Basics for Backend Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
“인덱싱과 쿼리 최적화”에서 뭘 배우나요?
데이터베이스 인덱스의 작동 방식과 사용 시점을 이해하고, 쓰기 성능을 심각하게 저하시키지 않으면서 읽기 쿼리를 빠르게 최적화하는 방법을 학습해 보세요. 브라우저에서 직접 실행하는 실습 코드로 System Design Basics for Backend Developers을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
System Design Basics for Backend Developers을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 System Design Basics for Backend Developers은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“인덱싱과 쿼리 최적화” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 System Design Basics for Backend Developers 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 System Design Basics for Backend Developers 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- SQL과 NoSQL 데이터베이스
- 샤딩과 데이터 복제
- 데이터 일관성 모델
- 인덱싱과 쿼리 최적화