Procedure parameters (IN, OUT, INOUT) in MySQL - Time & Space 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.
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 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.
As the input_val grows, the number of loop steps grows the same way.
| Input Size (input_val) | Approx. Operations (loop steps) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to run the procedure grows in a straight line as the input number grows.
[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.
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.
What if the procedure had two nested loops both depending on input_val? How would the time complexity change?