UPPER and LOWER in MySQL - Time & Space Complexity
We want to understand how the time it takes to change text to uppercase or lowercase grows as the text gets longer.
How does the work increase when we use UPPER or LOWER on bigger text?
Analyze the time complexity of the following code snippet.
SELECT UPPER(name) FROM employees;
SELECT LOWER(description) FROM products;
This code changes all letters in the 'name' or 'description' columns to uppercase or lowercase for every row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row, the function processes each character in the text string.
- How many times: Once for every character in every row's text.
As the text length grows, the work grows too because each character needs to be changed.
| Input Size (characters per row) | Approx. Operations (per row) |
|---|---|
| 10 | 10 character changes |
| 100 | 100 character changes |
| 1000 | 1000 character changes |
Pattern observation: The work grows directly with the number of characters; double the characters, double the work.
Time Complexity: O(n)
This means the time to convert text grows in a straight line with the length of the text.
[X] Wrong: "UPPER and LOWER run instantly no matter how long the text is."
[OK] Correct: Each character must be checked and changed, so longer text takes more time.
Knowing how string functions like UPPER and LOWER scale helps you explain performance when working with text data in databases.
"What if we applied UPPER or LOWER only to a fixed small part of the text instead of the whole string? How would the time complexity change?"