Why LIKE '%term' Destroys Database Performance
🎯 The Question
"If a table column
namehas a B+ Tree index, why doesWHERE name LIKE 'John%'execute in 1 ms, butWHERE name LIKE '%John'takes 10 seconds and consumes 100% CPU?"
⚡ 30-Second Elevator Pitch
Database indexes (B+ Trees) store keys in strictly sorted alphabetical order (like a printed telephone directory).
- Trailing Wildcard (
LIKE 'John%'): Because the prefix is known, the database performs a fast binary search () to jump directly to the first"John", and scans forward until"Joho". - Leading Wildcard (
LIKE '%John'): Because the prefix is unknown, the target could be"Elton John","Papa John", or"Little John". The sorted tree structure is completely useless, forcing the engine to abandon the index and perform a Full Table Scan (), inspecting every single string from disk.
🧠 The Phonebook Analogy: B-Tree Left-Prefix Matching
🔬 How to Optimize Wildcard Searches in Production
- Reverse String Indexing (for suffixes):
- If querying suffixes like
LIKE '%@gmail.com', store a generated columnREVERSE(email)with an index, and searchWHERE reverse_email LIKE 'moc.liamg@%'.
- If querying suffixes like
- Trigram Indexes (
pg_trgmin PostgreSQL):- Breaks text into 3-letter trigrams (e.g.,
"John"["joh", "ohn"]) inside a GIN (Generalized Inverted Index).
- Breaks text into 3-letter trigrams (e.g.,
- Full-Text Search Engines:
- For arbitrary substring searches across large texts, use dedicated inverted index engines like Elasticsearch / Meilisearch.
📌 Comparison Matrix: SQL Wildcard Query Performance
| SQL Query Pattern | Index Usability | Search Mechanism | Query Complexity |
|---|---|---|---|
WHERE name = 'John' | ✅ 100% Index Match | Exact B+ Tree Point Lookup | |
WHERE name LIKE 'John%' | ✅ Index Range Scan | Left-prefix binary search + scan | |
WHERE name LIKE '%John' | ❌ Index Abandoned | Full Table Scan (Heap disk read) | |
WHERE name LIKE '%John%' | ❌ Index Abandoned | Full Table Scan (Unless GIN Trigram) |
💡 What Interviewers Ask Next (Follow-Up Traps)
-
"What is the Leftmost Prefix Rule in Composite Indexes?"
- Answer: For a composite index on
(A, B, C), queries filtering on(A)or(A, B)or(A, B, C)can use the index. However, queries filtering on(B, C)alone cannot use the index because the leading columnAis missing, breaking the sorted hierarchy.
- Answer: For a composite index on
-
"Does
WHERE UPPER(name) LIKE 'JOHN%'use a standard index onname?"- Answer: No. Applying a function to an indexed column prevents the B+ Tree from matching raw keys. You must create a Function-Based Index (e.g.
CREATE INDEX idx ON users (UPPER(name));) to enable index lookup.
- Answer: No. Applying a function to an indexed column prevents the B+ Tree from matching raw keys. You must create a Function-Based Index (e.g.
Placement & Interview Takeaway
Interview Answer: B+ Tree indexes require a known left prefix to perform binary searches down the tree hierarchy. Leading wildcard queries (%term) break left-prefix ordering, forcing the database engine to perform an expensive Full Table Scan across the entire dataset.
📺 Video Explanation
💬