Why is mysql using filesort when i order my selected rows with ORDER BY x?
My table looks likes this:
CREATE TABLE `test` (
`sdf` varchar(100) NOT NULL,
`sdf33` varchar(100) NOT NULL,
KEY `sdf_2` (`sdf`),
FULLTEXT KEY `sdf33` (`sdf33`),
FULLTEXT KEY `sdf` (`sdf`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
When running
EXPLAIN SELECT *
FROM `test`
ORDER BY sdf
mysql says it’s using filesort, why? What do I need to change for it to not use filesort?
The
FULLTEXTindex does not preseve ordering and can’t be used forORDER BY.But even if you had a
BTREEindex onsdf, it most probably would not be used too, since it’s generally more expensive to to the table lookups in a loop than scan the table sequentially and sort it.The index would be of use for
ORDER BY / LIMIT:The
LIMIThas a certain threshold after which the optimizer would prefer a filesort.You can force the optimizer to use the index:
if you really don’t want to have a filesort.