Why Is MySQL Not Using My Index? Common Reasons
Functions on the column, type conversions, leading wildcards, OR, low selectivity and stale statistics. Why an index exists and is still ignored.
In this chapter
When an index exists and is still ignored or slow
Unused indexes: common questions
Why is my index not used?
Common causes: a function or calculation on the column, an implicit type conversion, LIKE with a leading %, OR across columns, or a filter that matches so many rows a full scan is cheaper.
What is a sargable query?
One whose WHERE conditions compare the bare column to a value, so the database can seek the index. WHERE YEAR(created_at) = 2026 is not sargable; a date range is.
More on Indexes: how queries use them
- Why an Index Lookup Is Still Slow: Key Lookups Explained
- Covering Index in SQL Explained with Examples
- Composite Index and the Leftmost Prefix Rule Explained
- Index Range Scan Explained: BETWEEN, > and < on a B-Tree
- ORDER BY and GROUP BY Using an Index (Avoid Filesort)
- B-Tree vs Hash Index: Which to Use and Why
Keep learning
Overview
Function or arithmetic on the column DATE(col), LOWER(col), col + 1 Type mismatch Character set or collation mismatch joins between utf8mb4 and latin1 Leading wildcard LIKE '%abc' Reverse-text column, FULLTEXT, or a different search design. OR over unindexed branches Index each branch (index merge), or rewrite as UNION. UNION ALL repeats rows that match both branches unless the second one excludes the first. Low selectivity status <> 'paid' on 95% rows A scan is right. Narrow the query or cover it. Leftmost prefix skipped Add the leading column to the query, or a second index. Range stops the prefix Put equalities first, ranges last. Stale statistics table changed a lot since ANALYZE Tiny table 200 rows, one page Nothing to fix: one page is read either way. chapters · indexes When an index exists and is still ignored or slow You created the index and the query is still slow. Usually the optimizer is not being stubborn: the query is written in a way an ordered structure cannot use, or reading the table really is cheaper. This chapter is a field guide to the common reasons, with a rewrite for each, then how to tell a justified scan from a mistake. The lab covers nine query shapes. The reference table at the end adds stale statistics and tiny tables.
1. Nine queries and why the index is not used
Every row below is a table of 2 million users with a sensible index. Choose a query, read why it fails, then switch to the rewrite and compare rows examined. The index basics (what a seek needs) are in index lookup and range scan.
2. Low selectivity: the scan that is correct
An index lookup visits matching rows one at a time, each a random table read. A scan reads all pages in order, and sequential reads are cheaper per row. Past some share of the table the scan wins. Where the line falls depends on row width, storage and caching, but 20 to 30% is the usual ballpark for ordinary data, and much lower on spinning disks. ⚙ Tell PostgreSQL the disk is fast random_page_cost defaults to 4: the planner assumes a random read costs four times a sequential one, which suits a spinning disk. On an SSD, or when the data fits in memory, a value near 1.1 makes it use indexes more readily. Change it, then compare plans on real queries before keeping it.
3. type=index is not type=ALL, and neither is always bad
So read the line, not the word: ask how many entries the plan will visit, and whether each one needs a trip to the table. The row estimates and EXPLAIN columns are in reading EXPLAIN.
4. When two indexes compete
Joins have the same choice between join orders. The way out is the same each time: an index that makes one plan clearly the best, here (type, created_at), so the planner has nothing to decide.
5. Test a drop with an invisible index
You can never be sure that nothing uses an index, and rebuilding a dropped one on a large table takes a long time. MySQL 8 lets you hide an index from the optimizer first. It is still maintained on every write, so nothing is saved yet, but bringing it back is instant. Some desktop tools do not mark invisible indexes. Check INFORMATION_SCHEMA.STATISTICS.IS_VISIBLE instead. PostgreSQL has no equivalent, so use pg_stat_user_indexes.idx_scan there.
Reference: causes and fixes
Why an index is ignored or ineffective One note on OR: MySQL can merge two index ranges (type: index_merge, Using union(...)) when both branches have indexes, but it reads two ranges and merges the row ids, so a single composite index or a UNION is often faster. Also remember NOT IN with a NULL in the list returns nothing: a correctness trap as well as a performance one.
Recap
An index is sorted by the stored value. Functions, arithmetic, type or collation conversions on the column break that, so move the work to the constant side. A leading wildcard has no starting point. A reversed column or FULLTEXT is the way out. Composite indexes: start from the left, equalities before ranges, and the first range ends the narrowing. When a query touches a large share of the table, a scan is cheaper than an index. Do not force the index without measuring. type=index is a full index scan: fine when covered or stopped early by LIMIT, bad when every entry needs a table row. Check statistics with ANALYZE TABLE, and remember a tiny table never needs an index.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.