How can I to avoid using filesort? Or is it impossible? I have read a lot of topics on SO, but couldn't understand right answer.
You would need an index over the priority
column:
ALTER TABLE navigation ADD INDEX (priority);
However, the chances are that MySQL will calculate that using such an index to sort the results will ultimately be more expensive than a filesort
(since the former will involve sequentially reading the index file in order to perform random I/O into the table, whereas the latter will involve sequentially reading the table and performing an in memory sort on the results). You can override this assessment with an index hint:
SELECT t.id, t.parent_id, t.title, t.priority
FROM navigation t FORCE INDEX FOR ORDER BY (priority)
ORDER BY t.priority ASC;
A covering index would altogether avoid the need to read into the table and thus could immediately return results merely from walking sequentially through the index file; it would therefore likely be selected by the query optimiser without further hinting:
ALTER TABLE navigation ADD INDEX(priority, id, parent_id, title);
Which approach is right for you will depend on your application's requirements, but remember Knuth's maxim: "premature optimisation is the root of all evil".