# 2.3 The Industrial Label (BRIN) ![The BRIN Post-it Notes](assets/arch_index_brin.png) When the depot becomes truly infinite (petabytes of data!), even the Index Clerk gets overwhelmed. He can't keep a map of every suitcase; his tiny hands would fall off! For these cases, we use a **BRIN** (Block Range Index). It is the ultimate expression of the Lazy Elephant’s philosophy. ## The Binoculars Approach Why look closely when you can look from afar? The BRIN index acts as a pair of **Magical Binoculars**. BRIN is the ultimate lazy index—it doesn't tell you where something IS, it only tells you where it definitely **ISN'T**. It’s a map of everywhere you shouldn't go! > [!NOTE] > **The Block Range**: By default, the elephant only sticks a post-it note on every **128 Shipping Containers** (Pages). This means the index is incredibly tiny—thousands of times smaller than a B-Tree—because it only needs one note for every 1MB of data! ### The Scrambled Depot (The BRIN Gotcha) BRIN only works when **nearby physical boxes contain nearby logical values**. This is the "Shape of the Idea." If your `delivery_time` is added sequentially (like a diary), it's perfect! But imagine a **Scrambled Depot** where you toss ingredients in randomly. If every 128-page block contains prices ranging from $1 to $1,000, the BRIN "Binoculars" will just see a blurry mess. Every post-it note will say "Min: $1, Max: $1,000," and the elephant will be forced to walk every single aisle anyway! ### The Binocular Boost Let's look at a range query on our massive `supply_deliveries` table: #### State 1: The Infinite Aisle (Before BRIN) ```sql EXPLAIN SELECT count(*) FROM supply_deliveries WHERE delivery_time BETWEEN '2024-01-01' AND '2024-01-31'; -- Results (The Foggy Walk): -- Seq Scan on supply_deliveries (cost=0.00..1845.00 rows=2880 width=0) > [!TIP] > **Elephant's Footnote: The Foggy Scan** > - **`cost=0.00..1845.00`**: The total estimated effort. > - **`rows=2880`**: The bird's guess for how many matches exist. ``` #### State 2: The Quick Glance (After BRIN) ```sql -- Sticking the Post-it Notes on the blocks CREATE INDEX idx_deliveries_brin ON supply_deliveries USING brin(delivery_time); EXPLAIN SELECT count(*) FROM supply_deliveries WHERE delivery_time BETWEEN '2024-01-01' AND '2024-01-31'; -- Results (The Clear View): -- Bitmap Heap Scan on supply_deliveries (cost=12.00..54.00 rows=2880 width=0) -- -> Bitmap Index Scan on idx_deliveries_brin (cost=0.00..12.00 rows=2880 width=0) > [!TIP] > **Elephant's Footnote: The Binocular Boost** > - **`cost=12.00..54.00`**: Notice how much the "Start" cost dropped! > - The elephant just glanced at the notes and skipped 90% of the depot! ``` The elephant just glanced at the notes and skipped 90% of the depot! For the technical manual on BRIN ranges, see the **[[Structures/Index/BRIN|BRIN Reference]]**. --- | ← Previous | ↑ Table of Contents | Next → | | :--- | :---: | ---: | | [[Chapter 2/2.2 - The Word Scavenger (GIN & GiST)\|2.2 The Word Scavenger (GIN & GiST)]] | [[Learn You a Postgres for Great Good\|Home]] | [[Chapter 2/2.4 - The Scent Tracker (Vector Search)\|2.4 The Scent Tracker (Vector Search)]] |