Databases 3 min read

MySQL Queries That Cannot Use Indexes

This article explains five common MySQL query patterns—using functions, leading wildcards with LIKE, OR operators, inequality comparisons, and NULL checks—that prevent the database engine from utilizing indexes, and provides example SQL statements illustrating each case.

php Courses
php Courses
php Courses
MySQL Queries That Cannot Use Indexes

MySQL is a widely used relational database that relies on indexes to speed up queries, but certain SQL statements prevent index usage.

1. Using functions – When a function such as UPPER() is applied to a column in the WHERE clause, MySQL cannot use the index. SELECT * FROM customers WHERE UPPER(name) = 'JOHN'; 2. Using LIKE with a leading wildcard – A LIKE pattern that starts with % (e.g., '%john') forces a full scan because MySQL can only use indexes for prefix matches. SELECT * FROM customers WHERE name LIKE '%john'; 3. Using the OR logical operator – When an OR combines conditions on different columns, MySQL may abandon the index for a table scan. SELECT * FROM customers WHERE name = 'john' OR age = 30; 4. Using inequality operators – Conditions with <> or != prevent index usage because they require scanning all rows to find non‑matching values. SELECT * FROM customers WHERE age <> 30; 5. Using NULL checks – Queries that test for NULL with IS NULL cannot leverage indexes, as indexes do not store missing values. SELECT * FROM customers WHERE name IS NULL; To improve performance, avoid these patterns or rewrite the queries so that indexes can be applied.

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.

SQLmysqlDatabase Optimizationindexesquery-performance
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.