What Is EXPLAIN ANALYZE and How to Read It?
What is EXPLAIN ANALYZE? EXPLAIN ANALYZE is the most reliable performance analysis tool that shows how a SQL query is planned by the database and how it actually executes. When a…
In this article↗
What is EXPLAIN ANALYZE?
EXPLAIN ANALYZE is the most reliable performance analysis tool that shows how a SQL query is planned by the database and how it actually executes.
When a query runs, the database follows these two steps:
Planning The database optimizer generates multiple possible paths to execute the query and selects the execution plan it believes has the lowest cost.
Execution The selected plan is applied to the actual data.
EXPLAIN only shows the first step.
EXPLAIN ANALYZE allows us to see both steps together.
This distinction is critical for understanding production performance problems.
A Simple Execution Plan Example
Let's assume we have an orders table with millions of records:
CREATE INDEX idx_orders_user_created_at
ON orders(user_id, created_at);EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE user_id = 42
AND created_at >= '2026-01-24'
AND created_at < '2026-01-25'; A sample EXPLAIN ANALYZE output for this query might look like this:
Index Scan using idx_orders_user_created_at on orders
(cost=0.43..245.67 rows=120 width=512)
(actual time=0.112..3.981 rows=118 loops=1)Now let's examine this output line by line.
How to Read an Execution Plan
1. Scan Type
Index Scan using idx_orders_user_created_at on ordersThis line tells us:
- Instead of scanning the entire table (
Seq Scan) - The database accessed it using the specified index
This is generally a good sign.
2. Cost Values (Planner Estimate)
This section is the planner's estimate, not the actual value.
0.43 → estimated cost to start the query
245.67 → estimated total cost to complete the query
rows=120 → number of rows the planner estimates will be returned
width=512 → estimated byte size per row
3. Actual Values (Real World)
(actual time=0.112..3.981 rows=118 loops=1) This section is the true value of EXPLAIN ANALYZE.
0.112 → time to return the first row
3.981 → time to complete the entire query
rows=118 → actual number of rows returned
loops=1 → how many times this plan was executed
Here's what we look at:
Estimated
rows=120Actualrows=118
This is a fairly accurate estimate.
The Gap Between Estimate and Reality
Now let's consider a problematic example:
Seq Scan on orders
(cost=0.00..18500.00 rows=500 width=512)
(actual time=0.020..820.432 rows=45000 loops=1)There's a serious problem here:
- The planner expected
500rows - Actually
45,000rows were read - As a result,
Seq Scanwas chosen - The query took over
800ms
This typically indicates:
- Incorrect or missing statistics
- Low selectivity
- Poor index design
- Functions used on columns
At this point, the solution:
- Is not "let's add an index"
- Is the question: why did the planner make a wrong estimate?
Why is EXPLAIN ANALYZE Essential in Production?
Because EXPLAIN ANALYZE shows you:
- Why the query is slow
- Which step is actually expensive
- Why the database chose that path
based on evidence.
Adding an index is an assumption.
EXPLAIN ANALYZE is the truth.
Conclusion
EXPLAIN ANALYZE is not a tool for fixing queries.
It is a tool for understanding why a query behaves the way it does.
In production systems, performance problems rarely come from missing indexes alone. They come from:
- Wrong assumptions
- Incorrect estimates
- And blind optimizations
Reading execution plans correctly allows you to:
- Trust data instead of intuition
- Fix the real bottleneck, not the visible one
- Make confident decisions in production environments
If you can read EXPLAIN ANALYZE,
you are no longer guessing — you are debugging with evidence.
