EXPLAIN ANALYZE
debt(d5/e3/b3/t5)
Closest to 'specialist tool catches' (d5). The term's detection_hints.tools list mysql-slow-query-log, percona-toolkit, and laravel-debugbar — these are specialist profiling/debugging tools that identify slow queries lacking EXPLAIN analysis, not default linters or compiler checks.
Closest to 'simple parameterised fix' (e3). The quick_fix is straightforward: run EXPLAIN FORMAT=JSON or EXPLAIN ANALYZE on the slow query and interpret the output. However, acting on findings (adding indexes, rewriting queries) may touch multiple queries across a component, pushing slightly beyond e1.
Closest to 'localised tax' (b3). EXPLAIN ANALYZE is a diagnostic technique, not a load-bearing architectural choice. It applies to database debugging sessions rather than imposing ongoing structural weight. The applies_to scope (web, cli) is broad, but the concept itself doesn't shape the codebase — it's a point-in-time tool.
Closest to 'notable trap' (t5). The misconception field explicitly states: 'EXPLAIN without ANALYZE shows how a query runs — it shows what the planner *thinks* will happen based on statistics; EXPLAIN ANALYZE shows what actually happens.' This is a documented gotcha that most developers eventually learn, but using EXPLAIN alone and trusting estimated rows is a common mistake that leads to misdiagnosis.
Also Known As
TL;DR
Explanation
EXPLAIN shows the planner's estimated plan. EXPLAIN ANALYZE actually executes the query and shows real row counts, actual time, and buffer usage. Key things to look for: Seq Scan on large tables (needs an index), Nested Loop with high row estimates (join order problem), estimated vs actual rows diverging significantly (stale statistics), and Sort operations that could be eliminated with an index. PostgreSQL's EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) gives the most detail.
Diagram
flowchart TD
QUERY[Slow SQL query] --> EXPLAIN[EXPLAIN ANALYZE]
EXPLAIN --> PLAN[Query Plan]
PLAN --> CHECK{Scan type?}
CHECK -->|Seq Scan - full table| BAD[Missing index<br/>add index on filter column]
CHECK -->|Index Scan - fast| GOOD[Index used correctly]
CHECK -->|Nested Loop huge rows| STAT[Stale statistics<br/>run ANALYZE]
subgraph Key Metrics
ROWS[rows estimate vs actual<br/>big diff means stale stats]
COST[cost= startup..total<br/>higher = slower]
LOOPS[loops=N means<br/>executed N times]
end
style BAD fill:#f85149,color:#fff
style GOOD fill:#238636,color:#fff
Common Misconception
Why It Matters
Common Mistakes
- Using EXPLAIN without ANALYZE — estimated rows can be wildly wrong; always use ANALYZE for diagnosis.
- Not running ANALYZE on tables after bulk inserts — stale statistics cause the planner to choose wrong indexes.
- Optimising based on a small dev dataset — plans change dramatically with production row counts.
- Not using EXPLAIN (BUFFERS) in PostgreSQL — buffer hits vs disk reads reveals I/O bottlenecks.
Code Examples
-- Investigating slow query without EXPLAIN:
SELECT * FROM orders WHERE user_id = 42 AND status = 'pending';
-- 'It's slow' — no idea why
-- Adding random indexes hoping one helps
-- PostgreSQL — full detail:
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT * FROM orders WHERE user_id = 42 AND status = 'pending';
-- Red flags to look for:
-- Seq Scan on orders (rows=1000000) → needs index
-- actual rows=1 vs estimated rows=50000 → stale stats, run ANALYZE
-- Buffers: read=8420 → disk I/O, consider covering index
-- Sort (cost=...) → add index with ORDER BY column