0
0
MySQLquery~5 mins

ABS and MOD in MySQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: ABS and MOD
O(n)
Understanding Time Complexity

We want to understand how the time it takes to run ABS and MOD functions changes as the amount of data grows.

How does the work needed grow when we apply these functions to many numbers?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


SELECT ABS(salary) AS absolute_salary, MOD(employee_id, 10) AS remainder
FROM employees;
    

This code calculates the absolute value of salaries and the remainder when employee IDs are divided by 10 for all employees.

Identify Repeating Operations
  • Primary operation: Applying ABS and MOD functions to each row in the employees table.
  • How many times: Once for every employee record (row) in the table.
How Execution Grows With Input

Each employee row requires two simple calculations. As the number of employees grows, the total work grows proportionally.

Input Size (n)Approx. Operations
1020 (2 operations x 10 rows)
100200 (2 operations x 100 rows)
10002000 (2 operations x 1000 rows)

Pattern observation: The total work grows directly with the number of rows; doubling rows doubles work.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows in a straight line with the number of rows processed.

Common Mistake

[X] Wrong: "ABS and MOD take the same time no matter how many rows there are because they are simple functions."

[OK] Correct: While each function is simple, they must be applied to every row, so more rows mean more total work.

Interview Connect

Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.

Self-Check

"What if we applied ABS and MOD only to a filtered subset of rows? How would that affect the time complexity?"