EXPLAIN ANALYZE for actual execution in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we run a query, we want to know how long it takes as data grows.
EXPLAIN ANALYZE helps us see the real work done by the database.
Analyze the time complexity of the following query with EXPLAIN ANALYZE.
EXPLAIN ANALYZE
SELECT * FROM orders WHERE customer_id = 123;
This shows how PostgreSQL runs the query and how long each step takes.
Look for repeated work inside the query execution.
- Primary operation: Scanning rows in the orders table.
- How many times: Once for each row checked against the condition.
As the number of rows grows, the time to scan grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 row checks |
| 100 | 100 row checks |
| 1000 | 1000 row checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time grows in a straight line as the data grows.
[X] Wrong: "EXPLAIN ANALYZE only shows estimated time, not real time."
[OK] Correct: EXPLAIN ANALYZE actually runs the query and shows real execution time, not just estimates.
Understanding how EXPLAIN ANALYZE shows real query time helps you explain performance clearly in real projects.
"What if we added an index on customer_id? How would the time complexity change?"
Practice
EXPLAIN ANALYZE in PostgreSQL?Solution
Step 1: Understand what EXPLAIN ANALYZE does
It runs the SQL query and collects detailed information about how the database executes it, including timing and row counts.Step 2: Compare with other options
Options A, C, and D describe different actions unrelated to EXPLAIN ANALYZE's purpose.Final Answer:
To run the query and show the actual execution plan with timing details -> Option BQuick Check:
EXPLAIN ANALYZE = actual execution plan with timing [OK]
- Thinking EXPLAIN ANALYZE only checks syntax
- Confusing it with backup or delete commands
- Assuming it does not run the query
SELECT * FROM users; using EXPLAIN ANALYZE?Solution
Step 1: Recall the correct EXPLAIN ANALYZE syntax
The correct syntax is to write EXPLAIN ANALYZE followed by the query to run and analyze.Step 2: Check each option
EXPLAIN SELECT * FROM users; misses ANALYZE, so it only shows estimated plan. ANALYZE EXPLAIN SELECT * FROM users; reverses keywords incorrectly. EXPLAIN RUN SELECT * FROM users; uses an invalid keyword RUN.Final Answer:
EXPLAIN ANALYZE SELECT * FROM users; -> Option CQuick Check:
EXPLAIN ANALYZE + query = correct syntax [OK]
- Omitting ANALYZE keyword
- Swapping EXPLAIN and ANALYZE order
- Using invalid keywords like RUN
EXPLAIN ANALYZE SELECT * FROM orders WHERE order_id = 10;, which part of the output tells you how many rows were actually returned?Solution
Step 1: Understand EXPLAIN ANALYZE output fields
"Actual Rows" shows the real number of rows returned by each step during execution.Step 2: Differentiate from other fields
"Estimated Rows" is the planner's guess before running. "Planning Time" is time spent planning. "Buffers" shows disk usage, not row count.Final Answer:
The "Actual Rows" value in the output -> Option DQuick Check:
Actual Rows = real returned rows [OK]
- Confusing estimated rows with actual rows
- Looking at planning time for row count
- Ignoring actual execution details
ANALYZE EXPLAIN SELECT * FROM products WHERE price > 100; but get an error: ERROR: syntax error at or near "EXPLAIN". What is the likely cause?Solution
Step 1: Analyze the error message
The error points to a syntax problem near "EXPLAIN" which suggests wrong keyword order.Step 2: Check correct keyword order
The correct order is EXPLAIN ANALYZE, not ANALYZE EXPLAIN. Writing ANALYZE first causes syntax error.Final Answer:
You wrote ANALYZE before EXPLAIN -> Option AQuick Check:
EXPLAIN must come before ANALYZE [OK]
- Swapping EXPLAIN and ANALYZE keywords
- Missing semicolon (usually different error)
- Running query without EXPLAIN ANALYZE
customers and orders. Using EXPLAIN ANALYZE, you see a sequential scan on orders despite an index on customer_id. What should you check or do next?Solution
Step 1: Understand why index might be ignored
If statistics are outdated, the planner may wrongly choose a sequential scan instead of using the index.Step 2: Use ANALYZE to update statistics
RunningANALYZE orders;updates table statistics so planner can make better decisions.Final Answer:
Check if statistics are outdated and run ANALYZE orders; -> Option AQuick Check:
Update stats with ANALYZE to help planner use indexes [OK]
- Dropping useful indexes
- Ignoring statistics update
- Trying to avoid JOINs instead of fixing plan
