MySQL EXPLAIN Explained: const, ref, range, index, ALL
Read MySQL EXPLAIN: the type column shows how rows are found, and rows and filtered show how much work was wasted. Each access type with an example.
In this chapter
Reading MySQL EXPLAIN: const, ref, range, index and ALL
MySQL EXPLAIN: common questions
What does type ALL mean in MySQL EXPLAIN?
A full table scan: MySQL reads every row. Look for a missing index or a condition that cannot use one.
More on MySQL and InnoDB
- InnoDB storage: clustered pages, change buffer, adaptive hash, doublewrite
- InnoDB redo, the binlog and the two-phase commit between them
- InnoDB read views, undo purge and the history list
- InnoDB Gap Locks and Next-Key Locks Explained
- A primary key is known before execution starts
- MySQL settings that change query speed
- MySQL diagnosis: slow log, optimizer trace, performance schema
Keep learning
Overview
No usable index, or the predicate hides the column. The whole index is read in order, often because it covers the query. Add a WHERE the index can seek on, or reorder index columns to match it. An index exists but the optimizer judged it more expensive (low selectivity, stale statistics). rows large, filtered small The index narrows only a little, then most rows fail other conditions. Add the extra WHERE columns to the index (equality columns first, range last). Extra: Using filesort The ORDER BY cannot be read from an index in order, so rows are sorted afterwards. Extra: Using temporary A temporary table holds intermediate rows, typical of GROUP BY / DISTINCT on non-indexed columns. Index the grouped columns, or reduce rows earlier with a WHERE. Extra: Using index Good news: the index covers the query, so the table is not read. Keep the column list short so it stays covered. SELECT * ends it. Joined table has type ALL The join column has no index on the inner table, so it is scanned once per outer row. Index the join column of the table read second. chapters · mysql and innodb Put EXPLAIN in front of a query and MySQL tells you its plan without running it: which tables it reads, in what order, through which index, and how many rows it expects to look at. The single most useful column is type, the access type, which says how the table is reached.
1. The columns that matter
Read these first, in this order Question it answers How is this table accessed? The ladder below, best to worst. Which index was used? NULL means none. How many bytes of that index were used, so how many of its columns. Estimated rows examined in this step (an estimate, not a count). Estimated percent of those rows that survive the remaining conditions. Side notes: covering index, filesort, temporary table, pushed-down conditions. Choose a query and click any cell of its plan. The panel under the table explains that cell in plain words. In the join, the plan lists o before u because MySQL reads them in that order: first the outer loop, then one lookup per outer row. The order is chosen by cost, so you can change it by changing indexes or statistics, not by reordering the FROM clause (use STRAIGHT_JOIN only as a last resort).
2. The access types from best to worst
One line each, with a query that produces it At most one row, found once by primary or unique key against a constant. One row per row of the previous table, through a primary or unique key. Equality on an index that can match many rows. A slice of an index: BETWEEN, <, >, IN, LIKE 'abc%'. WHERE created_at >= ... Every entry of an index, in order. SELECT status FROM orders Every row of the table. The word sounds like success, but type: index means a full scan of the index. It beats ALL only because index entries are smaller than rows, so fewer pages are read. If the query touches a million rows in the table, it touches a million entries in the index. The good cousin is range, where the index is entered at one point and left at another.
3. rows times filtered is what the next step receives
rows is how many rows this step reads. filtered is the share that remains after conditions the index could not apply (they are checked on each row, shown as Using where). Multiply: rows x filtered / 100 is the estimated number of rows handed on. In a join that number is how many times the next table is probed, so a low filtered value on the first table is multiplied through the whole plan. A high rows with a low filtered is a step that does a lot of reading to keep very little.
Covering index
Every column the query needs is stored in the index, so the table is never read. In InnoDB a secondary index also carries the primary key, so SELECT id is covered for free. SELECT * usually destroys this, because it asks for columns the index does not hold.
Sorting after the fact
The ORDER BY could not be satisfied by reading an index in order, so MySQL sorts rows in memory or on disk. Despite the name it need not touch a file. Fix with an index whose column order matches the equality filters and then the sort.
Scratch table
Grouping or DISTINCT needed an internal temporary table. It is cheap for small inputs and costly when millions of rows are staged. An index on the grouped columns lets rows arrive already grouped.
5. Fix the plan
Real MySQL 8 also has EXPLAIN ANALYZE (8.0.18+), which runs the query and shows actual rows and timings next to the estimates. When the two differ a lot, the statistics are stale: run ANALYZE TABLE. In PostgreSQL the same idea is EXPLAIN (ANALYZE, BUFFERS), whose vocabulary differs: Seq Scan, Index Scan, Index Only Scan and Bitmap Heap Scan correspond roughly to ALL, ref/range, covering index and a multi-range scan.
Symptom to change
What you see, what it means, what to try Likely cause ✓ Recap
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.