Track · 2:30 · Liner note
The Planner Takes the Scenic Route
A query planner chooses how a query is executed, including which indexes to use, the order of joins and the algorithm for each step, and EXPLAIN shows the route it picked.
Before a query runs, a planner decides how: which indexes to use, which table to read first, which join algorithm to apply. Usually it picks well. The Planner Takes the Scenic Route is for the days it does not, when a statistic is stale or a filter hides something and the chosen road is long, pretty and slow.
Ask for the plan with EXPLAIN before guessing. Look for full scans where you expected a skip, row estimates far from reality and join orders that start from the biggest table. Fix the statistics or the query, and recheck.