Index Structures
- B-tree indexes. A B-tree index is a sorted structure over a column: equality and range lookups stop scanning the table.
- Composite indexes. A composite index on (customer_id, created_at) serves the pair: leftmost columns first is the law.
- The write cost. Every index taxes every write: indexes are bought with insert and update speed, so each must earn its place.
Query Execution Plans
- EXPLAIN. EXPLAIN shows the plan: scans, index usage, join strategies and cost estimates: the query’s X-ray.
- Scans versus seeks. A sequential scan reads everything; an index scan jumps to matches: which is right depends on selectivity.
- Join strategies. Engines choose nested loops, hash or merge joins: the plan names which, and row estimates explain why.
Diagnosing Slow Queries
- Measure real timing. EXPLAIN ANALYZE runs the query and reports true times per node: estimates checked against reality.
- Bad estimates. Most slow plans trace to stale statistics: the planner guessed a thousand rows and got ten million.
- The usual fixes. The fix ladder: better index, rewritten predicate, refreshed statistics, then schema change: cheapest first.
Before and after this level
- Before: Level 9: Transactions: ACID, Isolation and Concurrency, Locks, Deadlocks and Transaction Design.
- Next: Level 11: Advanced SQL: Recursive Queries, Advanced Grouping and Set Operations, Complex Relational Patterns.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does SQL Level 10: Indexes & Query Planning cover?
Index Structures, Query Execution Plans, Diagnosing Slow Queries: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 9: Transactions, which covers ACID, Isolation and Concurrency, Locks, Deadlocks and Transaction Design.