我想优化我的mysql查询,以避免'使用临时的,使用filesort'。我可以使用一些帮助。第一;这里是解释使用临时的,在MySQL中使用filesort一个坏主意?
这里是查询
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 |
有涉及2个表。 ProfileFriends(pf)和会员(m)。此查询只是试图找到这个特定会员ID的'最近'24个朋友。最近意味着按LastLogin日期排序。
谢谢
+1是的,对于160行MySQL甚至可以将临时表保存在内存中,而无需将其写入磁盘。 “Filesort”有点误导,它只意味着它没有索引的好处而被排序。 – 2009-09-05 18:21:37