Join e aggregazioni partizionate
Scopra come PostgreSQL possa eseguire join e aggregazioni su tabelle partizionate, una partizione alla volta, ottenendo notevoli miglioramenti delle prestazioni.
Join e aggregazioni partizionate è una lezione Advanced PostgreSQL: Indexing, Partitioning, Replication gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Advanced PostgreSQL: Indexing, Partitioning, Replication, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Join e aggregazioni partizionate» è gratuita?
Sì — il testo completo di «Join e aggregazioni partizionate» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Advanced PostgreSQL: Indexing, Partitioning, Replication, passa a CoddyKit PRO. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.
Cosa imparerò in «Join e aggregazioni partizionate»?
Scopra come PostgreSQL possa eseguire join e aggregazioni su tabelle partizionate, una partizione alla volta, ottenendo notevoli miglioramenti delle prestazioni. Eserciti Advanced PostgreSQL: Indexing, Partitioning, Replication con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Advanced PostgreSQL: Indexing, Partitioning, Replication?
Non è richiesta alcuna esperienza precedente. Advanced PostgreSQL: Indexing, Partitioning, Replication su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Join e aggregazioni partizionate»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sì. Ogni lezione Advanced PostgreSQL: Indexing, Partitioning, Replication include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Ottimizzazione delle query con il partizionamento
- Collegamento e scollegamento delle partizioni
- Eliminazione ed esclusione delle partizioni
- Join e aggregazioni partizionate