LIKE pattern matching in MySQL - Time & Space Complexity
When searching text in a database, LIKE pattern matching helps find rows that fit a pattern.
We want to understand how the search time changes as the data grows.
Analyze the time complexity of the following code snippet.
SELECT *
FROM products
WHERE product_name LIKE '%phone%';
This query finds all products with "phone" anywhere in their name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row's product_name against the pattern.
- How many times: Once for every row in the products table.
As the number of rows grows, the database checks more product names one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the search time grows in a straight line as the table gets bigger.
[X] Wrong: "LIKE with % in front is fast because it uses indexes."
[OK] Correct: When % is at the start, indexes usually can't help, so the database checks every row.
Understanding how pattern searches scale helps you explain database performance clearly and confidently.
"What if the pattern was 'phone%' instead of '%phone%'? How would the time complexity change?"
Practice
% symbol represent in a MySQL LIKE pattern?Solution
Step 1: Understand the role of % in LIKE
The % symbol in a LIKE pattern matches any sequence of characters, including an empty sequence.Step 2: Compare with other wildcard symbols
The _ symbol matches exactly one character, not a sequence. % is for zero or more characters.Final Answer:
Any sequence of zero or more characters -> Option AQuick Check:
% means any characters [OK]
- Confusing % with _
- Thinking % matches only one character
- Assuming % matches digits only
Solution
Step 1: Identify pattern for names starting with 'Jo'
Names starting with 'Jo' means 'Jo' followed by any characters, so pattern is 'Jo%'.Step 2: Check each option
WHERE name LIKE 'Jo%' uses 'Jo%' which matches strings starting with 'Jo'. Others do not match this pattern correctly.Final Answer:
WHERE name LIKE 'Jo%' -> Option AQuick Check:
Start with 'Jo' = 'Jo%' [OK]
- Putting % before 'Jo' instead of after
- Using _ instead of %
- Misplacing wildcards
users with a column email, what will this query return?SELECT email FROM users WHERE email LIKE '%@gmail._om';Solution
Step 1: Analyze the pattern '%@gmail._om'
The % matches any characters before '@gmail.', then _ matches exactly one character, then 'om' follows.Step 2: Understand what emails match
Emails ending with '@gmail.com', '@gmail.dom', '@gmail.xom' etc. match because _ matches one character before 'om'.Final Answer:
Emails ending with '@gmail.com' or '@gmail.dom' etc. (one char before 'om') -> Option CQuick Check:
_ matches one char before 'om' [OK]
- Thinking _ matches zero or many characters
- Ignoring the position of _ in pattern
- Assuming % matches only one character
SELECT * FROM products WHERE name LIKE 'TV_';What is the likely problem?
Solution
Step 1: Understand the pattern 'TV_'
The pattern 'TV_' matches strings starting with 'TV' and exactly one more character, so total length 3.Step 2: Reason why no rows returned
If no product names are exactly 3 characters starting with 'TV', no rows match. Longer names like 'TVS' or 'TV42' won't match.Final Answer:
The pattern 'TV_' matches only names with exactly 3 characters starting with 'TV' -> Option DQuick Check:
_ means exactly one char [OK]
- Thinking _ matches zero or many characters
- Using LIKE incorrectly with =
- Assuming 'TV_' matches longer names
Solution
Step 1: Understand the requirement
Phone numbers start with '555' and have exactly 10 digits total, so 3 fixed digits + 7 more digits.Step 2: Count underscores needed
Each underscore _ matches exactly one character. After '555', we need 7 underscores to make total length 10.Step 3: Check options
SELECT * FROM customers WHERE phone LIKE '555_______'; has '555' plus 7 underscores, matching exactly 10 characters. Others have wrong underscore counts or use % which allows variable length.Final Answer:
SELECT * FROM customers WHERE phone LIKE '555_______'; -> Option BQuick Check:
7 underscores after '555' = 10 chars total [OK]
- Using % which allows variable length
- Using wrong number of underscores
- Confusing _ with %
Ready to prove it?
Pass the test and this pattern moves unknown
