Module Review: Indexing Strategies
Congratulations on completing the Indexing Strategies module! You’ve moved beyond “just add an index” to understanding the internal structures and advanced optimizations that make Postgres unique.
1. Key Takeaways
- B-Tree is King: Use it for 95% of queries (Equality, Range, Sorting). It’s the only index that enforces uniqueness.
- GIN for Composites: Use GIN for JSONB containment (
@>), Arrays (&&), and Full Text Search (@@). It maps keys to lists of TIDs. - GiST for Spatial/Custom: Use GiST for Geometric data (PostGIS), Ranges, and Nearest Neighbor (
<->) searches. - Partial Indexes Save Space: Use a
WHEREclause in your index definition to index only the rows that matter (e.g.,WHERE status = 'active'). - Covering Indexes for Speed: Use
INCLUDEto add payload columns to leaf nodes, enabling Index-Only Scans. - VACUUM Matters: Index-Only Scans rely on the Visibility Map, which is updated by VACUUM.
2. Cheat Sheet: Which Index?
| Data Type | Query Pattern | Recommended Index | Example |
|---|---|---|---|
| Integer / Text | Equality (=) |
B-Tree | CREATE INDEX ON table (col); |
| Integer / Text | Range (<, >) |
B-Tree | CREATE INDEX ON table (col); |
| Text | Prefix (LIKE 'abc%') |
B-Tree | CREATE INDEX ON table (col text_pattern_ops); |
| JSONB | Containment (@>) |
GIN | CREATE INDEX ON table USING GIN (col); |
| JSONB | Extract Key (->>) |
B-Tree (Expression) | CREATE INDEX ON table ((col->>'key')); |
| Array | Overlap (&&) |
GIN | CREATE INDEX ON table USING GIN (col); |
| Geometry | Intersection (&&) |
GiST | CREATE INDEX ON table USING GIST (col); |
| Geometry | Nearest (<->) |
GiST | CREATE INDEX ON table USING GIST (col); |
| Full Text | Match (@@) |
GIN | CREATE INDEX ON table USING GIN (to_tsvector(...)); |
3. Flashcards
Test your understanding of the core concepts.
Question
Loading...
Click to flip
Answer
Loading...
1 / 5
4. Next Steps
Now that you’ve mastered indexing, it’s time to look at how to write queries that actually use them effectively.