Chapter 14: Data and failure

Query plan

Also called EXPLAIN

The steps the database chose to answer your query: which table to read first, whether to read all of it or use a shortcut, and how to join. You write what you want, and a part of the database called the planner decides how. EXPLAIN shows you its decision.

An agent asked why a page is slow will guess, usually by rewriting code that isn't the problem. The plan isn't a guess. And it can change by itself as a table grows, so a query that was fine on your laptop's 100 rows can be a different query on production's 10 million.

Say it to your agent

“Don't guess why this is slow. Run EXPLAIN (ANALYZE, BUFFERS) on the query against a copy with production-sized data, and paste the plan. Tell me which node takes the time, and how many rows it read compared with how many it returned.”

The workshop

Name a word, or describe what you mean in everyday language.