# 3.1 The Head Chef's Menu ![The Imperial Ledger](assets/arch_planning_ledger.png) Before the first waiter takes a single step, the Lazy Head Chef must confront a terrifying truth: your query is not a command; it is a **Riddle**. In the database depot, there is almost never just "one way" to find a piece of data. If you are looking for a red suitcase, you could walk every single aisle (a [[Operations/SeqScan|Sequential Scan]]), or you could check the librarian's map (an [[Postgres/Chapter 2/2.1 - The B-Tree (The Librarian's Map)|Index Scan]]). For a complex question involving three people and their luggage, there are literally millions of possible "paths" through the aisles. The Head Chef's job is **The Algebra of Service**. He treats your query like a mathematical formula that can be rearranged and warped. "A query is a riddle!" he shouts, throwing his hat into the air. "And every riddle has a price!" He doesn't make these decisions based on "vibes" or intuition; he makes them based on a rigid **Imperial Ledger of Costs** grounded in the harsh reality of [[Resources/_Resources|Elephant Time]]. ## The Cost of Cold Winters The Head Chef knows that while a thought in his head takes only a microsecond, a single trip to the frozen basement ([[Resources/Disk IO|Disk IO]]) is the equivalent of a **4-month-long winter expedition**. He hates the cold! He assigns points to every action using his trusty **Abacus of Doom**: - **Sequential Page Read**: 1.0 points (Walking down a known path in the sunshine). - **Random Page Read**: 4.0 points (Stumbling through a dark, frozen forest). - **CPU Tuple Processing**: 0.01 points (Opening a suitcase and finding a sock). - **Index Tuple Processing**: 0.005 points (Checking a post-it note with a magnifying glass). ## The Probability Game But the Head Chef is only as good as his menu. If you haven't performed an **[[Operations/_Postgres Operations|ANALYZE]]** recently, his menu will be outdated, and he might send a team of elephants to move a single matchstick while the tea gets cold. ### The Kitchen Census (`ANALYZE`) To keep the ledger accurate, the Head Chef occasionally sends out a census team. You can trigger this manually in your Cafe: ```sql -- Updating the Head Chef's menu ANALYZE animals; -- Sneaking a look at the Head Chef's Histograms SELECT common_vals, most_common_freqs FROM pg_stats WHERE tablename = 'animals' AND attname = 'species_id'; ``` If the Head Chef knows that 90% of your animals are 'Capybaras', he’ll realize that a scan for 'Capybaras' should probably just walk the whole aisle, whereas a scan for the 'One Lone Aardvark' deserves a specialized map-reading. For the deep mathematical specs on these cost constants, see the [[Chapter 5/5.0 - The Hunger of Resources|Resources Chapter]]. --- [[Chapter 3/3.0 - The Great Lunch Rush|← 3.0 - The Great Lunch Rush]] | [[Chapter 3/3.0 - The Great Lunch Rush|↑ 3.0 - The Great Lunch Rush]] | [[Chapter 3/3.2 - Query Algebra|3.2 - Query Algebra →]]