0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี

ค้นพบว่าดัชนีแบบครอบคลุมช่วยลดการเข้าถึงตารางและเปิดใช้การสแกนเฉพาะดัชนีที่มีประสิทธิภาพสูงได้อย่างไร

ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Covering Indexes?

Welcome to our lesson on Covering Indexes and Index-Only Scans! These advanced indexing techniques can dramatically boost your PostgreSQL query performance.

A regular index helps PostgreSQL quickly find rows based on certain column values. Think of it like a book's index: it tells you which page to go to for a topic.

The Cost of Table Access

When you use a standard index, PostgreSQL first finds the row's location (its tuple ID or CTID) in the index. Then, it has to go to the actual table to fetch the rest of the data for that row.

This extra step, called a table lookup or heap fetch, can be slow, especially if many rows are involved or if the table data isn't in memory.

Indexing All You Need

A covering index is an index that contains all the columns needed to fulfill a query, not just the columns in the WHERE clause. This means PostgreSQL can answer the query by reading only the index, without ever touching the main table data.

It 'covers' the query's data requirements entirely.

Building a Covering Index

To create a covering index, you include all the columns your query might select or filter on. PostgreSQL supports including non-key columns using the INCLUDE clause, which stores them without making them part of the primary sort key.

Let's create a table and then a covering index.

CREATE TABLE products (
  product_id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  price DECIMAL(10, 2) NOT NULL,
  category VARCHAR(50)
);

INSERT INTO products (name, price, category) VALUES
('Laptop', 1200.00, 'Electronics'),
('Keyboard', 75.00, 'Accessories'),
('Mouse', 25.00, 'Accessories'),
('Monitor', 300.00, 'Electronics');

-- Covering index for queries on category that also select name and price
CREATE INDEX idx_products_category_cover_name_price
ON products (category) INCLUDE (name, price);

Introducing Index-Only Scans

When a query can be fully satisfied by a covering index, PostgreSQL performs an Index-Only Scan. This is a highly efficient operation because the database engine doesn't need to read any data blocks from the main table (the 'heap').

It's like finding all the information you need directly in the book's index, without ever turning to the main chapters.

MVCC & Index-Only Scans

PostgreSQL uses MVCC (Multi-Version Concurrency Control). For an Index-Only Scan to work, PostgreSQL must ensure that the versions of the rows it finds in the index are visible to the current transaction.

It does this by checking the table's visibility map, a special data structure that tracks which table pages contain only 'all-visible' tuples. If a page is marked all-visible, no heap fetch is needed to check row visibility.

Confirming with EXPLAIN

You can verify if PostgreSQL is using an Index-Only Scan by checking the query plan with EXPLAIN ANALYZE. Look for Index Only Scan in the output.

Let's try a query that our covering index should handle.

EXPLAIN ANALYZE
SELECT name, price
FROM products
WHERE category = 'Electronics';

Benefits: Speed & Efficiency

Index-Only Scans offer several key advantages:

  • Reduced Disk I/O: Fewer disk reads are needed because the main table is skipped.
  • Faster Query Execution: Less I/O directly translates to quicker query responses.
  • Better Cache Utilization: Indexes are often smaller and more frequently accessed, leading to better use of memory caches.
  • Concurrency: Less contention on table data blocks.

Trade-offs & Considerations

While powerful, covering indexes aren't a silver bullet:

  • Larger Indexes: Including more columns makes the index physically larger, consuming more disk space.
  • Slower Writes: Updates, inserts, and deletes to the main table also require updating the index, which can be slower for larger indexes.
  • Maintenance: Larger indexes might take longer to VACUUM.

Use them strategically for read-heavy queries that frequently access a specific subset of columns.

Practical Covering Index Example

Consider a `users` table where you often query user names and emails based on their registration date. An index on `registration_date` including `name` and `email` would be a perfect candidate for a covering index.

CREATE TABLE users (
  user_id SERIAL PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  email VARCHAR(100) NOT NULL,
  registration_date DATE NOT NULL
);

INSERT INTO users (username, email, registration_date) VALUES
('alice', 'alice@example.com', '2023-01-15'),
('bob', 'bob@example.com', '2023-02-20'),
('charlie', 'charlie@example.com', '2023-01-25');

CREATE INDEX idx_users_regdate_cover_name_email
ON users (registration_date) INCLUDE (username, email);

EXPLAIN ANALYZE
SELECT username, email
FROM users
WHERE registration_date BETWEEN '2023-01-01' AND '2023-01-31';

Quick Check: Index Scans

Based on what you've learned, identify the correct statements about Index-Only Scans in PostgreSQL.

Recap: Covering Indexes

You've learned that covering indexes include all columns needed by a query, enabling highly efficient Index-Only Scans. These scans bypass the main table, significantly reducing disk I/O and speeding up read-heavy queries.

Remember to use EXPLAIN ANALYZE to confirm their usage and consider the trade-offs of increased index size and potential write overhead. Happy optimizing!

คำถามที่พบบ่อย

บทเรียน “ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี”

ค้นพบว่าดัชนีแบบครอบคลุมช่วยลดการเข้าถึงตารางและเปิดใช้การสแกนเฉพาะดัชนีที่มีประสิทธิภาพสูงได้อย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ดัชนี Hash, GIN และ GiST
  2. ดัชนีบางส่วนและดัชนีนิพจน์
  3. ดัชนีแบบครอบคลุมและการสแกนเฉพาะดัชนี
  4. ดัชนี BRIN สำหรับข้อมูลลำดับขนาดใหญ่
← กลับไปที่ PostgreSQL Performance & Query Optimization