ROUND, CEIL, FLOOR in MySQL - Time & Space Complexity
We want to understand how the time it takes to run rounding functions changes as the amount of data grows.
How does using ROUND, CEIL, or FLOOR on many numbers affect performance?
Analyze the time complexity of the following code snippet.
SELECT ROUND(price, 2) AS rounded_price,
CEIL(price) AS ceiling_price,
FLOOR(price) AS floor_price
FROM products;
This query applies rounding functions to each price in the products table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying ROUND, CEIL, and FLOOR to each row's price.
- How many times: Once per row in the products table.
Each row requires the functions to run once, so the total work grows as the number of rows grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 function calls |
| 100 | About 300 function calls |
| 1000 | About 3000 function calls |
Pattern observation: The work increases directly with the number of rows.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of rows processed.
[X] Wrong: "Rounding functions run instantly no matter how many rows there are."
[OK] Correct: Each row needs its own calculation, so more rows mean more work and more time.
Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.
"What if we added a WHERE clause to filter rows before applying ROUND, CEIL, and FLOOR? How would the time complexity change?"