# 3.2.1 The Food Runners (Scans) ![The Food Runners](assets/arch_plan_runners.png) Before any assembling or organizing can happen, someone has to actually go out and retrieve the plates from the depot. 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 depot. ## Sequential Scan: The Exhausted Waiter The **[[Operations/SeqScan|Sequential Scan]]** is the most honest, yet most exhausting way to find a plate. Imagine a tired elephant walking down every single aisle of the depot, opening every single physical container, and checking every single plate inside. If the Maitre D' asks, "Find me all plates containing blue cheese," the Sequential Scan will check *all ten million plates* just to be absolutely sure none were missed. It is the path of maximum resistance. ## Index Scan: The Runners on Roller Blades The **[[Operations/IndexScan|Index Scan]]** avoids the long walk entirely. These specialized runners use the **[[Chapter 2/2.1 - The B-Tree|Index Clerk's Map]]** to pinpoint the exact coordinates of a required plate. They walk directly to a specific container, grab the one plate, and vanish. "Snipers," the elephant nods respectfully (though he really means "efficient"). ## Bitmap Heap Scan: The Kitchen Whiteboard What if the Maitre D' needs five thousand scattered plates? Walking back and forth between the Index Clerk and the depot five thousand times would cause absolute chaos. Enter the **[[Operations/BitmapHeapScan|Bitmap Heap Scan]]**. First, they scan the Index and mark every required container location on a giant white kitchen board (the Bitmap Index Scan). Then, they organize a route to visit each container *exactly once*, collecting all necessary plates from a container before moving to the next. Pure, lazy efficiency! ### The High-Stakes Dinner Rush To see the difference in cost, we can trick the Head Chef into thinking our `orders` table has 10 million rows (see the **[[scripts/simulate_scale.sql|Simulation Script]]**). **The Sequential Scan (Walking the Entire Room):** ```sql -- Searching for all 'Pending' orders in a massive pile EXPLAIN SELECT * FROM orders WHERE status = 'Pending'; -- Result: -- Seq Scan on orders (cost=0.00..625000.00 rows=10000000 width=44) ``` **The Index Scan (Using the Clerk's Map):** ```sql -- Finding a single, specific order by its ID EXPLAIN SELECT * FROM orders WHERE id = 123456; -- Result: -- Index Scan using orders_pkey on orders (cost=0.43..8.45 rows=1 width=44) ``` **The Index Only Scan (The Ultimate Lazy Victory):** If the index already contains everything the Head Chef asked for, the Runner doesn't even have to walk to the depot! He just reads the map and reports back. ```sql -- We only need the ID, which is already on the map! EXPLAIN SELECT id FROM orders WHERE id < 100; -- Result: -- Index Only Scan using orders_pkey on orders (cost=0.43..12.30 rows=99 width=8) ``` Notice the **`cost`**! The Head Chef estimated the Sequential Scan would cost **625,000** points of "sweat," while the Index Scan only costs **8.45**. He will choose the Index every single time. It’s a victory for laziness! --- [[Chapter 3/3.2 - Query Algebra|← 3.2 - Query Algebra]] | [[Chapter 3/3.2 - Query Algebra|↑ 3.2 - Query Algebra]] | [[Chapter 3/3.2.2 - The Matchmakers (Joins)|3.2.2 - The Matchmakers (Joins) β†’]]