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?"
Practice
interval type in PostgreSQL represent?Solution
Step 1: Understand the interval type purpose
The interval type is designed to store durations, not fixed points in time.Step 2: Compare with other types
Unlike timestamp or date types, interval holds a length of time like '3 days' or '2 hours'.Final Answer:
A duration of time like hours, days, or months -> Option CQuick Check:
Interval = Duration [OK]
- Confusing interval with timestamp
- Thinking interval stores exact dates
- Assuming interval is a string
Solution
Step 1: Recall interval literal syntax
PostgreSQL requires the interval value to be a string inside single quotes after the INTERVAL keyword.Step 2: Check each option
OnlyINTERVAL '5 days'matches the correct syntax; others are invalid or reversed.Final Answer:
INTERVAL '5 days' -> Option BQuick Check:
Interval syntax = INTERVAL 'value unit' [OK]
- Omitting quotes around the duration
- Placing INTERVAL after the string
- Swapping number and unit order
SELECT CURRENT_DATE + INTERVAL '3 days';
Solution
Step 1: Understand adding interval to date
Adding an interval to a date shifts the date forward by that duration.Step 2: Apply the interval '3 days'
Adding '3 days' to CURRENT_DATE results in a date 3 days later than today.Final Answer:
The date 3 days after today -> Option DQuick Check:
Date + Interval = Date shifted forward [OK]
- Thinking it subtracts days
- Expecting a text output
- Believing it causes an error
SELECT CURRENT_TIMESTAMP + '2 hours';
Solution
Step 1: Check interval usage with timestamp
To add a duration, the value must be an interval type, declared with INTERVAL keyword.Step 2: Analyze the query
The string '2 hours' lacks INTERVAL keyword, so PostgreSQL will error.Final Answer:
Missing INTERVAL keyword before '2 hours' -> Option AQuick Check:
Interval keyword required for durations [OK]
- Omitting INTERVAL keyword
- Using wrong quotes
- Assuming timestamp can't add intervals
Solution
Step 1: Understand interval addition
Adding intervals separately ensures correct handling of months and days.Step 2: Evaluate each option
SELECT DATE '2024-01-10' + INTERVAL '1 month + 15 days'; tries to combine units in one string, which is invalid syntax. SELECT DATE '2024-01-10' + INTERVAL '45 days'; adds 45 days, which is not exactly 1 month and 15 days. SELECT DATE '2024-01-10' + INTERVAL '1.5 months'; uses fractional months, which PostgreSQL does not support directly.Final Answer:
SELECT DATE '2024-01-10' + INTERVAL '1 month' + INTERVAL '15 days'; -> Option AQuick Check:
Separate intervals for months and days [OK]
- Combining units in one interval string
- Using fractional months
- Adding days as total instead of separate
Ready to prove it?
Pass the test and this pattern moves unknown
