Add lesson 8: Indexes and why queries get slow
3 changed files with 33 additions and 0 deletions
lessons/08-indexes-and-why-queries-get-slow/README.md ADDED
| @@ -0,0 +1,21 @@ | ||
| 1 | +# Lesson 8: Indexes and why queries get slow | |
| 2 | + | |
| 3 | +Indexes and why queries get slow comes up in almost every real SQL project. We start with the why, then write code together. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/sql-essentials/lessons/8](https://glibcode.net/courses/sql-essentials/lessons/8) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `indexes.sql`: EXPLAIN QUERY PLAN before and after an index | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +sqlite3 shop.db < ../../setup/schema.sql | |
| 15 | +sqlite3 shop.db < ../../setup/seed.sql | |
| 16 | +sqlite3 shop.db < indexes.sql | |
| 17 | +``` | |
| 18 | + | |
| 19 | +## Exercises | |
| 20 | + | |
| 21 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/08-indexes-and-why-queries-get-slow/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 8 | |
| 2 | + | |
| 3 | +1. Add a check that prints a warning when a result looks wrong (negative, empty, too big). | |
| 4 | +2. Make the example ask for its input instead of using a fixed value. | |
| 5 | +3. Break the example on purpose: delete a bracket or misspell a name. Read the error message. Which line does it point to? | |
lessons/08-indexes-and-why-queries-get-slow/indexes.sql ADDED
| @@ -0,0 +1,7 @@ | ||
| 1 | +EXPLAIN QUERY PLAN | |
| 2 | +SELECT * FROM orders WHERE customer_id = 42; -- SCAN orders | |
| 3 | + | |
| 4 | +CREATE INDEX idx_orders_customer ON orders (customer_id); | |
| 5 | + | |
| 6 | +EXPLAIN QUERY PLAN | |
| 7 | +SELECT * FROM orders WHERE customer_id = 42; -- SEARCH orders USING INDEX | |