การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN
เรียนรู้การตีความตัวเลขต้นทุน จำนวนแถวโดยประมาณ และค่าความกว้างที่ EXPLAIN กำหนดให้โหนดแต่ละโหนดของแผน รวมถึงวิธีสังเกตว่าค่าประมาณของตัววางแผนผิดพลาดเมื่อใด
การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What the Cost Numbers Mean
Every node in an EXPLAIN plan shows a cost=startup..total pair. These are arbitrary planner units, not milliseconds. The planner uses them only to compare alternative plans and pick the cheapest one.
Startup vs Total Cost
The two numbers tell different stories:
- Startup cost: work before the first row is returned (e.g. building a hash table)
- Total cost: work to return all rows
A node that must sort or aggregate everything has a high startup cost.
A Sample Plan
Run EXPLAIN on a simple query and read the cost on each line.
EXPLAIN SELECT * FROM orders WHERE total > 100;The rows Estimate
Each node shows rows=N, the planner's estimated number of rows it will emit. The planner derives this from table statistics gathered by ANALYZE.
Bad estimates lead to bad plans, so this number matters a lot.
The width Value
The width=N field is the estimated average row size in bytes. Multiplied by rows it estimates memory and I/O needs, which influences whether sorts or hashes spill to disk.
Estimated vs Actual
EXPLAIN alone shows only estimates. Add ANALYZE to also run the query and see real timings and real row counts side by side.
EXPLAIN ANALYZE SELECT * FROM orders WHERE total > 100;Spotting Estimate Errors
Compare rows (estimate) with actual rows in EXPLAIN ANALYZE output. A large gap — say estimate 10 but actual 100000 — signals stale or insufficient statistics.
Why Estimates Drift
Estimates go stale when:
- Data changed a lot since the last
ANALYZE - Columns are correlated and the planner assumes independence
- The default statistics target is too low for skewed data
Refreshing Statistics
Run ANALYZE to recompute statistics for a table. Better stats produce better row estimates and therefore better plans.
ANALYZE orders;Raising the Statistics Target
For columns with skewed distributions, increase how many distinct values are sampled. A higher target means more accurate estimates at the cost of slightly slower ANALYZE.
ALTER TABLE orders ALTER COLUMN total SET STATISTICS 500;
ANALYZE orders;Cost Multipliers in postgresql.conf
Constants like seq_page_cost, random_page_cost, and cpu_tuple_cost scale how the planner weighs disk vs CPU. On SSDs, lowering random_page_cost makes index scans look cheaper.
Quick Check
Test your understanding of cost estimates.
Recap
You learned to read EXPLAIN's numbers:
- Cost is in arbitrary units; startup vs total cost differ
rowsandwidthare estimates from statistics- Compare estimated vs actual rows to spot bad stats
- Fix drift with
ANALYZEand a higher statistics target - Cost constants tune disk-vs-CPU weighting
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN”
เรียนรู้การตีความตัวเลขต้นทุน จำนวนแถวโดยประมาณ และค่าความกว้างที่ EXPLAIN กำหนดให้โหนดแต่ละโหนดของแผน รวมถึงวิธีสังเกตว่าค่าประมาณของตัววางแผนผิดพลาดเมื่อใด คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ EXPLAIN และ ANALYZE
- การตีความโหนดในแผน
- การระบุคอขวดด้านประสิทธิภาพ
- การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN