分区级连接与聚合
了解 PostgreSQL 如何逐个分区地连接和聚合分区表,从而大幅提升性能。
分区级连接与聚合 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Are Partition-wise Operations
When two tables are partitioned the same way, PostgreSQL can join matching partitions to each other instead of joining the whole tables. This is a partition-wise join.
The same idea applies to grouping: a partition-wise aggregate.
Enabling the Feature
These optimizations are off by default because they increase planning cost. Turn them on per session or in postgresql.conf.
SET enable_partitionwise_join = on;
SET enable_partitionwise_aggregate = on;Matching Partition Boundaries
For a partition-wise join the tables must share the same partition key type and identical bounds. Otherwise the planner cannot pair partitions.
Example Setup
Two tables partitioned by the same range on customer_id.
CREATE TABLE orders (customer_id int, total numeric)
PARTITION BY RANGE (customer_id);
CREATE TABLE refunds (customer_id int, amount numeric)
PARTITION BY RANGE (customer_id);The Join
With matching partitions, this join runs as several small joins, each fitting in memory more easily.
SELECT o.customer_id, sum(o.total), sum(r.amount)
FROM orders o
JOIN refunds r USING (customer_id)
GROUP BY o.customer_id;Why It Is Faster
Smaller per-partition joins mean:
- Smaller hash tables that fit in
work_mem - Better cache locality
- Opportunities for parallel workers per partition
Reading the Plan
Use EXPLAIN to confirm. You will see an Append node over several join nodes rather than one giant join.
EXPLAIN
SELECT * FROM orders o JOIN refunds r USING (customer_id);Partition-wise Aggregate
If you group by the partition key, each partition is aggregated independently and the results are concatenated. No global sort or hash across the whole table is needed.
SELECT customer_id, sum(total)
FROM orders
GROUP BY customer_id;Planning Cost Trade-off
With many partitions, considering each one increases planning time. Enable these settings only when partition counts are moderate and the query benefits clearly.
Combining with Pruning
Partition-wise joins combine well with partition pruning: pruning removes irrelevant partitions first, then the remaining ones are joined pair-by-pair.
When It Does Not Apply
If only one table is partitioned, or the bounds differ, PostgreSQL falls back to a normal join over the appended partitions.
Quick Check
What is required for a partition-wise join?
Recap
You learned to speed up queries on partitioned tables with partition-wise joins and aggregates. Enable the settings, ensure matching bounds, verify with EXPLAIN, and combine with pruning for the best results.
常见问题解答
「分区级连接与聚合」课时是免费的吗?
是的 — 「分区级连接与聚合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
「分区级连接与聚合」这节课中我会学到什么?
了解 PostgreSQL 如何逐个分区地连接和聚合分区表,从而大幅提升性能。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「分区级连接与聚合」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?
能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。