EXPLAIN ANALYZE for actual execution in PostgreSQL - Time & Space Complexity
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?"