0

我只有一个问题:假设我们有一个表 (activity_feed) 有 1.000.000 行,一个表 (activity_feed_per_user) 与请求提要的用户和他将看到的活动之间的关系以及表中的一些统计信息全部由应用程序获取的活动。

如果我必须按等级对结果进行排序(甚至取决于时间,所以每秒都是变量),这个查询好用吗?或者根本没有?

EXPLAIN EXTENDED SELECT feed.activity_id, feed.body, counter.comments, counter.likes, user.username,
(0.25 * (
        (1 / TIMESTAMPDIFF(MINUTE,feed.datetime,now()) ) +
        ( 1 - (1 / (((comments + 1)* 1) + ((likes + 1) * 0.5))) ) +
        activity_type.peso +
        user_affinity.affinity
    )) as ranking
FROM activity_feed_per_user as feed_user
INNER JOIN activity_feed as feed ON feed_user.activity_id = feed.activity_id
INNER JOIN activity_type ON feed.activity_type = activity_type.activity_type_id
INNER JOIN activity_social_counter as counter ON feed_user.activity_id = counter.activity_id
INNER JOIN user_info as user ON user.user_id = feed.user_id
INNER JOIN user_affinity ON feed.user_id = user_affinity.user_related AND user_affinity.user_id = '1'
WHERE feed_user.user_id = '1' 
ORDER BY ranking DESC

这是解释

1 | SIMPLE | user_affinity | ref | PRIMARY | PRIMARY | 4 | const | 2 | 100.00 | Using temporary; Using filesor
1 | SIMPLE | user | eq_ref | PRIMARY | PRIMARY | 4 |db.user_affinity.user_related | 1 | 100.00
1 | SIMPLE | feed_user | ref | PRIMARY | PRIMARY | 4 | const | 9 | 100.00 | Using index
1 | SIMPLE | feed | eq_refvPRIMARY,activity_type,user_idvPRIMARY | 4 | db.feed_user.activity_id | 1 | 100.00 | Using where
1 | SIMPLE | activity_type | eq_ref | PRIMARY | PRIMARY | 1 | db.feed.activity_type | 1 | 100.00
1 | SIMPLE | counter | eq_ref | PRIMARY | PRIMARY | 4 | db.feed_user.activity_id | 1 | 100.00
4

2 回答 2

1

ORDER BY在没有索引的值上,必须查看查询生成的每一行,因此您提出的建议可能会损害返回大量结果集的查询的性能。

于 2013-03-29T12:47:56.237 回答
0

The ORDER BY in your query will force a "Using filesort" operation, and that can be an expensive operation on large result sets. (MySQL can sometimes satisfy an ORDER BY using an index, avoiding a "Using filesort" operation; but in your case, the expression ("math") will need to evaluated for all rows in the result set.

If it's returning the resultset you need, then your query looks good.

于 2013-03-29T12:55:31.063 回答