A MySQL product-search query's EXPLAIN shows type ALL, key NULL and Extra 'Using where; Using filesort'. What does each part tell you, and how would you fix the query's access path?
answer
- no candidate index at all
- the server filters, the engine scans
- an extra sorting pass
- equality columns, then the sort column
basics
~20 stype ALL with key NULL means a full scan with no usable index; Using where means the server discards most rows read; Using filesort means an extra sort pass. Index the equality columns, then the ORDER BY column.
solid answer
~40 s`type: ALL` with `possible_keys` and `key` `NULL` says InnoDB reads every row because no index matches the `WHERE` clause. `Using where` says the server then applies the condition to discard rows, and a small `filtered` value says most are thrown away. `Using filesort` says MySQL must sort the matching rows in an extra pass because no index delivers them in `ORDER BY` order; it does not by itself mean the sort spilled to disk. For `WHERE category_id = ? AND is_active = 1 ORDER BY price LIMIT 24`, an index on `(category_id, is_active, price)` lets MySQL use `ref` access on the two equality columns and read rows already ordered by `price`, so it stops after 24. Re-run `EXPLAIN` and expect `type: ref`, the new `key`, `ref: const,const` and no `Using filesort`.
code
plaintext · 12 linesid: 1
select_type: SIMPLE
table: products
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1987654
filtered: 1.00
Extra: Using where; Using filesortgo deeper
Recall that ALL is a full table scan and that Using filesort means an extra sort pass, not necessarily on disk.
Walk through each column of the plan and explain how an index on the equality columns followed by the sort column removes both the scan and the sort.
Diagnose from the plan alone, predict the new plan before re-running EXPLAIN, and name the cases where an index still cannot remove the filesort.
Decide which slow-query signals a team acts on, such as full scans on hot tables, and weigh each new index's write cost against the read latency it buys.
## The plan, column by column The product-search page runs this query against a table of about two million products: ```sql SELECT id, name, price FROM products WHERE category_id = 42 AND is_active = 1 ORDER BY price LIMIT 24; ``` `EXPLAIN` returns one row for `products` with `type: ALL`, `possible_keys: NULL`, `key: NULL`, `rows` near two million, `filtered` around 1 and `Extra: Using where; Using filesort`. Each piece says something different: - **`type: ALL`**: a full table scan. Every row of the clustered index is read. - **`possible_keys: NULL`**: no index is even relevant to the conditions. That separates this case from one where an index exists and the optimizer judged it too costly. - **`rows` × `filtered`**: about 2,000,000 × 1% ≈ 20,000 rows survive the filter. Reading two million to keep twenty thousand is the waste. - **`Using where`**: the server applies the `WHERE` condition to rows the engine returns. With `ALL` it confirms that filtering happens after the full read. - **`Using filesort`**: MySQL needs an extra pass to put the matching rows in `ORDER BY` order. It collects sort keys for every row that passes the filter, sorts them, then returns rows in order. ## What "filesort" does and does not mean The name misleads. A filesort is MySQL's general sort algorithm, not a promise of disk I/O: - It can run entirely in the sort buffer, whose size is bounded by `sort_buffer_size`; it uses temporary files only when the data does not fit. - `EXPLAIN` **does not distinguish** an in-memory sort from one that spills. The optimizer trace's `filesort_summary` (`number_of_tmp_files`, `peak_memory_used`) is where that shows. - With `LIMIT`, MySQL can keep only the top rows in a priority queue, but it still has to examine every row that passes the filter. The real cost here is that 20,000 rows must be found and sorted before the first 24 can be returned. ## The fix: let one index filter and order ```sql ALTER TABLE products ADD INDEX idx_cat_active_price (category_id, is_active, price); ``` With equality on `category_id` and `is_active`, the entries for one category and active flag sit together and are already ordered by `price`. MySQL can: 1. seek to `(42, 1)` with `ref` access, comparing two constants; 2. read entries in `price` order, so no sort pass is needed; 3. stop after 24 matches, fetching only those rows from the clustered index. Re-running `EXPLAIN` should show `type: ref`, `key: idx_cat_active_price`, `ref: const,const`, a small `rows`, and **no** `Using filesort`. Why the equality columns come before the sorted column is the general rule for multi-column indexes, covered with leftmost-prefix order. ## The other Extra values you will be handed | Extra value | What MySQL is doing | Usually means | |---|---|---| | `Using index` | reads columns from the index tree only | a covering index; good | | `Using index condition` | Index Condition Pushdown: tests index columns before reading the full row | fewer row reads; InnoDB uses it for secondary indexes only | | `Using where` | the server applies a condition to rows returned | fine with `ref`; a warning sign with `ALL` or `index` | | `Using filesort` | an extra sort pass for `ORDER BY` | no index supplies the order | | `Using temporary` | an internal temporary table holds the result | typical when `GROUP BY` and `ORDER BY` list columns differently | | `Backward index scan` | an InnoDB index is read in reverse | the order matches the index reversed | | `Using union(...)`, `Using intersect(...)` | Index Merge across several indexes of one table | `type` is `index_merge` | ## When the index is not enough - **Range before the sort column.** `WHERE category_id = 42 AND price > 100 ORDER BY name` cannot be served in `name` order by an index on `(category_id, price, name)`, because the range on `price` breaks the `name` order, so `Using filesort` returns. - **Mixed directions.** `ORDER BY price DESC, name ASC` needs an index whose `ASC`/`DESC` parts match that order or its exact reverse. - **Sort on an expression.** `ORDER BY ABS(price)` or `ORDER BY -price` cannot use a plain index on `price`. - **A prefix index on the sort column** cannot fully resolve the order, so a filesort is needed. - **`Using temporary; Using filesort` together** usually comes from grouping by one column and ordering by another; the fix is in the query's shape, not only in indexing. Timing each step of the plan, rather than reading estimates, is `EXPLAIN ANALYZE`, covered with query and server tuning.
- In MySQL, after adding the index, EXPLAIN shows Using index condition. What does that mean, and is it a problem?It means Index Condition Pushdown: the server pushes the part of the `WHERE` clause that can be checked from index columns down to InnoDB, which tests each index entry before reading the full row. It reduces row reads, so it is a good sign. InnoDB applies it only to secondary indexes, and only for `range`, `ref`, `eq_ref` and `ref_or_null` access when full rows are needed.
- In MySQL, a report query shows Extra 'Using temporary; Using filesort'. What usually causes that pairing?MySQL needs an internal temporary table to hold the result and then sorts it, which typically happens when `GROUP BY` and `ORDER BY` name different columns; it can also appear when a join is ordered by columns that do not come from the first table read. An index on the grouping columns can remove the temporary table; ordering by the grouped columns, or sorting in a smaller outer query, can remove the sort.
- Using filesort means MySQL sorted the rows on disk
- Using where shows that an index was used to filter
- LIMIT 24 means only 24 rows are read
- Any index on category_id alone removes the filesort
- type ALL is fine as long as the result is small