In plain words
A query plan is the step-by-step route the database chooses to answer a query, which explain shows and explain analyze measures, so you can see where the time goes instead of guessing.
Where you see it
Harbor's overdue-invoices report takes two seconds. Running it with explain analyze in the SQL editor shows a tree of steps, each with an estimated and an actual row count, and one step, a sequential scan over every invoice, takes nearly all the time.
What goes wrong
- Running explain analyze on a write
explain analyze really runs the statement, so on a delete or update it deletes or updates. Wrap it in begin and rollback, or use plain explain, which only plans.
explain analyze delete from invoices where status = 'void'; - An index that doesn't match the query
An index on email doesn't help a query on lower(email), and an index on (team_id, created_at) serves filters on team_id far better than filters on created_at alone. Match the index to the query as written, or index the expression.
- Ignoring estimates that are far off
When the plan expects 1,200 rows and gets 310,000, the database chose its route on bad information. Run analyze on the table to refresh its statistics, then read the plan again.
What to tell your agent
Quick check
Sign in to earn +10 XP for itHarbor's sign-in lookup filters on lower(email) and still does a sequential scan, even though users.email has an index. Why?