Índices BRIN para tablas particionadas grandes
Aprenda cuándo los índices de rango de bloques superan a los B-tree en tablas particionadas enormes y ordenadas de forma natural.
Índices BRIN para tablas particionadas grandes 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.
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.
Aprende Advanced PostgreSQL: Indexing, Partitioning, Replication 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
- 11
- Lecciones
- 44
Preguntas frecuentes
¿La lección «Índices BRIN para tablas particionadas grandes» es gratis?
Sí — el texto completo de «Índices BRIN para tablas particionadas grandes» 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 «Índices BRIN para tablas particionadas grandes»?
Aprenda cuándo los índices de rango de bloques superan a los B-tree en tablas particionadas enormes y ordenadas de forma natural. 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 «Índices BRIN para tablas particionadas grandes»?
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
- Escalado de índices con particionamiento
- Selección de estrategias de índices y particionamiento
- Casos prácticos reales
- Índices BRIN para tablas particionadas grandes