PostgreSQL Performance & Query Optimization · Lección

Índices BRIN para datos secuenciales de gran tamaño

Descubra BRIN (Block Range INdexes), un tipo de índice pequeño ideal para tablas enormes cuyos datos están ordenados de forma natural, como las series temporales y los logs de solo inserción.

Lección 4 de 413 pasos

Índices BRIN para datos secuenciales de gran tamaño es una lección gratuita de PostgreSQL Performance & Query Optimization 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 PostgreSQL Performance & Query Optimization, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de PostgreSQL Performance & Query Optimization incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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; tune pages_per_range
  • Check correlation first; summarize new blocks
  • Avoid them on randomly ordered columns
Gratis para empezar

Aprende SQL con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
22
Lecciones
88

Preguntas frecuentes

¿La lección «Índices BRIN para datos secuenciales de gran tamaño» es gratis?

Sí — el texto completo de «Índices BRIN para datos secuenciales de gran tamaño» 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 PostgreSQL Performance & Query Optimization, actualiza a CoddyKit PRO. El curso de PostgreSQL Performance & Query Optimization incluye 4 lecciones en total.

¿Qué aprenderé en «Índices BRIN para datos secuenciales de gran tamaño»?

Descubra BRIN (Block Range INdexes), un tipo de índice pequeño ideal para tablas enormes cuyos datos están ordenados de forma natural, como las series temporales y los logs de solo inserción. Practicas PostgreSQL Performance & Query Optimization 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 PostgreSQL Performance & Query Optimization?

No se requiere experiencia previa. PostgreSQL Performance & Query Optimization 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 «Índices BRIN para datos secuenciales de gran tamaño»?

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 PostgreSQL Performance & Query Optimization?

Sí. Cada lección de PostgreSQL Performance & Query Optimization 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

  1. Índices Hash, GIN y GiST
  2. Índices parciales y de expresiones
  3. Índices de cobertura y escaneos solo de índice
  4. Índices BRIN para datos secuenciales de gran tamaño
← Volver a PostgreSQL Performance & Query Optimization