← All writing

Know why a leading wildcard changes a search query

MySQL 8.0+ / InnoDBSources checked 2026-09-14

A product search for AX% and a search for %AX% look similar to a user, but they ask the database to do different work. A prefix search can align with an ordinary ordered index in suitable conditions; a contains search generally cannot use the same prefix range.

Match the search behavior to the field

SELECT id, sku FROM products WHERE sku LIKE 'AX%';

For a known supplier-code prefix, this may be the intended behavior. For arbitrary words in a product description, a dedicated full-text or search system may fit better. Do not silently change contains search to prefix search solely to make a benchmark look faster.

Wildcards are part of the input contract

If a user enters % or _, decide whether those characters are literal or wildcard syntax. Bound parameters protect query construction, but do not neutralize LIKE wildcards. Escape according to the database and documented search behavior when a literal match is required.

Collation affects case and accent matching. Test actual customer-facing examples instead of assuming every deployment has the same defaults.

Measure with realistic selectivity: a prefix matching half the catalog is different from one matching five products. Verify the plan and page latency, and cap result sizes. A search endpoint also needs sensible request limits so an expensive broad query cannot monopolize the shared hosting database through repeated requests.

Reference

Official documentation.