Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using temporary, using filesort a bad idea in mysql?

Tags:

mysql

I am trying to optimize my mysql queries to avoid 'using temporary, using filesort'. I could use some help. First; here is the explain

Here is the Query

select pf.*,m.login,m.avatar 
from profile_friends pf, members m  
where pf.friend_id = m.id and pf.member_id = 16586 
order by m.lastLogin desc 
limit 0,24;


mysql> EXPLAIN select pf.*,m.login,m.avatar from profile_friends pf, members m  where pf.friend_id = m.id and pf.member_id = 16586 order by m.lastLogin desc limit 0,24;
+----+-------------+-------+--------+-----------------------------------------------------+-----------------+---------+--------------------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                                       | key             | key_len | ref                      | rows | Extra                                        |
+----+-------------+-------+--------+-----------------------------------------------------+-----------------+---------+--------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | pf    | ref    | member_id_index,friend_id_index                     | member_id_index |       4 | const                    |  160 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | m     | eq_ref | PRIMARY,member_id_privacy_index,id_last_login_index | PRIMARY         |       4 | mydb.pf.friend_id        |    1 | Using where                                  |

There are 2 tables involved. ProfileFriends (pf), and Members (m). This query is just trying to find the 'recent' 24 friends for this particular member id. Recent means sort by LastLogin date.

Thanks

like image 835
shergill Avatar asked Sep 05 '09 02:09

shergill


People also ask

Is using filesort bad MySQL?

Filesort itself is not a bad thing... the number of rows that need to be processed is. Show activity on this post. Your WHERE condition uses price , status , approved to select, and then date_updated is used to sort.

What does using filesort mean?

It means that the data gets sorted. – Gordon Linoff. Jan 27, 2021 at 3:10. This means that there is no index which provides the rows in needed order, and the rowset will be sorted.

What is using Filesort in MySQL?

Use of filesort to Satisfy ORDER BY If an index cannot be used to satisfy an ORDER BY clause, MySQL performs a filesort operation that reads table rows and sorts them. A filesort constitutes an extra sorting phase in query execution. To obtain memory for filesort operations, as of MySQL 8.0.


1 Answers

It is a problem? Yeah.

Is it a problem when you're dealing with 160 rows? Nope.

"Filesort" is a method, not the actual creation of a file and sorting it. If we were talking about 160,000 rows instead of 160 rows, then there'd probably be reason to consider further optimizations.

Edit: Also, you omitted the actual query running time. You're hitting indexes and only working with a handful of rows. If this query is taking more than a fraction of a fraction of a second, it's probably not worth even looking at for optimization.

like image 164
Charles Avatar answered Oct 29 '22 14:10

Charles