what does Using filesort mean in MySQL?
Asked Answered
P

1

10

I want to know what does Using filesort mean in MySQL?

explain select * from user order by user_id;

It turn up when i use 'order by'.

1,SIMPLE,orderitems,,ALL,,,,,1,100,Using filesort

The 'Using filesort' is in extra. Thanks for everyone!

Pronuba answered 27/1, 2021 at 3:8 Comment(2)
It means that the data gets sorted.Granuloma
This means that there is no index which provides the rows in needed order, and the rowset will be sorted. Create any index where user_id is a prefix, and look for execution plan change.Delegate
V
5

This is actually in the MySQL documentation:

https://dev.mysql.com/doc/refman/8.0/en/explain-output.html#explain-extra-information

Using filesort (JSON property: using_filesort)

MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause. The keys then are sorted and the rows are retrieved in sorted order. See Section 8.2.1.16, “ORDER BY Optimization”.

When you don't see "using filesort," it means the order you requested for the query result matches the natural order in which the data was read, so there was no extra work needed to sort the result.

When querying InnoDB tables (the default storage engine), MySQL queries always read the rows by the order of the index reported by EXPLAIN. If the order it read rows matches the order the query requested, then the query simply returns rows as it reads them. If the order the rows are read is different from the order you requested, then the query must sort the results before returning them.

Vagrant answered 27/1, 2021 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.