การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น
ทำความเข้าใจว่าดัชนีฐานข้อมูลทำงานอย่างไร ควรใช้เมื่อใด และจะเพิ่มประสิทธิภาพคำค้นเพื่อให้อ่านข้อมูลได้รวดเร็วโดยไม่ทำให้การเขียนช้าลงอย่างมากได้อย่างไร
การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
คำถามที่พบบ่อย
บทเรียน “การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น”
ทำความเข้าใจว่าดัชนีฐานข้อมูลทำงานอย่างไร ควรใช้เมื่อใด และจะเพิ่มประสิทธิภาพคำค้นเพื่อให้อ่านข้อมูลได้รวดเร็วโดยไม่ทำให้การเขียนช้าลงอย่างมากได้อย่างไร คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม
ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ฐานข้อมูล SQL เทียบกับ NoSQL
- การแบ่งส่วนและการจำลองข้อมูล
- รูปแบบความสอดคล้องของข้อมูล
- การทำดัชนีและการเพิ่มประสิทธิภาพคำค้น