Final challenge: speed up the school
After this: Read plans, pick the index that helps, and say why some searches still scan.
This challenge has five steps, and each step is scored on its own. You will pick the index that turns a scan of the school's enrolments into a search, read the plan it gives, say why a login search still scans, choose the column order of a two-column index, and say what an index costs on every new row. You can try as many times as you like, and hints work as they do in the lessons.
Before you start, try it yourself. The block below asks for the plan of "students in course 1". Run it and read the plan. Then add a CREATE INDEX line above it, run it again, and see whether the plan changes.
The school's page for a course lists its students. Its query gives the plan SCAN enrolments. Which one new index turns that SCAN into a SEARCH?
EXPLAIN QUERY PLANSELECT student_id, mark FROM enrolmentsWHERE course_id = 1;Now the index exists. What does the detail column of the plan say?
CREATE INDEX idx_enrolments_course ON enrolments(course_id);EXPLAIN QUERY PLANSELECT student_id, mark FROM enrolmentsWHERE course_id = 1;The members table has idx_members_email. The login page searches with WHERE lower(email) = 'user4242@mail.test', and the plan still says SCAN members. Why?
The club's busiest page searches WHERE city = ? AND joined = ?, and another page searches WHERE city = ? alone. You may create one index. Which one serves both pages best?
The members table now has idx_members_email and idx_members_city_joined. A new member signs up. What does that one INSERT have to do?
When the plan says SEARCH, delete the EXPLAIN QUERY PLAN line to see the answer. It should be these four rows:
- Pick the index that turns a scan into a search, read its plan, explain why lower(email) still scans, choose a two-column index, and say what one new row costs.