NOW, CURDATE, CURTIME in MySQL - Time & Space 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?
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 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.
Getting the current time once is very fast and does not depend on input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls if used per row |
| 100 | 100 calls if used per row |
| 1000 | 1000 calls if used per row |
Pattern observation: The total work grows linearly with the number of rows when used per row.
Time Complexity: O(n)
This means the time to get the current time grows directly with the number of rows you ask for.
[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.
Understanding how functions like NOW() behave with many rows helps you write efficient queries and shows you think about performance.
"What if we used NOW() in a WHERE clause instead of SELECT? How would the time complexity change?"
Practice
NOW() return?Solution
Step 1: Understand the function purpose
NOW()is designed to return both date and time together.Step 2: Compare with other functions
CURDATE()returns only date,CURTIME()returns only time, soNOW()returns both.Final Answer:
The current date and time -> Option BQuick Check:
NOW() = current date + time [OK]
- Confusing NOW() with CURDATE()
- Thinking NOW() returns only time
- Assuming NOW() returns only year
Solution
Step 1: Identify function for current date
CURDATE()returns only the current date in MySQL.Step 2: Check other options
NOW()returns date and time,CURRENT_TIME()andCURTIME()return only time.Final Answer:
SELECT CURDATE(); -> Option AQuick Check:
CURDATE() = current date only [OK]
- Using NOW() when only date is needed
- Confusing CURTIME() with CURDATE()
- Using CURRENT_TIME() for date
SELECT NOW(), CURDATE(), CURTIME();
Solution
Step 1: Understand each function output
NOW()returns full date and time,CURDATE()returns date only,CURTIME()returns time only.Step 2: Match given time values
At 2024-06-15 14:30:45,NOW()= '2024-06-15 14:30:45',CURDATE()= '2024-06-15',CURTIME()= '14:30:45'.Final Answer:
'2024-06-15 14:30:45', '2024-06-15', '14:30:45' -> Option DQuick Check:
NOW() = full datetime, CURDATE() = date, CURTIME() = time [OK]
- Mixing order of outputs
- Assuming CURTIME() returns date
- Confusing NOW() with CURDATE()
SELECT NOW, CURDATE(), CURTIME();
Solution
Step 1: Check function syntax
MySQL functions require parentheses even if no arguments are passed.NOWwithout () is invalid.Step 2: Verify other functions
CURDATE()andCURTIME()have correct syntax with parentheses.Final Answer:
NOW is missing parentheses -> Option AQuick Check:
Functions need () even if empty [OK]
- Omitting parentheses for functions
- Assuming functions can be called without ()
- Thinking CURDATE() is incorrect
created_at column. Which query correctly uses MySQL functions to do this?Solution
Step 1: Understand the column data type
created_atusually stores full date and time, so we need a function that returns both.Step 2: Choose correct function
NOW()returns current date and time,CURDATE()returns only date,CURTIME()only time, and quoting function names inserts strings, not values.Final Answer:
INSERT INTO logs (created_at) VALUES (NOW()); -> Option CQuick Check:
Use NOW() for full datetime insert [OK]
- Using CURDATE() when time is needed
- Using CURTIME() when date is needed
- Inserting function name as string
Ready to prove it?
Pass the test and this pattern moves unknown
