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

การปรับต้นทุนดัชนีด้วย ANALYZE และสถิติ

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

บทเรียน 4 จาก 413 ขั้นตอน

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

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

The Planner Needs Data About Data

PostgreSQL's planner picks between index scans and sequential scans using statistics about your tables: row counts, value distributions, and more.

Stale statistics lead to bad plans.

What ANALYZE Does

The ANALYZE command samples a table and updates statistics stored in the system catalogs, so the planner estimates row counts accurately.

ANALYZE orders;

Autovacuum and Autoanalyze

The autovacuum daemon also runs ANALYZE automatically when enough rows change. But heavy bulk loads may need a manual ANALYZE right away.

Reading Estimates vs Actuals

Use EXPLAIN ANALYZE to compare the planner's estimated rows with actual rows. A big gap signals stale or insufficient statistics.

EXPLAIN ANALYZE SELECT * FROM orders WHERE status = 'paid';

Statistics Target

The default_statistics_target controls sample size. Raise it on columns with skewed data for sharper histograms.

ALTER TABLE orders ALTER COLUMN status SET STATISTICS 500;

Why Bad Estimates Hurt

If the planner underestimates matching rows it may pick an index scan that becomes slow; overestimate and it may skip a perfectly good index for a seq scan.

Inspecting pg_stats

The pg_stats view exposes the collected statistics like most common values and null fraction per column.

SELECT attname, n_distinct, null_frac FROM pg_stats WHERE tablename = 'orders';

Extended Statistics

For correlated columns, create extended statistics so the planner understands dependencies between them.

CREATE STATISTICS orders_corr (dependencies) ON city, zip FROM orders;

Cost Parameters

Settings like random_page_cost tell the planner how expensive random I/O is. On SSDs, lowering it makes index scans more attractive.

SET random_page_cost = 1.1;

When the Index Is Ignored

If an index exists but is unused, suspect:

  • Stale statistics
  • Low selectivity (most rows match)
  • A type mismatch preventing index use

A Tuning Workflow

  1. Run EXPLAIN ANALYZE
  2. Check estimate vs actual gap
  3. ANALYZE or raise statistics target
  4. Adjust cost settings if needed
  5. Re-measure

Quick Check

Test your statistics tuning knowledge.

Recap

The PostgreSQL planner chooses indexes based on statistics kept fresh by ANALYZE and autovacuum.

Compare estimated versus actual rows with EXPLAIN ANALYZE, raise the statistics target for skewed columns, add extended statistics for correlated ones, and tune cost parameters to guide index choice.

เริ่มต้นได้ฟรี

เรียนรู้ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
11
บทเรียน
44

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

บทเรียน “การปรับต้นทุนดัชนีด้วย ANALYZE และสถิติ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การปรับต้นทุนดัชนีด้วย ANALYZE และสถิติ”

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

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

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

บทเรียน “การปรับต้นทุนดัชนีด้วย ANALYZE และสถิติ” ใช้เวลานานแค่ไหน

บทเรียน 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