0Pricing
PostgreSQL Performance & Query Optimization · 课时

面向大型顺序数据的 BRIN 索引

了解 BRIN(Block Range INdexes),这是一种非常小的索引类型,适合数据天然有序的超大型表,例如时间序列和仅追加日志

面向大型顺序数据的 BRIN 索引 是 CoddyKit 上的免费 PostgreSQL Performance & Query Optimization 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 PostgreSQL Performance & Query Optimization 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「面向大型顺序数据的 BRIN 索引」课时是免费的吗?

是的 — 「面向大型顺序数据的 BRIN 索引」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PostgreSQL Performance & Query Optimization 课程的其余内容,请升级到 CoddyKit PRO。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。

「面向大型顺序数据的 BRIN 索引」这节课中我会学到什么?

了解 BRIN(Block Range INdexes),这是一种非常小的索引类型,适合数据天然有序的超大型表,例如时间序列和仅追加日志 你通过在浏览器中直接运行的动手代码来练习 PostgreSQL Performance & Query Optimization,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 PostgreSQL Performance & Query Optimization 需要有经验吗?

无需任何先前经验。CoddyKit 上的 PostgreSQL Performance & Query Optimization 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「面向大型顺序数据的 BRIN 索引」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 PostgreSQL Performance & Query Optimization 课中编写并运行代码吗?

能。每节 PostgreSQL Performance & Query Optimization 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Hash、GIN 与 GiST 索引
  2. 部分索引与表达式索引
  3. 覆盖索引与仅索引扫描
  4. 面向大型顺序数据的 BRIN 索引
← 返回 PostgreSQL Performance & Query Optimization