SQL Execution Plans: Reading What the Database Actually Does
The execution plan reveals what the database does with your query. Learning to read it is the single most valuable optimization skill. Here is how I read plans. The execution plan is the database telling you exactly how it will run your query. Which tables it scans, which indexes it uses, what join strategies it picks, and how much each step costs. Every optimization I do starts with reading the plan. A query that is slow has a plan that shows where the time goes, and the fix usually becomes obvious once you see it. Here is how I read execution plans. Generating a Plan Every database has a way to show the plan. PostgreSQL and MySQL use EXPLAIN to show the plan and EXPLAIN ANALYZE to run the query and show actual timings. I always use ANALYZE because estimated plans can be wrong when statistics are stale. EXPLAIN ANALYZE SELECT c.name, COUNT(o.id) AS order_count FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE c.created_at >= '2026-01-01' GROUP BY c.name ORDER BY order_count DESC LIMIT 10; The output is a tree of operations. The database executes from the leaves to the root. Each node shows…