String types (VARCHAR, CHAR, TEXT) in MySQL - Time & Space Complexity
When working with string types in MySQL, it's important to understand how the size of the data affects the time it takes to store and retrieve it.
We want to see how the time needed grows as the string length or number of rows increases.
Analyze the time complexity of inserting and selecting data using different string types.
CREATE TABLE example (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
code CHAR(10),
description TEXT
);
INSERT INTO example (name, code, description) VALUES
('Alice', 'A123456789', 'Short description'),
('Bob', 'B987654321', 'Longer description text here...');
SELECT * FROM example WHERE name = 'Alice';
This code creates a table with VARCHAR, CHAR, and TEXT columns, inserts some rows, and selects data by name.
Look at what repeats when handling string data in this example.
- Primary operation: Reading or writing string data for each row.
- How many times: Once per row inserted or selected, repeated for all rows in the table.
As the number of rows grows, the total time to insert or select all rows grows roughly in direct proportion.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | 10 string reads/writes |
| 100 | 100 string reads/writes |
| 1000 | 1000 string reads/writes |
Pattern observation: The time grows linearly with the number of rows because each row's string data is handled separately.
Time Complexity: O(n)
This means the time to process string data grows directly with the number of rows involved.
[X] Wrong: "String types like TEXT always take the same time regardless of length."
[OK] Correct: Longer strings take more time to read and write, so time depends on string length and number of rows.
Understanding how string data size affects query time helps you explain database performance clearly and confidently.
"What if we changed the VARCHAR size limit from 100 to 1000? How would the time complexity change when inserting or selecting data?"
Practice
Solution
Step 1: Understand string types for short variable-length text
VARCHARstores variable-length strings efficiently, saving space for shorter values.Step 2: Compare with other types
CHARis fixed-length and wastes space if data varies;TEXTis for large text;BLOBis for binary data.Final Answer:
VARCHAR -> Option AQuick Check:
Variable-length short text = VARCHAR [OK]
- Choosing CHAR wastes space for variable length
- Using TEXT for small strings is inefficient
- Confusing BLOB with text types
description that can hold up to 255 characters using VARCHAR in MySQL?Solution
Step 1: Recall VARCHAR syntax
VARCHAR requires a length in parentheses, e.g., VARCHAR(255).Step 2: Check options for correct syntax
description VARCHAR(255)uses correct syntax;description TEXT(255)uses wrong type;description CHAR(255)uses fixed-length CHAR;description VARCHARmisses length.Final Answer:
description VARCHAR(255) -> Option AQuick Check:
VARCHAR needs length in parentheses [OK]
- Omitting length for VARCHAR
- Adding length to TEXT type
- Confusing CHAR and VARCHAR syntax
users with a username CHAR(10) column, what will be the result of this query?INSERT INTO users (username) VALUES ('bob');
SELECT username, LENGTH(username) FROM users WHERE username = 'bob';Solution
Step 1: Understand CHAR behavior on insert
CHAR(10) stores fixed length strings, padding with spaces to length 10.Step 2: Check LENGTH function on padded string
LENGTH counts all characters including spaces, so LENGTH('bob ') = 10.Final Answer:
username = 'bob ', LENGTH(username) = 10 -> Option DQuick Check:
CHAR pads with spaces, LENGTH counts all chars [OK]
- Assuming LENGTH returns 3 for 'bob'
- Ignoring space padding in CHAR
- Confusing CHAR with VARCHAR behavior
notes TEXT. You try to insert a string longer than 65,535 characters and get an error. What is the likely cause and how to fix it?Solution
Step 1: Recall TEXT max size limits
TEXT can store up to 65,535 bytes; longer text causes error.Step 2: Choose correct larger text type
MEDIUMTEXT supports up to 16 million bytes, suitable for longer text.Final Answer:
TEXT type has a max length of 65,535 bytes; use MEDIUMTEXT for longer text -> Option BQuick Check:
TEXT max 65,535 bytes; MEDIUMTEXT for more [OK]
- Thinking TEXT has unlimited length
- Using VARCHAR for very large text
- Confusing CHAR with TEXT size limits
Solution
Step 1: Consider storage needs for very long text
VARCHAR max length is limited (usually 65,535 bytes total row size), so very long comments may exceed it.Step 2: Evaluate TEXT type for large variable text
TEXT stores large text efficiently without fixed length and supports very long strings.Final Answer:
Use TEXT because it stores large text without fixed length -> Option CQuick Check:
Large variable text = TEXT type [OK]
- Choosing fixed-length CHAR wastes space
- Using VARCHAR too large causes errors
- Assuming VARCHAR can store unlimited text
Ready to prove it?
Pass the test and this pattern moves unknown
