【发布时间】:2015-03-09 07:00:55
【问题描述】:
我希望加快此查询的速度。我目前在
上有一个索引users_score.appID
app_names.name
SELECT users_scores.username, users_scores.avatar, users_scores.score
FROM users_scores
RIGHT JOIN app_names ON app_names.id = users_scores.appID
WHERE app_names.name = "testapp1"
ORDER BY users_scores.score DESC
LIMIT 0 , 30
【问题讨论】:
-
基本上出现在 JOIN 或 WHERE 或 ORDER BY 子句中的每一列。
-
在您的问题中包含
EXPLAIN SELECT users_scores.username, users_scores.avatar, users_scores.score FROM users_scores RIGHT JOIN app_names ON app_names.id = users_scores.appID WHERE app_names.name = "testapp1" ORDER BY users_scores.score DESC的结果,您最终将学习如何为 any 查询解决这个问题... -
除了一般查询优化之外,我要注意的一件事是,当内部连接似乎足够时,表 app_names 正在被正确连接 - 不仅如此,连接根本不需要如果您的应用程序知道它需要为其查找分数的 app_names.id - 那么您只需要 users_scores.appID 上的索引
-
@PaulDixon 我只是根据分析速度测试使用正确的连接。我使用的是内部联接,但查询的运行时间是右联接的两倍。此外,应用程序不会知道它需要使用什么 ID,它会通过使用应用程序名称查看该表来找到它。
-
好的,我现在将其设置为内部连接。不知道配置文件的内容是什么,但昨晚它的运行速度几乎是内部与右侧的两倍。今天很火爆。
标签: mysql performance indexing