# 3.2.1 The Food Runners (Scans)

Before any assembling or organizing can happen, someone has to actually go out and **fetch** the raw ingredients from the depths of the cafe. In the Query Algebra, this is the job of the **Food Runners**, officially known as **Scan Nodes**.
The Runners are the only staff in the entire kitchen who actually touch the heavy metal shelves of the **[[Chapter 6/6.2 - The Elevator Queue (Disk Wait)|Frozen Pantry]]** (Disk) or the **[[Chapter 5/5.1 - The Warming Rack (Shared Buffers)|Cold Storage]]** (Shared Buffers). Their goal is to find the right **[[Chapter 1/1.3 - The Shipping Container (The Page)|Pages]]** and bring them into the heat of the kitchen.
## Sequential Scan: Walking the Frozen Pantry
The **[[Operations/SeqScan|Sequential Scan]]** is the most honest, yet most exhausting way to fetch ingredients. Imagine a tired elephant walking down every single aisle of the Frozen Pantry, opening every single physical container, and checking every single plate inside.
If the Head Chef asks, "Find me all plates containing blue cheese," the Sequential Fetcher will check *all ten million plates* just to be absolutely sure none were missed. It is the path of maximum resistance and causes the most **[[Chapter 6/6.2 - The Elevator Queue (Disk Wait)|I/O Sweat]]**.
## Index Scan: The Express Elevator
The **[[Operations/IndexScan|Index Scan]]** avoids the long walk entirely. These specialized fetchers use the **[[Chapter 2/2.1 - The Balanced Bookshelf (The B-Tree)|Index Clerk's Map]]** to pinpoint the exact coordinates of a required plate. They use an express elevator to go directly to a specific shelf, grab the one plate, and vanish.
## Bitmap Heap Scan: The Kitchen Whiteboard
What if the Head Chef needs five thousand scattered plates? Walking back and forth between the Index Clerk and the Pantry five thousand times would be foolish.
Enter the **[[Operations/BitmapHeapScan|Bitmap Heap Scan]]**. This is the sweet spot for when you need *more* than 1 row but *less* than the whole table.
1. **The Clipboard (Bitmap Index Scan)**: First, they scan the Index and mark every required shelf location on a giant white kitchen board (the Bitmap).
2. **The Route (Bitmap Heap Scan)**: Then, a single Fetcher organizes a route to visit each shelf *exactly once*, collecting all necessary plates from a container before moving to the next.
**The Contrast**:
- A **Sequential Scan** is walking every aisle in order, checking every box.
- A **Bitmap Heap Scan** is reading the map first, marking only the "interesting" aisles on your clipboard, and then visiting only those marked aisles in one efficient loop.
### The High-Stakes Dinner Rush
To see the difference in cost, we can trick the Head Chef into thinking our `ingredients` table has 10 million rows.
**The Sequential Fetch (Walking the Entire Room):**
```sql
-- Searching for all 'Herb' ingredients in a massive pile
EXPLAIN SELECT * FROM ingredients WHERE category = 'Herb';
-- Result (The Exhausting Walk):
-- Seq Scan on ingredients (cost=0.00..625000.00 rows=10000000 width=44)
> [!TIP]
> **Elephant's Footnote: The Sequential Sweat**
> - **`cost=0.00..625000.00`**: A massive estimate! The elephant assumes he'll have to walk the entire 10 miles.
> - **`rows=10000000`**: The Head Chef's census count of the ingredients.
```
**The Index Fetch (Using the Clerk's Map):**
```sql
-- Finding a single, specific ingredient by its ID
EXPLAIN SELECT * FROM ingredients WHERE id = 12;
-- Result (The Express Elevator):
-- Index Scan using ingredients_pkey on ingredients (cost=0.14..8.16 rows=1 width=31)
> [!TIP]
> **Elephant's Footnote: The Rapid Grab**
> - **`cost=0.14..8.16`**: Barely any sweat at all. The elephant is happy!
```
**The Index-Only Fetch (The Ultimate Lazy Victory):**
If the index map already contains everything the Head Chef asked for, the Fetcher doesn't even have to walk into the Pantry! He just reads the map and reports back.
```sql
-- We only need the ID, which is already on the map!
EXPLAIN SELECT id FROM ingredients WHERE id < 100;
-- Result:
-- Index Only Scan using ingredients_pkey on ingredients (cost=0.43..12.30 rows=99 width=8)
```
Notice the **`cost`**! The Head Chef estimated the Sequential Fetch would cost **625,000** points of "sweat," while the Index Fetch only costs **8.45**. He will choose the Index every single time. Itβs a victory for pure laziness!
---
| β Previous | β Table of Contents | Next β |
| :--- | :---: | ---: |
| [[Chapter 3/3.2 - The Assembly Line (Query Algebra)\|3.2 The Assembly Line (Query Algebra)]] | [[Learn You a Postgres for Great Good\|Home]] | [[Chapter 3/3.2.2 - The Matchmakers (Joins)\|3.2.2 The Matchmakers (Joins)]] |