MySQL-SELECT + JOIN + ORDER BY性能

问题描述:

我有两个表,我需要从两个表中选择一些数据

I'm having two tables, I need to select some data joined from both of them

SELECT f.* 
FROM file_data f
JOIN subscriptions s ON f.uid = s.elementid
WHERE s.uid = 119762 AND f.private=0  
ORDER BY f.date DESC

现在,即使对于一小部分数据,查询也要花费一秒钟的时间. 这是由于ORDER BY f.date导致订阅"上使用了文件排序"和临时"(删除该条件会导致时间降至0.01秒以下)

Now, even for a small set of data the query takes over a second. This is due to 'filesort' and 'temporary' used on "subscriptions", caused by ORDER BY f.date (removing that condition causes the time to drop under 0.01s)

有人可以告诉我如何加快查询速度吗?

Can anyone tell me how to speed up the query?

这是解释的结果

id  select_type    table    type   possible_keys    key            key_len  ref           rows  Extra
1    SIMPLE         s        ref    uid_elementid    uid_elementid  4        const         171   Using index; Using temporary; Using filesort
1    SIMPLE         f        ref    uid_uname        uid_uname      5        s.elementid   22    Using where

您应该在f.date上放置一个索引

You should put an index on f.date