0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · บทเรียน

การตรวจสอบการใช้งานดัชนี

เรียนรู้การตรวจสอบประสิทธิผลของดัชนีด้วยมุมมองของระบบ และค้นหาดัชนีที่ไม่ได้ใช้งานหรือทำงานได้ต่ำกว่าที่ควร

การตรวจสอบการใช้งานดัชนี เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Monitor Index Usage?

Indexes are powerful tools for speeding up queries, but they aren't free. They consume disk space and add overhead to data modifications (INSERT, UPDATE, DELETE).

Monitoring index usage helps us understand if our indexes are actually working for us or just taking up space.

Introducing `pg_stat_user_indexes`

PostgreSQL provides several system views to monitor database activity. For index usage, the pg_stat_user_indexes view is your best friend.

This view tracks statistics for indexes on user-defined tables, giving you insights into how often each index is being scanned.

Key Index Usage Metrics

When you query pg_stat_user_indexes, look out for these columns:

  • idx_scan: The number of times the index has been scanned.
  • idx_tup_read: The number of index entries returned by scans.
  • idx_tup_fetch: The number of live table rows fetched through the index.

These tell you how frequently and effectively an index is being used.

Finding Unused Indexes

The easiest win in index optimization is identifying indexes that are never used. An index with idx_scan = 0 is a strong candidate for removal.

Removing unused indexes can reduce disk space, speed up writes, and simplify database maintenance.

Demo: Querying Unused Indexes

Let's run a query to find all indexes that have never been scanned since the last statistics reset. Try running this example:

SELECT
  relname AS table_name,
  indexrelname AS index_name,
  idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY table_name, index_name;

Beyond Unused: Underperforming Indexes

An index might be used (idx_scan > 0) but still be 'underperforming' if it's not chosen by the query planner when it should be, or if it's leading to many sequential scans on the table itself.

To spot these, we need to compare index usage with overall table access patterns.

Table Scan Insights with `pg_stat_user_tables`

The pg_stat_user_tables view provides statistics at the table level. Key columns here are:

  • seq_scan: Number of sequential scans initiated on this table.
  • idx_scan: Number of index scans initiated on this table.

A high seq_scan count on a large table often indicates a missing or ineffective index.

Comparing Sequential vs. Index Scans

By comparing seq_scan and idx_scan from pg_stat_user_tables, we can identify tables that are frequently being scanned sequentially, even if indexes exist.

A high ratio of sequential scans to index scans on a table suggests potential indexing issues or queries that aren't utilizing available indexes.

Demo: Scan Ratio Query

This query calculates the percentage of sequential scans for each table. Tables with a high percentage of seq_scan might need attention.

SELECT
  relname AS table_name,
  seq_scan,
  idx_scan,
  (seq_scan * 100.0) / (CASE WHEN seq_scan + idx_scan = 0 THEN 1 ELSE seq_scan + idx_scan END) AS seq_scan_percent
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY seq_scan_percent DESC;

Quick Check

You're trying to find indexes that are consuming disk space but are never being used by any query. Which PostgreSQL system view would you primarily consult for this information?

Recap & Next Steps

Great job! In this lesson, you learned how to monitor index effectiveness using PostgreSQL's system views.

  • pg_stat_user_indexes helps find unused indexes (idx_scan = 0).
  • pg_stat_user_tables reveals the balance between sequential and index scans on tables.
  • By combining these, you can identify indexes that are candidates for removal or further investigation.

In the next lesson, we'll dive into reindexing and maintaining index health!

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

บทเรียน “การตรวจสอบการใช้งานดัชนี” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตรวจสอบการใช้งานดัชนี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบการใช้งานดัชนี”

เรียนรู้การตรวจสอบประสิทธิผลของดัชนีด้วยมุมมองของระบบ และค้นหาดัชนีที่ไม่ได้ใช้งานหรือทำงานได้ต่ำกว่าที่ควร คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การตรวจสอบการใช้งานดัชนี” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม

ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การวิเคราะห์แผนการสอบถามด้วย EXPLAIN
  2. การตรวจสอบการใช้งานดัชนี
  3. การสร้างดัชนีใหม่และการดูแลดัชนี
  4. การปรับต้นทุนดัชนีด้วย ANALYZE และสถิติ
← กลับไปที่ Advanced PostgreSQL: Indexing, Partitioning, Replication