LENGTH and CHAR_LENGTH in MySQL - Time & Space Complexity
We want to understand how the time to find the length of strings grows as we check more or longer strings.
How does the work change when the input strings get bigger or when we have more strings?
Analyze the time complexity of the following code snippet.
SELECT LENGTH(name), CHAR_LENGTH(name)
FROM employees;
This code gets the byte length and character count of the 'name' field for every employee.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row, the database reads the string and counts bytes or characters.
- How many times: Once per row in the table, so it repeats for every employee.
As the number of rows grows, the total work grows too because each string is checked once.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | 10 string length checks |
| 100 | 100 string length checks |
| 1000 | 1000 string length checks |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles the work.
Time Complexity: O(n * m)
This means the time grows with the number of rows (n) and the length of each string (m).
[X] Wrong: "Counting string length is always quick and does not depend on string size."
[OK] Correct: The database must look at each character or byte to count, so longer strings take more time.
Knowing how string length functions scale helps you understand query costs when working with text data in databases.
"What if we only checked the length of strings for a filtered subset of rows? How would the time complexity change?"
Practice
LENGTH() return when used on a string?Solution
Step 1: Understand LENGTH() function
The LENGTH() function returns the number of bytes used by the string, not characters.Step 2: Compare with CHAR_LENGTH()
CHAR_LENGTH() counts characters, LENGTH() counts bytes, which can differ for multibyte characters.Final Answer:
The number of bytes in the string -> Option DQuick Check:
LENGTH() = bytes count [OK]
- Confusing LENGTH() with CHAR_LENGTH()
- Assuming LENGTH() counts characters
- Thinking LENGTH() counts words
name in MySQL?Solution
Step 1: Identify function for character count
CHAR_LENGTH() returns the number of characters in a string, which is what we want.Step 2: Check syntax correctness
SELECT CHAR_LENGTH(name) FROM users; is valid syntax to get character count per row.Final Answer:
SELECT CHAR_LENGTH(name) FROM users; -> Option BQuick Check:
CHAR_LENGTH() counts characters [OK]
- Using LENGTH() instead of CHAR_LENGTH() for characters
- Using COUNT() which counts rows, not string length
- Using non-existent SIZE() function
messages with a column text containing the value 'café', what will the query SELECT LENGTH(text), CHAR_LENGTH(text) FROM messages; return if the encoding is UTF-8?Solution
Step 1: Understand UTF-8 encoding for 'café'
The character 'é' uses 2 bytes in UTF-8, so total bytes = 3 (c,a,f) + 2 (é) = 5 bytes.Step 2: Count characters vs bytes
CHAR_LENGTH counts characters: 'c','a','f','é' = 4 characters. LENGTH counts bytes = 5.Final Answer:
5, 4 -> Option AQuick Check:
LENGTH=5 bytes, CHAR_LENGTH=4 chars [OK]
- Assuming LENGTH and CHAR_LENGTH return same value
- Ignoring multibyte characters in UTF-8
- Swapping LENGTH and CHAR_LENGTH results
SELECT LENGTH('hello'), CHAR_LENGTH('hello'); but got an error. What is the likely cause?Solution
Step 1: Check syntax of the query
SELECT LENGTH('hello'), CHAR_LENGTH('hello'); is valid in MySQL and does not require FROM clause.Step 2: Verify function existence and quotes
LENGTH and CHAR_LENGTH are valid functions; single quotes are correct for string literals.Final Answer:
No error; the query is valid and returns results -> Option AQuick Check:
Query syntax is valid [OK]
- Thinking FROM clause is mandatory in all SELECT queries
- Confusing string quotes usage
- Assuming LENGTH or CHAR_LENGTH are invalid functions
users where the username column has exactly 5 characters, but some usernames contain multibyte UTF-8 characters. Which query correctly finds these rows?Solution
Step 1: Understand difference between LENGTH and CHAR_LENGTH
LENGTH counts bytes, which can be more than characters for multibyte UTF-8 chars.Step 2: Choose function that counts characters
CHAR_LENGTH counts characters correctly regardless of byte size, so use CHAR_LENGTH(username) = 5.Final Answer:
SELECT * FROM users WHERE CHAR_LENGTH(username) = 5; -> Option CQuick Check:
Use CHAR_LENGTH for character count in UTF-8 [OK]
- Using LENGTH() which counts bytes, not characters
- Assuming byte count equals character count
- Using incorrect comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
