Reading EXPLAIN and EXPLAIN ANALYZE
Read the plan tree, understand cost vs actual time, and pick out the heavy nodes.
Why EXPLAIN?
EXPLAIN shows the planner's strategy for executing a query — without running it. EXPLAIN ANALYZE runs the query AND shows actual timings.
Basic EXPLAIN
Show the estimated plan:
EXPLAIN SELECT * FROM orders WHERE user_id = 42;
-- QUERY PLAN
-- ---------------------------------------------------------------
-- Index Scan using orders_user_id_idx on orders
-- (cost=0.43..8.45 rows=5 width=120)
-- Index Cond: (user_id = 42)All lessons in this course
- Reading EXPLAIN and EXPLAIN ANALYZE
- Sequential Scans vs Index Scans
- Hash Join vs Merge Join vs Nested Loop
- Identifying and Fixing Slow Queries