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?
Practice
IN parameter type in a MySQL stored procedure do?Solution
Step 1: Understand the role of IN parameters
IN parameters are used to pass values into a procedure. They act like input-only variables.Step 2: Clarify output behavior of IN parameters
Changes to IN parameters inside the procedure do not affect the caller's variables; they are input-only.Final Answer:
It passes a value into the procedure but does not allow changes to be sent back. -> Option BQuick Check:
IN = input only [OK]
- Thinking IN parameters can send data back
- Confusing IN with OUT or INOUT
- Assuming IN parameters can be modified outside
Solution
Step 1: Recall correct parameter declaration order
In MySQL, the parameter mode (IN, OUT, INOUT) comes before the parameter name and type.Step 2: Check each option's syntax
CREATE PROCEDURE myProc(OUT param INT) BEGIN END; uses 'OUT param INT' which is correct. Placing OUT after the parameter name or type is invalid syntax.Final Answer:
CREATE PROCEDURE myProc(OUT param INT) BEGIN END; -> Option CQuick Check:
Parameter mode before name and type [OK]
- Placing OUT after parameter name or type
- Mixing parameter mode with data type
- Omitting BEGIN and END block
CREATE PROCEDURE addValues(IN a INT, INOUT b INT, OUT c INT) BEGIN SET b = b + a; SET c = a + b; END; CALL addValues(5, @varB, @varC);
If
@varB is initially 10, what will be the values of @varB and @varC after the call?Solution
Step 1: Understand parameter roles and initial values
a = 5 (IN), b = 10 (INOUT), c is OUT (initially NULL). INOUT means b can be read and updated.Step 2: Calculate new values inside procedure
SET b = b + a = 10 + 5 = 15; SET c = a + b = 5 + 15 = 20.Final Answer:
@varB = 15, @varC = 20 -> Option AQuick Check:
INOUT b updated, OUT c set correctly [OK]
- Assuming INOUT parameter does not change
- Confusing OUT parameter initial value
- Ignoring that OUT must be assigned inside procedure
CREATE PROCEDURE testProc(INOUT x INT) BEGIN SET x = x + 1; END;
Which of the following calls will cause an error?
Solution
Step 1: Understand INOUT parameter requirements
INOUT parameters require a variable to pass by reference, so the procedure can read and modify it.Step 2: Analyze each call
CALL testProc(5); passes a literal (5), which is not a variable, causing an error. The other options pass variables or NULL variables, which are allowed.Final Answer:
CALL testProc(5); -> Option DQuick Check:
INOUT needs variable, not literal [OK]
- Passing literals to INOUT parameters
- Confusing INOUT with IN parameters
- Assuming NULL literal is invalid for INOUT
Solution
Step 1: Identify input and output roles
The two numbers are inputs only, so they should be IN parameters. The sum and product are results to send back, so they should be OUT parameters.Step 2: Match parameter types to roles
IN parameters accept input values; OUT parameters return output values. INOUT is for parameters used as both input and output, which is not needed here.Final Answer:
IN for both inputs, OUT for sum and product outputs -> Option AQuick Check:
Inputs = IN, Outputs = OUT [OK]
- Using INOUT unnecessarily for outputs
- Confusing OUT as input
- Using IN for output parameters
Ready to prove it?
Pass the test and this pattern moves unknown
