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

การสร้างดัชนีใหม่และการดูแลดัชนี

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

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

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

Keeping Indexes Healthy

Indexes are vital for database performance, but like any component, they need maintenance. Over time, indexes can become less efficient due to fragmentation and 'bloat'.

In this lesson, we'll learn why index maintenance is crucial, how to spot issues like bloat, and how to fix them using reindexing.

What is Index Bloat?

Index bloat refers to wasted space within an index. It occurs when index entries become outdated but are not immediately removed, or when an index structure becomes inefficient.

This 'bloat' can lead to:

  • Larger index files, consuming more disk space.
  • More I/O operations, slowing down queries.
  • Reduced cache effectiveness.

How Bloat Accumulates

PostgreSQL uses a technique called MVCC (Multi-Version Concurrency Control). When you UPDATE or DELETE rows, the old versions (called 'dead tuples') aren't immediately removed from the table or its indexes.

The VACUUM process cleans up these dead tuples. However, if VACUUM doesn't run frequently enough, or if transactions hold locks preventing cleanup, dead tuples accumulate, leading to bloat.

Identifying Index Bloat

Spotting bloat can be tricky. You can't just look at file size, as it includes useful data. However, you can query PostgreSQL's system catalogs to estimate bloat by comparing the actual space used by an index to the space it should theoretically occupy.

Key tables for this are pg_class (for relation sizes) and pg_stat_user_indexes (for usage statistics).

Code: Check Index Size

While a full bloat calculation is complex, you can easily check an index's current size. A rapidly growing index size without corresponding data growth might signal bloat. Replace 'your_index_name' with an actual index.

SELECT
  c.relname AS index_name,
  pg_size_pretty(pg_relation_size(c.oid)) AS index_size
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
  AND c.relkind = 'i'
  AND c.relname = 'accounts_pkey'; -- Example: primary key index

What is Reindexing?

Reindexing is the process of rebuilding an index from scratch. When you reindex, PostgreSQL constructs a completely new, clean version of the index.

This new index is:

  • Free of bloat and fragmentation.
  • Optimized for storage and access.
  • Potentially faster for queries.

When to Reindex

Reindexing isn't a daily task, but it's important for several situations:

  • High Index Bloat: When bloat significantly increases index size and degrades performance.
  • Performance Degradation: If query plans show indexes are less effective over time.
  • Schema Changes: After major changes that might affect index structure.
  • PostgreSQL Upgrades: Sometimes recommended for optimal performance with new versions.

The REINDEX Command

PostgreSQL provides the REINDEX command to rebuild indexes. You can reindex individual indexes, all indexes on a table, or even all indexes in a database.

The CONCURRENTLY option is crucial for production systems as it allows reindexing without blocking reads or writes on the table. Without it, the table is locked during the operation.

Code: Reindex an Index

To reindex a specific index, use the REINDEX INDEX command. Remember to use CONCURRENTLY for non-blocking operations in production. Replace 'my_table_col_idx' with your actual index name.

REINDEX INDEX CONCURRENTLY my_table_col_idx; -- Example: a specific index
-- Or without CONCURRENTLY (blocks access):
-- REINDEX INDEX my_table_col_idx;

Code: Reindex a Table

You can also reindex all indexes associated with a particular table using REINDEX TABLE. This is convenient but affects all indexes on that table. Again, CONCURRENTLY is highly recommended.

REINDEX TABLE CONCURRENTLY my_table; -- Reindexes all indexes on 'my_table'
-- Or without CONCURRENTLY (blocks access):
-- REINDEX TABLE my_table;

Index Maintenance Check

Which of the following is a primary reason to use REINDEX ... CONCURRENTLY in a production PostgreSQL environment?

Your Index Maintenance Toolkit

Congratulations! You've learned about the critical aspects of PostgreSQL index maintenance.

  • You can now identify index bloat and understand how it impacts performance.
  • You know when and why to perform reindexing.
  • You've seen how to use the REINDEX command, especially with the important CONCURRENTLY option.

Regular monitoring and maintenance of your indexes will keep your PostgreSQL database running smoothly and efficiently!

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

บทเรียน “การสร้างดัชนีใหม่และการดูแลดัชนี” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสร้างดัชนีใหม่และการดูแลดัชนี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 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