ENUM and SET types in MySQL - Time & Space Complexity
We want to understand how the time it takes to work with ENUM and SET types changes as the data grows.
Specifically, how does MySQL handle these types when searching or filtering data?
Analyze the time complexity of the following code snippet.
CREATE TABLE products (
id INT PRIMARY KEY,
color ENUM('red', 'green', 'blue', 'yellow'),
tags SET('new', 'sale', 'popular', 'limited')
);
SELECT * FROM products WHERE color = 'red';
SELECT * FROM products WHERE FIND_IN_SET('sale', tags);
This code creates a table with ENUM and SET columns and queries rows filtering by these types.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows to compare ENUM or SET values.
- How many times: Once per row in the table during the query.
As the number of rows grows, the database checks each row's ENUM or SET value to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to find matching rows grows in a straight line as the table gets bigger.
[X] Wrong: "ENUM and SET types make searches instant no matter the table size."
[OK] Correct: Even though ENUM and SET store values efficiently, searching still checks each row unless there is an index.
Understanding how ENUM and SET types affect query time helps you explain data design choices clearly and shows you know how databases handle different data types.
"What if we add an index on the ENUM column? How would the time complexity change?"
Practice
ENUM and SET types in MySQL?Solution
Step 1: Understand
ENUMtypeENUMrestricts a column to one value from a predefined list.Step 2: Understand
SETtypeSETallows storing zero or more values from a predefined list in one column.Final Answer:
ENUMallows only one value,SETallows multiple values -> Option CQuick Check:
ENUM= single,SET= multiple [OK]
- Confusing which type allows multiple values
- Thinking ENUM stores numbers only
- Assuming SET is for single values
colors that can store multiple colors using SET type?Solution
Step 1: Recall correct syntax for SET
The correct syntax uses parentheses and single quotes: SET('value1', 'value2', ...)Step 2: Identify correct option
colors SET('red', 'green', 'blue') uses SET with parentheses and quoted values correctly.Final Answer:
colors SET('red', 'green', 'blue') -> Option AQuick Check:
SET uses parentheses and quotes [OK]
- Using square brackets instead of parentheses
- Mixing ENUM syntax with SET
- Omitting quotes around values
products with a column features SET('wifi', 'bluetooth', 'gps'), what will be the result of this query?SELECT features FROM products WHERE FIND_IN_SET('gps', features);Solution
Step 1: Understand FIND_IN_SET function
FIND_IN_SET('gps', features) checks if 'gps' is one of the comma-separated values in the SET column.Step 2: Interpret the query result
The query returns rows where 'gps' is included in the features set, not just exact matches.Final Answer:
Returns rows where 'gps' is one of the features. -> Option AQuick Check:
FIND_IN_SET finds value inside SET [OK]
- Thinking FIND_IN_SET matches exact whole column
- Assuming syntax error with FIND_IN_SET
- Confusing SET with ENUM behavior
CREATE TABLE users (status ENUM('active', 'inactive', 'pending'));Which of the following INSERT statements will cause an error?
Solution
Step 1: Check allowed ENUM values
The ENUM allows only 'active', 'inactive', or 'pending'. 'blocked' is not allowed.Step 2: Analyze each INSERT
Inserting 'blocked' will cause an error because it is not in the ENUM list. NULL is allowed if column permits it.Final Answer:
INSERT with 'blocked' causes error -> Option DQuick Check:
Only ENUM values allowed, others error [OK]
- Assuming NULL always causes error
- Thinking any string is allowed
- Confusing ENUM with SET behavior
Solution
Step 1: Identify requirement for multiple choices
Users can select any combination of preferences, so multiple values per row are needed.Step 2: Choose appropriate data type
SETtype allows storing multiple predefined values efficiently in one column.Step 3: Compare other options
ENUMallows only one value, VARCHAR wastes space and needs parsing, separate BOOLEANs increase columns.Final Answer:
Use SET('email', 'sms', 'push') for multiple preferences -> Option BQuick Check:
Multiple choices = SET type [OK]
- Using ENUM for multiple selections
- Storing comma-separated strings manually
- Adding many boolean columns unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
