I need some help in avoiding filesort for this query.
SELECT id
FROM articles USE INDEX(group开发者_如何学Python)
WHERE type = '4'
AND category = '161'
AND did < '10016'
AND id < '9869788'
ORDER BY id DESC
LIMIT 10
INDEX(group) is a covering index of (type
, category
, did
, id
)
Because of ORDER BY id DESC
, filesort is performed. Is there a way to avoid filesort for such query?
Change the index column order. The index is useless for the sort because it'^s the 4th column and isn't ready to be used as-is.
Of course, this affects the usefulness of the index for this WHERE because you need an inequality column before an equality one
In the MySQL docs, you break "You use ORDER BY on nonconsecutive parts of a key" and "The key used to fetch the rows is not the same as the one used in the ORDER BY"
Edit: as per my link above, you can't have an index that satisfies both WHERE and ORDER BY. They are mutually exclusive because of the 2 conditions I posted above
Another suggestion:
- a single column index on id
- go back to the original index too
- remove the index hint
- hope that the optimiser works out that both indexes can be used ("index intersection")
Create index on id and type it should work for you. If Id is unique you can also create the Primary Key for it.
your query is using file sort because it might be possible that the optimizer is using the first column of index you created in the query.
精彩评论