Does prefixing a statement with EXPLAIN run it, and what does EXPLAIN ANALYZE do differently?
answer
- one form only asks, the other does
- estimates need no execution
- measurements require the work to happen
- think about what that means for a DELETE
- a transaction you never commit
basics
~20 sPlain EXPLAIN does not execute the statement; it returns the execution plan the engine would use instead of any rows. EXPLAIN ANALYZE really runs the statement and reports measured timings, so on an UPDATE or DELETE it changes data.
solid answer
~40 s`EXPLAIN <statement>` asks the engine what it *would* do: you get the plan, not the result rows, and the statement itself is not executed. `EXPLAIN ANALYZE <statement>` is different — it executes the statement for real and annotates the plan with what actually happened. That distinction matters most for writes: `EXPLAIN ANALYZE DELETE FROM sessions WHERE …` genuinely deletes the rows. The standard defence is to run it inside a transaction you abandon: `BEGIN; EXPLAIN ANALYZE DELETE …; ROLLBACK;`. Spelling varies by engine — PostgreSQL and MySQL 8.0 have `EXPLAIN ANALYZE`, Oracle uses `EXPLAIN PLAN FOR` followed by a `DBMS_XPLAN` display call, and SQL Server exposes estimated and actual plans through `SET SHOWPLAN_XML ON` and `SET STATISTICS XML ON` rather than an `EXPLAIN` keyword.
code
sql · 4 linesBEGIN;
EXPLAIN ANALYZE
DELETE FROM sessions WHERE expires_at < CURRENT_TIMESTAMP;
ROLLBACK;go deeper
Remember the two-line difference: plain EXPLAIN returns the intended plan without running the statement, the analyze form runs it and measures. Never point the analyze form at a write statement casually.
Explain what each form can and cannot evidence, show the BEGIN/ROLLBACK pattern for write statements, and know that the syntax differs across engines.
Describe the operational care: the analyze form takes real locks and does real work, so choose where you measure, and treat a plan captured on unrepresentative data as no evidence at all.
Make plan inspection a normal part of how queries get reviewed and shipped, with a safe place to measure at realistic scale, so that performance claims in review are backed by a measured plan rather than an opinion.
## Two different requests There are two things a developer wants from a database when a query is slow, and they are not the same request. 1. **What plan would you choose?** That is plain `EXPLAIN`. The engine parses, plans and hands the plan back. No rows of your query are produced, nothing is written, and the statement's runtime cost is not paid. 2. **What actually happened when you ran it?** That is the analyze form. The engine executes the statement and returns the plan annotated with measurements taken during execution. Because the second form runs the statement, everything the statement does really happens. ## The write-statement trap The classic accident is treating `EXPLAIN` as universally read-only: ```sql EXPLAIN ANALYZE DELETE FROM sessions WHERE expires_at < CURRENT_TIMESTAMP; ``` The rows are gone. Under autocommit they are gone permanently. The safe pattern is to bracket it in a transaction you never commit: ```sql BEGIN; EXPLAIN ANALYZE DELETE FROM sessions WHERE expires_at < CURRENT_TIMESTAMP; ROLLBACK; ``` You still get real measurements, and the data is restored when the transaction unwinds. Two caveats: the work is genuinely performed, so locks are taken and other sessions can be blocked while it runs, and rolling back does not undo everything the world observed — a long-running measurement on a busy production table is still an intrusive act. Measure on a copy when you can. ## Reading what plain EXPLAIN gives you Plain `EXPLAIN` is cheap and safe, which makes it the right first tool while you are still editing the query. It tells you which access method the engine intends, which index it intends to use, and which of your predicates it can push into that access method. It cannot tell you how long anything took, because nothing ran; every number in it is the planner's own estimate. ## Engine spellings The idea is universal; the syntax is not. - PostgreSQL: `EXPLAIN stmt` and `EXPLAIN ANALYZE stmt`, plus parenthesised options. - MySQL: `EXPLAIN stmt`; MySQL 8.0 added `EXPLAIN ANALYZE`, which executes the statement. - Oracle: `EXPLAIN PLAN FOR stmt`, then read the plan table via `DBMS_XPLAN.DISPLAY`. - SQL Server: no `EXPLAIN` keyword — `SET SHOWPLAN_ALL ON` / `SET SHOWPLAN_XML ON` for the estimated plan, `SET STATISTICS PROFILE ON` / `SET STATISTICS XML ON` (or the client's "include actual execution plan" toggle) for the measured one. When you move between engines, check whether the form you are typing executes the statement before you type it against anything that matters. ## A workflow, not a party trick The habit worth forming as a query author is small and repeatable: run plain `EXPLAIN` while shaping the statement, switch to the analyze form on realistic data once the shape looks right, and re-run it after every edit so you know which edit did what. Guessing at a rewrite and shipping it without looking at a plan is how queries acquire changes that never helped anyone. ## What interviewers are checking They want to know you have actually used the tool: that you know the analyze form executes, that you know the transaction trick for writes, and that you do not assume the keyword is spelled the same on every product. Candidates who have only read about plans usually say "EXPLAIN shows the plan" and stop there.
- How do you get measured timings for an UPDATE without keeping the change?Run it inside an explicit transaction and roll back: `BEGIN; EXPLAIN ANALYZE UPDATE …; ROLLBACK;`. The work is really performed, so the numbers are real and the locks are real, but the rows are restored when the transaction unwinds. On a busy production table, prefer measuring on a copy.
- Why can plain EXPLAIN be misleading on its own?Everything it reports is a prediction made before any work happened, so it tells you the intended plan but not what the execution cost. It is the right tool while you are shaping the statement, and it is not evidence that a rewrite made anything faster — for that you need the measured form.
saying these in an interview costs you the question
- Believes any statement prefixed with EXPLAIN is read-only
- Runs EXPLAIN ANALYZE on a production DELETE to 'just check'
- Assumes every engine spells it EXPLAIN ANALYZE
- Thinks plain EXPLAIN reports how long the query took
- Treats a plan from an empty dev table as proof of anything