Software Development

Why do wildcards conflict with best practices for writing efficient SQL queries?

NI Asked by Nicole Hawthorne · 19-07-2025
▲ 0 upvotes 6,622 views 0 comments
The question

I keep seeing senior developers flag the use of leading wildcards during code reviews. Can someone explain why using a leading wildcard conflicts so heavily with the best practices for writing efficient SQL queries? Is there any indexing strategy that can mitigate this issue?

3 answers

▲ 0
BR
Answered on 29-08-2025

A leading wildcard, such as using LIKE percent-term, completely destroys performance because B-Tree indexes are sorted from left to right. When you hide the beginning of the string, the database engine cannot perform an Index Seek to find matches; it is forced to read every single row in the index or table, resulting in a costly Full Table Scan. To stick to the best practices for writing efficient SQL queries when pattern matching, always use trailing wildcards instead, or implement a dedicated Full-Text Search index if substring matching is absolutely required.

▲ 0
PH
Answered on 14-10-2025

That makes perfect sense for standard B-Tree indexing limitations, but what if we are forced to stick to relational DBs and cannot spin up an external search engine? Is a trigram index a viable workaround?

KE 18-10-2025

Philip, yes it is. Trigram indexes break strings down into three-character chunks, allowing the engine to index the middle of strings. It works wonders for wildcard queries, though it does increase your write overhead and storage footprint.

▲ 0
DI
Answered on 03-12-2025

Using a leading wildcard guarantees a full table scan. If you need efficient lookups, rethink your schema or use trailing wildcards so the engine can utilize standard index trees.

NI 06-12-2025

Diana is totally right. We changed our search inputs to disallow front wildcards, and our server CPU utilization instantly dropped from ninety percent to a stable five percent.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Still have questions? Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Book Free Session

Book Free Session