0
0
MySQLquery~5 mins

Procedure parameters (IN, OUT, INOUT) in MySQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Procedure parameters (IN, OUT, INOUT)
O(n)
Understanding Time Complexity

We want to understand how the time it takes to run a stored procedure changes as the input size grows.

Specifically, how the use of IN, OUT, and INOUT parameters affects this time.

Scenario Under Consideration

Analyze the time complexity of the following stored procedure.


CREATE PROCEDURE example_proc(IN input_val INT, OUT output_val INT, INOUT inout_val INT)
BEGIN
  DECLARE i INT DEFAULT 0;
  SET output_val = 0;
  WHILE i < input_val DO
    SET output_val = output_val + i;
    SET inout_val = inout_val + 1;
    SET i = i + 1;
  END WHILE;
END
    

This procedure loops from 0 up to the input value, updating the OUT and INOUT parameters each time.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The WHILE loop that runs from 0 to input_val - 1.
  • How many times: Exactly input_val times.
How Execution Grows With Input

As the input_val grows, the number of loop steps grows the same way.

Input Size (input_val)Approx. Operations (loop steps)
1010
100100
10001000

Pattern observation: The number of operations grows directly with the input size.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the procedure grows in a straight line as the input number grows.

Common Mistake

[X] Wrong: "OUT and INOUT parameters make the procedure run slower in a way that changes the time complexity."

[OK] Correct: The parameters themselves do not add loops or repeated work; they just hold values. The main time depends on the loop count, not the parameter type.

Interview Connect

Understanding how procedure parameters and loops affect time helps you explain how your code scales with input size.

This skill shows you can think about efficiency, which is important in real projects and interviews.

Self-Check

What if the procedure had two nested loops both depending on input_val? How would the time complexity change?