ดัชนีแบบผสมและแบบครอบคลุม
เร่งการสืบค้นหลายคอลัมน์ด้วยดัชนีแบบผสม และกำจัดการค้นหาตารางโดยสิ้นเชิงด้วยดัชนีแบบครอบคลุมที่ใช้ INCLUDE
ดัชนีแบบผสมและแบบครอบคลุม เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Single-Column Indexes
Many queries filter on more than one column. A composite index spans multiple columns and can serve such queries far better than separate single-column indexes.
Creating a Composite Index
List columns in the order you want them indexed.
CREATE INDEX idx_orders_cust_date
ON orders (customer_id, order_date);Column Order Matters
A composite index on (a, b) helps queries filtering on a alone or a AND b, but generally NOT queries filtering only on b.
The Leftmost Prefix Rule
PostgreSQL can use any leftmost prefix of the index. So (a, b, c) serves filters on a, on a+b, and on a+b+c.
Ordering by Selectivity
Put the most selective or most frequently filtered column first, often an equality column before a range column, to maximize how many rows the index eliminates early.
CREATE INDEX idx_orders_status_date
ON orders (status, order_date);The Heap Lookup Cost
A normal index scan finds matching rows then visits the table (the heap) to read the other columns. Those extra fetches cost I/O.
Covering Indexes
A covering index contains every column the query needs, so PostgreSQL answers from the index alone, an Index Only Scan, with no heap visit.
Using INCLUDE
The INCLUDE clause adds non-key columns to the index leaf pages so they are available without being part of the searchable key.
CREATE INDEX idx_orders_cust_incl
ON orders (customer_id)
INCLUDE (order_date, total);Confirming Index Only Scan
Check the plan; you want to see Index Only Scan rather than Index Scan plus heap fetches.
EXPLAIN ANALYZE
SELECT order_date, total
FROM orders WHERE customer_id = 42;The Visibility Map Caveat
Index Only Scans still need the heap when pages are not marked all-visible. Keeping tables well-vacuumed maintains the visibility map so scans stay index-only.
VACUUM ANALYZE orders;Costs of Wide Indexes
More columns mean larger indexes and slower writes. Add covering columns only for hot queries, not everywhere.
Quick Check
Test what you have learned.
Recap
You learned how composite indexes serve multi-column filters under the leftmost prefix rule, how column order and selectivity matter, and how covering indexes with INCLUDE enable Index Only Scans, balanced against larger size and write cost.
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “ดัชนีแบบผสมและแบบครอบคลุม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ดัชนีแบบผสมและแบบครอบคลุม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ดัชนีแบบผสมและแบบครอบคลุม”
เร่งการสืบค้นหลายคอลัมน์ด้วยดัชนีแบบผสม และกำจัดการค้นหาตารางโดยสิ้นเชิงด้วยดัชนีแบบครอบคลุมที่ใช้ INCLUDE คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ดัชนีแบบผสมและแบบครอบคลุม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานดัชนี B-Tree
- การสร้างและใช้ดัชนี
- ควรสร้างดัชนีเมื่อใดและอย่างไร
- ดัชนีแบบผสมและแบบครอบคลุม