Databases 3 min read

When MySQL Indexes Fail: 4 Common Pitfalls to Avoid

This article explains four situations where MySQL will ignore indexes—using a non‑leading column in a composite index, leading wildcards in LIKE queries, OR conditions with non‑indexed fields, and unquoted string literals—offering guidance to improve query performance.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
When MySQL Indexes Fail: 4 Common Pitfalls to Avoid

MySQL will not use an index in several situations.

1. Composite index with non‑leading column

If a composite index (a,b) is defined but the query condition uses column b instead of the first column a, the index is ignored. Using a as the condition will employ the index.

2. LIKE queries with a leading wildcard

A LIKE pattern that starts with '%' (e.g., WHERE a LIKE '%xxx') prevents index usage, while a trailing wildcard ( WHERE a LIKE 'xxx%') can still use the index. For extensive fuzzy searches, consider full‑text search engines such as Sphinx that support Chinese.

3. OR conditions containing non‑indexed columns

When an OR clause mixes an indexed column with a non‑indexed one (e.g., WHERE a='x' OR b='y' where b is not indexed), MySQL cannot use the index.

4. String literals without quotes

If a string‑type indexed column is compared without surrounding single quotes ( WHERE a=111), the index is not used. Using quotes ( WHERE a='111') enables the index. Unquoted strings are interpreted as column names, leading to errors.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLindexingmysqlDatabase Optimization
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.