0Pricing
PostgreSQL Performance & Query Optimization · 课时

阅读 EXPLAIN 的成本估算与行数

学习解读 EXPLAIN 为每个计划节点附加的成本数字、估算行数和宽度值,并识别规划器估算错误的情况

阅读 EXPLAIN 的成本估算与行数 是 CoddyKit 上的免费 PostgreSQL Performance & Query Optimization 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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
  • rows and width are estimates from statistics
  • Compare estimated vs actual rows to spot bad stats
  • Fix drift with ANALYZE and a higher statistics target
  • Cost constants tune disk-vs-CPU weighting

常见问题解答

「阅读 EXPLAIN 的成本估算与行数」课时是免费的吗?

是的 — 「阅读 EXPLAIN 的成本估算与行数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PostgreSQL Performance & Query Optimization 课程的其余内容,请升级到 CoddyKit PRO。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。

「阅读 EXPLAIN 的成本估算与行数」这节课中我会学到什么?

学习解读 EXPLAIN 为每个计划节点附加的成本数字、估算行数和宽度值,并识别规划器估算错误的情况 你通过在浏览器中直接运行的动手代码来练习 PostgreSQL Performance & Query Optimization,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 PostgreSQL Performance & Query Optimization 需要有经验吗?

无需任何先前经验。CoddyKit 上的 PostgreSQL Performance & Query Optimization 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「阅读 EXPLAIN 的成本估算与行数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 PostgreSQL Performance & Query Optimization 课中编写并运行代码吗?

能。每节 PostgreSQL Performance & Query Optimization 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. EXPLAIN 与 ANALYZE 简介
  2. 解读计划节点
  3. 识别性能瓶颈
  4. 阅读 EXPLAIN 的成本估算与行数
← 返回 PostgreSQL Performance & Query Optimization