使用 ANALYZE 和统计信息调节索引成本
学习 PostgreSQL 规划器如何依赖表统计信息选择索引,以及如何及时更新统计信息,让查询计划始终保持高效。
使用 ANALYZE 和统计信息调节索引成本 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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
- Run EXPLAIN ANALYZE
- Check estimate vs actual gap
- ANALYZE or raise statistics target
- Adjust cost settings if needed
- 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.
常见问题解答
「使用 ANALYZE 和统计信息调节索引成本」课时是免费的吗?
是的 — 「使用 ANALYZE 和统计信息调节索引成本」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
「使用 ANALYZE 和统计信息调节索引成本」这节课中我会学到什么?
学习 PostgreSQL 规划器如何依赖表统计信息选择索引,以及如何及时更新统计信息,让查询计划始终保持高效。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 ANALYZE 和统计信息调节索引成本」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?
能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 EXPLAIN 分析查询计划
- 索引使用情况监控
- 重建索引与索引维护
- 使用 ANALYZE 和统计信息调节索引成本