0
0
MySQLquery~5 mins

NOW, CURDATE, CURTIME in MySQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: NOW, CURDATE, CURTIME
O(n)
Understanding Time Complexity

We want to understand how the time it takes to get the current date and time changes as we ask more times.

How does the cost grow when using NOW, CURDATE, and CURTIME functions repeatedly?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();

-- Using these functions in a query with many rows
SELECT id, NOW() FROM orders;

This code gets the current date and time, date only, or time only. It also shows using NOW() for each row in a table.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Calling NOW(), CURDATE(), or CURTIME() functions.
  • How many times: Once if called alone; once per row if used in a query with many rows.
How Execution Grows With Input

Getting the current time once is very fast and does not depend on input size.

Input Size (n)Approx. Operations
1010 calls if used per row
100100 calls if used per row
10001000 calls if used per row

Pattern observation: The total work grows linearly with the number of rows when used per row.

Final Time Complexity

Time Complexity: O(n)

This means the time to get the current time grows directly with the number of rows you ask for.

Common Mistake

[X] Wrong: "Calling NOW() once in a query with many rows only runs once and is very cheap."

[OK] Correct: In many SQL engines, NOW() is evaluated once per row, so the cost grows with rows, not constant.

Interview Connect

Understanding how functions like NOW() behave with many rows helps you write efficient queries and shows you think about performance.

Self-Check

"What if we used NOW() in a WHERE clause instead of SELECT? How would the time complexity change?"