Explain Plan
ELI5 — The Vibe Check
EXPLAIN shows you exactly how the database plans to execute your query — which indexes it uses, how many rows it scans, where it is slow. It is like X-ray vision for your SQL. Run it on any slow query to see what is actually happening.
Real Talk
EXPLAIN (or EXPLAIN ANALYZE in PostgreSQL) shows the execution plan the query planner generates for a SQL statement. It reveals operations like sequential scans, index scans, hash joins, and sort operations, along with estimated (and with ANALYZE, actual) row counts and costs. Essential for identifying and fixing slow queries.
Show Me The Code
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE user_id = 42
ORDER BY created_at DESC;
When You'll Hear This
"Run EXPLAIN ANALYZE on the slow query to see if it's doing a sequential scan." / "The Explain Plan showed a missing index was causing a full table scan."
Related Terms
Index
A database index is like the index in the back of a book. Without it, the database reads every single row to find what you want.
N+1 Query
N+1 is when your code runs 1 query to get a list of things, then runs 1 more query for EACH thing on the list.
Query Optimization
Query optimization is the art of making slow database queries fast. Add an index here, rewrite that subquery as a JOIN, fetch only the columns you need.