Índices BRIN para dados sequenciais grandes
Descubra BRIN (Block Range INdexes), um tipo de índice pequeno, ideal para tabelas enormes cujos dados são naturalmente ordenados, como séries temporais e registros somente de acréscimo.
Índices BRIN para dados sequenciais grandes é uma aula grátis de PostgreSQL Performance & Query Optimization no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de PostgreSQL Performance & Query Optimization, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de PostgreSQL Performance & Query Optimization inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
What is a BRIN Index?
A BRIN (Block Range INdex) stores summary information about ranges of physical table blocks instead of pointing at individual rows. Each entry covers many pages, so the index is extremely small.
How BRIN Differs from B-tree
A B-tree has one entry per row and can be large. A BRIN keeps just the min and max value for each block range.
- B-tree: precise, big, great for random lookups
- BRIN: approximate, tiny, great for range scans on ordered data
When BRIN Shines
BRIN works best when the column's values correlate with physical storage order. Classic cases:
- Time-series tables ordered by inserted timestamp
- Append-only logs
- Large fact tables loaded in key order
Creating a BRIN Index
Use the USING brin clause. Notice how small and fast it is to build compared to a B-tree on the same column.
CREATE INDEX idx_events_ts_brin
ON events USING brin (created_at);Querying Through a BRIN
A range filter lets the planner skip block ranges whose min/max cannot match, reading only the relevant pages.
EXPLAIN ANALYZE
SELECT * FROM events
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';The pages_per_range Option
You can tune how many pages each summary entry covers. Smaller ranges make the index more precise but larger.
CREATE INDEX idx_events_ts_brin
ON events USING brin (created_at)
WITH (pages_per_range = 32);Why Correlation Matters
If the column is not physically ordered, almost every block range will overlap the search value and BRIN will scan the whole table. Check correlation with the stats view.
SELECT attname, correlation
FROM pg_stats
WHERE tablename = 'events';Summarizing New Data
BRIN entries for freshly inserted blocks may be unsummarized. Run this to summarize them so queries can prune those ranges.
SELECT brin_summarize_new_values('idx_events_ts_brin');Size Comparison
On a billion-row table a B-tree might be tens of gigabytes while a BRIN is a few megabytes. Compare them directly.
SELECT pg_size_pretty(pg_relation_size('idx_events_ts_brin'));Trade-offs to Remember
BRIN is not a free win:
- Useless on randomly ordered columns
- Slower for exact single-row lookups than B-tree
- Needs periodic summarization of new blocks
Pick it when the table is huge and naturally ordered.
Maintaining BRIN over Updates
If existing rows are heavily updated, a block range's min/max may widen and lose precision over time. For volatile data, periodically rebuild the index to restore tight ranges.
REINDEX INDEX idx_events_ts_brin;Quick Check
Test your BRIN knowledge.
Recap
You learned BRIN indexes:
- They store min/max summaries per block range — tiny footprint
- Ideal for large, physically ordered data like time-series
- Created with
USING brin; tunepages_per_range - Check
correlationfirst; summarize new blocks - Avoid them on randomly ordered columns
Perguntas Frequentes
A aula “Índices BRIN para dados sequenciais grandes” é grátis?
Sim — o texto completo de “Índices BRIN para dados sequenciais grandes” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de PostgreSQL Performance & Query Optimization, atualize para CoddyKit PRO. O curso de PostgreSQL Performance & Query Optimization inclui 4 aulas no total.
O que vou aprender em “Índices BRIN para dados sequenciais grandes”?
Descubra BRIN (Block Range INdexes), um tipo de índice pequeno, ideal para tabelas enormes cujos dados são naturalmente ordenados, como séries temporais e registros somente de acréscimo. Você pratica PostgreSQL Performance & Query Optimization com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar PostgreSQL Performance & Query Optimization?
Nenhuma experiência prévia é necessária. PostgreSQL Performance & Query Optimization no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Índices BRIN para dados sequenciais grandes”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de PostgreSQL Performance & Query Optimization?
Sim. Cada aula de PostgreSQL Performance & Query Optimization inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Índices Hash, GIN e GiST
- Índices parciais e de expressões
- Índices de cobertura e varreduras somente de índice
- Índices BRIN para dados sequenciais grandes