简体   繁体   English

使用临时,使用Filesort在mysql中是个坏主意?

[英]Using temporary, using filesort a bad idea in mysql?

I am trying to optimize my mysql queries to avoid 'using temporary, using filesort'. 我试图优化我的mysql查询,以避免“使用临时,使用文件排序”。 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. 有2个表。 ProfileFriends (pf), and Members (m). ProfileFriends(pf)和Members(m)。 This query is just trying to find the 'recent' 24 friends for this particular member id. 该查询正试图为此特定会员ID查找“最近”的24个朋友。 Recent means sort by LastLogin date. 最近的均值按LastLogin日期排序。

Thanks 谢谢

It is a problem? 有问题吗? Yeah. 是的

Is it a problem when you're dealing with 160 rows? 处理160行时是否有问题? 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. 如果我们谈论的是160,000行而不是160行,则可能有理由考虑进一步优化。

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. 如果此查询花费的时间不超过一秒的几分之一,那么即使进行优化也可能不值得。

I think you should be able to avoid the temporary/filesort with an index on members (id,lastLogin) —in that order— but for that type of queries it's overkill and judging by your EXPLAIN it seems you've already tried that? 我认为您应该能够避免对members (id,lastLogin)进行索引的临时/文件members (id,lastLogin) 顺序 members (id,lastLogin) 但是对于这种类型的查询来说,这是过分的,并且通过EXPLAIN判断,看来您已经尝试过了?

You might complement it with a PRIMARY/UNIQUE KEY on profile_friends (member_id,friend_id) and see how it works. 您可以在profile_friends (member_id,friend_id)上使用PRIMARY / UNIQUE KEY对其进行补充profile_friends (member_id,friend_id)并查看其工作原理。

In last resort, if that query is executed so often and with so many record that you must have the fastest SELECT possible, you could denormalize your table and add a copy of your members.lastLogin column to profile_friends with an index on (member_id,lastLogin) . 在最后的手段,如果执行该查询经常,有这么多的记录,你必须拥有最快的选择可能,你可以进行非规范化你的表和你的副本添加members.lastLoginprofile_friends与指数(member_id,lastLogin) With that, you'd have no join, no filesort, no nothing. 这样一来,您将无需加入,没有文件排序,什么也没有。 On the other hand, you'd have big UPDATE queries everytime anyone with many friends logs in. Again, this seems completely overkill for the kind of numbers you're talking about. 另一方面,每次有很多朋友的人登录时,您都会有较大的UPDATE查询。同样,对于您所谈论的数字来说,这似乎完全过头了。

I almost forgot to address the original question: 我几乎忘了回答原始问题:

Using temporary, using filesort a bad idea in mysql? 使用临时,使用Filesort在mysql中是个坏主意?

No it's not. 不,这不对。 If you can optimize it away easily then you should always seek "filesort-free" queries, but otherwise unless they pose a real performance problem you shouldn't worry about it. 如果可以轻松地对其进行优化,则应始终寻求“无文件排序”查询,否则,除非它们带来了真正的性能问题,否则您不必担心。 Filesort is part of the normal execution of a query. Filesort是查询正常执行的一部分。

That's the most efficient way to write that query. 这是编写该查询的最有效方法。

Make sure that pf.friend_id , pf.member_id , and m.id have indices on them. 确保pf.friend_idpf.member_idm.id都有索引。 Then it will use the indices to join the tables and filter the results. 然后它将使用索引来联接表并过滤结果。

That sort is going to come up, anyway, because of your order by . 无论如何,由于您的order by这种排序即将到来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM