Interval type for durations in PostgreSQL - Time & Space Complexity
We want to understand how the time to process interval durations grows as the amount of data increases.
How does the database handle many interval values when calculating or filtering durations?
Analyze the time complexity of the following query using interval durations.
SELECT employee_id, SUM(work_duration) AS total_duration
FROM work_logs
GROUP BY employee_id
HAVING SUM(work_duration) > INTERVAL '40 hours';
This query sums up work durations stored as intervals for each employee and filters those with total durations over 40 hours.
Look for repeated actions in the query.
- Primary operation: Summing interval values for each employee.
- How many times: Once per row in the work_logs table, grouped by employee.
As the number of work log entries grows, the database sums more intervals per employee.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 interval sums |
| 100 | About 100 interval sums |
| 1000 | About 1000 interval sums |
Pattern observation: The work grows roughly in direct proportion to the number of rows processed.
Time Complexity: O(n)
This means the time to compute grows linearly as the number of interval entries increases.
[X] Wrong: "Summing intervals is a constant time operation regardless of data size."
[OK] Correct: Each interval must be added for every row, so more rows mean more additions, increasing time.
Understanding how interval calculations scale helps you explain performance when working with time durations in databases.
"What if we indexed the employee_id column? How would that affect the time complexity of grouping and summing intervals?"