Explain Plan
You'll take a query that's slow, read what the planner intends to do, add the one index that changes it, and show the before and after with numbers.
About
Indexes are the part of databases everyone has heard of and almost nobody has measured. This badge is about measuring: a query that takes seconds on a table big enough to matter, the planner's output read line by line, an index chosen on purpose, and a timing that drops by at least ten times. Then you'll do it wrong on purpose, with an index the planner ignores, because knowing why it's ignored is the actual skill. You need a table of at least a million rows, which you can download (taxi trips, flight delays, Wikipedia pageviews) or generate with a recursive CTE. SQLite's EXPLAIN QUERY PLAN and Postgres's EXPLAIN ANALYZE both work. What counts is the plan shown before and after, the timings, and a written explanation a friend could follow.
At a glance
- Per requirement
- 35 XP
- Completion bonus
- 300 XP
- Requirements
- 7
- Takes
- Two or three weeks of evenings
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Build or load a table of at least one million rows and find a query that takes more than a second. A WHERE on an unindexed column, or a JOIN on one, will do it. Use .timer on in sqlite3 or \timing in psql. If nothing is slow, the table is too small, so make it bigger before you go on. Take a screenshot of the row count and the slow timing. Photo
-
2
Run EXPLAIN on the slow query and annotate every line in plain words. 'SCAN trips' in SQLite or 'Seq Scan on trips' in Postgres means it reads every row. Beside each line, write what the database is doing and roughly how many rows it touches, in a text file or on a printout. Take a photo or screenshot of the annotated plan. Photo
-
3
Create one index, rerun the query, and show the plan and the timing change. CREATE INDEX trips_pickup_date ON trips (pickup_date), or whatever your filter column is. The plan should now say SEARCH or Index Scan, and the time should drop by ten times or more. If it didn't, the index is on the wrong column, or the query is doing something to that column the index can't see through. Film the two timings back to back, or screenshot the new plan and timing beside the old. Photo or video
-
4
Write a query that can't use your index, show the plan going back to a scan, and explain why. WHERE lower(email) = ..., WHERE name LIKE '%son', and WHERE date + 1 = ... each hide the column from the index. Then fix one: an index on the expression, or a rewritten predicate that leaves the column alone. Take a screenshot of the scan plan, the fix, and the plan after. Photo
-
5
Build a composite index for a two-column query and show that column order matters. An index on (city, date) helps WHERE city = ... AND date = ..., helps WHERE city = ... alone, and is almost no help for WHERE date = ... alone. Create it, run all three, and show the three plans. Take a screenshot. Photo
-
6
Measure what the indexes cost: file size and insert speed, with and without them. Every index makes writes slower and the file bigger, which is why you don't index everything. Compare the database file size and the time to insert 100,000 rows with your indexes in place and with them dropped. Take a screenshot of both pairs of numbers. Photo
-
7
Write 300 words for a friend on when to add an index and when not to. Post the text with the before and after plans. Say what surprised you, because something will have. Photo