How do you use an EXPLAIN plan to diagnose a slow-running query?
Last updated
Run
EXPLAIN ANALYZEfor the actual plan with real rows and timings, not just estimates, then find the costliest nodes.Look for full scans on large tables (missing index or non-SARGable predicate), bad joins, costly sorts or disk spills, and big estimated-vs-actual row mismatches (stale statistics).
Fix by adding indexes, rewriting predicates, or updating statistics, then re-run to confirm the plan improved.
- https://www.postgresql.org/docs/7.3/performance-tips.html
- https://www.postgresql.org/docs/current/using-explain.html
- https://garvitgupta58.medium.com/learnings-from-a-slow-query-analysis-in-postgresql-d2316def97d7
- https://dev.to/philip_mcclarence_2ef9475/reading-postgresql-explain-and-explain-analyze-output-3o74
- https://pganalyze.com/solutions/slow-postgres-queries
- https://medium.com/@rohansodha10/explain-analyze-demystified-reading-query-plans-like-a-pro-d28ccf82edff
- https://medium.com/@harsh1995hg/unmasking-slow-queries-how-to-read-a-sql-execution-plan-and-fix-performance-bottlenecks-%EF%B8%8F-103f732c58ea
- https://kiransabne.dev/how-to-read-a-postgresql-execution-plan-the-complete-guide-with-real-examples