CURRENT_DATE, CURRENT_TIMESTAMP, NOW() in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to get the current date or time changes as the database grows.
Does asking for the current date or time take longer if the database has more data?
Analyze the time complexity of these PostgreSQL functions.
SELECT CURRENT_DATE;
SELECT CURRENT_TIMESTAMP;
SELECT NOW();
These commands return the current date or timestamp from the database server.
These commands do not repeat any operations like loops or scans.
- Primary operation: Fetching the system clock value once.
- How many times: Exactly once per query execution.
Getting the current date or time does not depend on how much data is in the database.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same no matter the input size.
Time Complexity: O(1)
This means the time to get the current date or time stays constant, no matter how big the database is.
[X] Wrong: "Getting the current date or time takes longer if the database has more rows."
[OK] Correct: These functions just read the system clock once and do not scan or process any data in the database.
Knowing that some queries run in constant time helps you understand which operations are fast and predictable, a useful skill in database work.
"What if we used CURRENT_TIMESTAMP inside a query that processes thousands of rows? How would the time complexity change?"