![The Mighty Indexes](assets/chap_2_indexes.png) # Chapter 2: The Mighty Indexes (Building Cheat Sheets) Reading a whole book just to find one single name is the absolute opposite of lazy. Imagine you ask Postgres to find the ingredient "Saffron". If you provide no other help besides pointing to a massive `ingredients` [[Structures/Table]] containing ten million rows, the elephant will let out a heavy sigh, start at page one, and read every... single... line. This nightmare scenario is called a **Sequential Scan**, and it is the literal opposite of the Lazy Elephant's philosophy. To avoid this mind-numbing manual labor, the elephant allows you to construct **Indexes**. > [!NOTE] > **Wait, why so many?**: You might wonder why the elephant needs a whole staff of different index types (B-Tree, GIN, GiST, BRIN). It's because different questions require different shortcuts. You wouldn't use a dictionary to find a place on a map, and the elephant wouldn't use a B-Tree to find a word inside a JSON document! An index is essentially a tiny, highly specialized cheat sheet. Instead of rewriting the entire book, the index only stores the *one specific thing* you care about, alongside a direct map right to the page where the rest of the actual data is buried. Because there are many different ways to be lazy, Postgres has developed many different flavors of cheat sheets. The most common is the **[[Chapter 2/2.1 - The Balanced Bookshelf (The B-Tree)|B-Tree]]**. It acts exactly like the alphabetized index at the back of a textbook—perfectly designed for "greater than" or "equal to". But what if you need an exact, rapid match for a unique key? For that, the elephant will use a **[[Operations/Hash|Hash]]** index—a magical dictionary where knowing the word drops you instantly onto the precise page. However, things get strange when you ask the elephant to find something that isn't just a word or a number. How do you build a cheat sheet for the abstract concept of "nearby"? The **[[Structures/Index/GiST|GiST]]** index handles this by acting as a sieve for people drawing overlapping circles and boxes (like geographic mapping or full-text search). And what if a single record contains many distinct ingredients, like a recipe or a JSON document? The **[[Structures/Index/GIN|GIN]]** index acts exactly like a cookbook's ingredient list, mapping a single grain of "Salt" back to every recipe that requires it. "How very curious! A reversed index!" Even among cheat sheets, there is a hierarchy of laziness. The absolute laziest of them all is **[[Chapter 2/2.3 - The Industrial Label (BRIN)|BRIN]]**. It refuses to look at actual records at all. Instead, it simply glances at the outside of a massive filing cabinet and writes a note saying, "Prices between $10 and $50 are probably somewhere in this block." It is incredibly tiny, delightfully vague, and requires almost zero effort to build. And for the truly avant-garde, the elephant has learned to follow his nose. When searching through the abstract, multidimensional 'aromas' of your data—like finding an ingredient that smells "sort of" like Saffron—the **[[Chapter 2/2.4 - The Scent Tracker (Vector Search)|Scent Tracker]]** (Vector Index) uses complex geometric maps (HNSW) to navigate the pantry by proximity rather than by name. "Aha! A most sophisticated snout!" Each index is a trade-off. They require some effort to write today, but the elephant is more than happy to scribble a sticky note now if it guarantees avoiding a 10-mile hike tomorrow. Finally, the **[[Chapter 2/2.5 - The Meticulous Beavers and the Dominoes|Meticulous Beavers and the Dominoes]]** act as the quality control officers, checking every ingredient for freshness (Constraints) and pulling strings to set off chain reactions (Triggers) the moment a suitcase is packed. Hat's off to that! --- | ← Previous | ↑ Table of Contents | Next → | | :--- | :---: | ---: | | [[Chapter 1/1.6 - The Separate Trailer (TOAST)\|1.6 The Separate Trailer (TOAST)]] | [[Learn You a Postgres for Great Good\|Home]] | [[Chapter 2/2.1 - The Balanced Bookshelf (The B-Tree)\|2.1 The Balanced Bookshelf (The B-Tree)]] |