复合索引与覆盖索引
使用复合索引加速多列查询,并通过带有 INCLUDE 的覆盖索引完全消除表查找
复合索引与覆盖索引 是 CoddyKit 上的免费 PostgreSQL Performance & Query Optimization 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 PostgreSQL Performance & Query Optimization 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Single-Column Indexes
Many queries filter on more than one column. A composite index spans multiple columns and can serve such queries far better than separate single-column indexes.
Creating a Composite Index
List columns in the order you want them indexed.
CREATE INDEX idx_orders_cust_date
ON orders (customer_id, order_date);Column Order Matters
A composite index on (a, b) helps queries filtering on a alone or a AND b, but generally NOT queries filtering only on b.
The Leftmost Prefix Rule
PostgreSQL can use any leftmost prefix of the index. So (a, b, c) serves filters on a, on a+b, and on a+b+c.
Ordering by Selectivity
Put the most selective or most frequently filtered column first, often an equality column before a range column, to maximize how many rows the index eliminates early.
CREATE INDEX idx_orders_status_date
ON orders (status, order_date);The Heap Lookup Cost
A normal index scan finds matching rows then visits the table (the heap) to read the other columns. Those extra fetches cost I/O.
Covering Indexes
A covering index contains every column the query needs, so PostgreSQL answers from the index alone, an Index Only Scan, with no heap visit.
Using INCLUDE
The INCLUDE clause adds non-key columns to the index leaf pages so they are available without being part of the searchable key.
CREATE INDEX idx_orders_cust_incl
ON orders (customer_id)
INCLUDE (order_date, total);Confirming Index Only Scan
Check the plan; you want to see Index Only Scan rather than Index Scan plus heap fetches.
EXPLAIN ANALYZE
SELECT order_date, total
FROM orders WHERE customer_id = 42;The Visibility Map Caveat
Index Only Scans still need the heap when pages are not marked all-visible. Keeping tables well-vacuumed maintains the visibility map so scans stay index-only.
VACUUM ANALYZE orders;Costs of Wide Indexes
More columns mean larger indexes and slower writes. Add covering columns only for hot queries, not everywhere.
Quick Check
Test what you have learned.
Recap
You learned how composite indexes serve multi-column filters under the leftmost prefix rule, how column order and selectivity matter, and how covering indexes with INCLUDE enable Index Only Scans, balanced against larger size and write cost.
常见问题解答
「复合索引与覆盖索引」课时是免费的吗?
是的 — 「复合索引与覆盖索引」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PostgreSQL Performance & Query Optimization 课程的其余内容,请升级到 CoddyKit PRO。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。
「复合索引与覆盖索引」这节课中我会学到什么?
使用复合索引加速多列查询,并通过带有 INCLUDE 的覆盖索引完全消除表查找 你通过在浏览器中直接运行的动手代码来练习 PostgreSQL Performance & Query Optimization,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 PostgreSQL Performance & Query Optimization 需要有经验吗?
无需任何先前经验。CoddyKit 上的 PostgreSQL Performance & Query Optimization 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「复合索引与覆盖索引」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 PostgreSQL Performance & Query Optimization 课中编写并运行代码吗?
能。每节 PostgreSQL Performance & Query Optimization 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- B-Tree 索引基础
- 创建和使用索引
- 何时以及如何创建索引
- 复合索引与覆盖索引