SteelReed

Search

Search posts, questions…

SQL & Query TuningQuestion 28 / 37

Junior

How do EXPLAIN and EXPLAIN ANALYZE differ, and how do you read an execution plan?

Short answer

EXPLAIN shows the plan the planner intends to run, with estimated cost and row counts, without executing it. EXPLAIN ANALYZE actually runs the statement and adds the actual time and row count of every node.

EXPLAINEXPLAIN ANALYZESQL statementPlanner picks a planfrom statisticsPlan + estimatesnot executedReally runsactual time, rowsCompare estimatedvs actual rows

3 points to say:

  1. EXPLAIN = estimates; EXPLAIN ANALYZE = real execution, so wrap UPDATE/DELETE in a transaction and ROLLBACK.
  2. Multiply actual time by loops; estimated rows far from actual usually means bad statistics.
  3. Add BUFFERS to see whether data came from cache (hit) or disk.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.