Uniones y agregaciones por partición
Descubra cómo PostgreSQL puede unir y agregar tablas particionadas partición por partición para obtener importantes mejoras de rendimiento.
Uniones y agregaciones por partición es una lección gratuita de Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Advanced PostgreSQL: Indexing, Partitioning, Replication, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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.
Preguntas frecuentes
¿La lección «Uniones y agregaciones por partición» es gratis?
Sí — el texto completo de «Uniones y agregaciones por partición» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, actualiza a CoddyKit PRO. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
¿Qué aprenderé en «Uniones y agregaciones por partición»?
Descubra cómo PostgreSQL puede unir y agregar tablas particionadas partición por partición para obtener importantes mejoras de rendimiento. Practicas Advanced PostgreSQL: Indexing, Partitioning, Replication con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Advanced PostgreSQL: Indexing, Partitioning, Replication?
No se requiere experiencia previa. Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Uniones y agregaciones por partición»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sí. Cada lección de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Optimización de consultas con particionamiento
- Adjuntar y separar particiones
- Poda y exclusión de particiones
- Uniones y agregaciones por partición