Indici BRIN per grandi tabelle partizionate
Impari quando i Block Range Indexes offrono prestazioni migliori degli alberi B-tree su enormi tabelle partizionate e ordinate naturalmente.
Indici BRIN per grandi tabelle 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.
The Scale Problem
On tables with billions of rows, a B-tree index can grow huge and consume a lot of memory. BRIN (Block Range Index) offers a tiny alternative for naturally ordered data.
How BRIN Works
BRIN stores the min and max value per block range instead of one entry per row. A query checks which ranges could contain matching rows and scans only those blocks.
Correlation Is Key
BRIN shines when the column's physical order correlates with its value order, such as an inserted_at timestamp on an append-only table. Random ordering makes BRIN nearly useless.
Creating a BRIN Index
You declare it with USING brin. It is dramatically smaller than a B-tree on the same column.
CREATE INDEX ON measurements USING brin (recorded_at);pages_per_range
The pages_per_range storage parameter controls granularity. Smaller values give more precise (but larger) indexes.
CREATE INDEX ON measurements USING brin (recorded_at)
WITH (pages_per_range = 32);Size Comparison
A B-tree might be many gigabytes on a billion-row table; the equivalent BRIN can be just a few megabytes. The trade-off is BRIN does coarser filtering.
BRIN on Partitions
On a range-partitioned table, create the BRIN on the parent; it propagates to each partition. Within a partition the data is usually well-correlated, so BRIN works great.
CREATE INDEX ON events USING brin (created_at);Keeping BRIN Fresh
New blocks must be summarized for BRIN to cover them. Autovacuum does this, or you can force it.
SELECT brin_summarize_new_values('measurements_recorded_at_idx');Verifying with EXPLAIN
Look for a Bitmap Index Scan on the BRIN index followed by a recheck. The recheck confirms candidate blocks really contain matches.
EXPLAIN SELECT * FROM measurements
WHERE recorded_at > now() - interval '1 day';When Not to Use BRIN
Avoid BRIN for:
- Columns with random physical order
- Point lookups needing exact, fast access (use B-tree)
- Uniqueness enforcement (BRIN cannot)
Combining Strategies
At scale, a common pattern is: range-partition by time, BRIN on the time column for cheap range scans, and a small B-tree on a frequently filtered foreign key.
Quick Check
When is BRIN a strong choice?
Recap
You learned BRIN indexes: tiny block-range summaries ideal for huge, well-correlated partitioned tables. Tune pages_per_range, keep summaries fresh, verify with EXPLAIN, and combine BRIN with selective B-trees.
Domande Frequenti
La lezione «Indici BRIN per grandi tabelle partizionate» è gratuita?
Sì — il testo completo di «Indici BRIN per grandi tabelle 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 «Indici BRIN per grandi tabelle partizionate»?
Impari quando i Block Range Indexes offrono prestazioni migliori degli alberi B-tree su enormi tabelle partizionate e ordinate naturalmente. 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 «Indici BRIN per grandi tabelle 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
- Scalabilità degli indici con il partizionamento
- Scelta delle strategie per indici e partizioni
- Casi di studio reali
- Indici BRIN per grandi tabelle partizionate