我有一个我需要优化的MYSQL查询,它在我的测试环境中运行良好但是对于更大的数据库它是缓慢的方式
我正在使用PHP activerecord作为我的db-handler。
Users:
userId | userName | gameId
-------+----------+--------
1 | John | 1
2 | Sally | 1
3 | Mike | 2
4 | Lex | 1
Scores:
id | userId | gameId | score | added |
---+--------+---------+-------+-----------+
1 | 2 | 1 | 300 | time
2 | 2 | 1 | 325 |
3 | 1 | 1 | 200 |
4 | 1 | 1 | 400 |
5 | 4 | 1 | 100 |
extra_fields:
id | score_id | fieldname | fieldvalue |
---+----------+-----------+------------+
1 | 1 | level | 5 |
2 | 1 | image | icon.jpg |
3 | 2 | level | 7 |
4 | 2 | image | smilie.jpg |
5 | 3 | level | 5 |
6 | 3 | image | hello.jpg |
7 | 4 | level | 1 |
8 | 4 | image | fun.png |
9 | 5 | level | 3 |
10 | 5 | image | mfw.png |
现在问题是,我想从每个用户中选择最高分,然后获取相关的额外值。 所以在上面的示例数据库中,结果将如下所示:
游戏1中的用户请求(其中gameId = 1):
1 -> username: John ; Score: 400 ; level : 1 ; image : fun.png
2 -> username: Sally ; Score: 325 ; level : 7 ; image : smilie.jpg
3 -> username: Lex ; Score: 100 ; level 3 ; image : mfw.png
现在这就是我所拥有的:
"SELECT * FROM leaderboard_users a JOIN (
SELECT d1.*
FROM leaderboard_scores d1
LEFT OUTER JOIN leaderboard_scores d2
ON (d1.userId = d2.userId AND d1.score < d2.score AND d1.added < d2.added)
WHERE d2.id is null AND d1.gameId = " . intval($this->gameId) . "
AND DATEDIFF(NOW() , d1.added) <= " . intval($this->calcPeriod) . "
)b
ON b.userId = a.userId
GROUP BY b.userId
ORDER BY b.score DESC
LIMIT " . $this->limitWithOffset . " , " . $this->limit;
从中我得到用户名,得分和得分_然后我再做一个查询来查找所有额外的字段(如果有的话)
$extraValues = \extraFields::find('all', array(
'conditions' => array(
'score_id = ?',
$j->id)
));
我猜是什么需要时间是JOIN语句,因为我加入了得分表(30k +)中的所有记录,这似乎很疯狂。
有谁知道我如何优化这个? 或者我的表格布局是否全部错误并需要更改?
编辑(解释RaviH)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1554 Using temporary; Using filesort
1 PRIMARY a eq_ref PRIMARY PRIMARY 4 b.userId 1
2 DERIVED d1 ALL NULL NULL NULL NULL 41644 Using where
2 DERIVED d2 ref leaderboard_scores_FI_1 leaderboard_scores_FI_1 4 lechuck_se.d1.userId 12 Using where; Not exists
答案 0 :(得分:0)
您的查询正在从leaderboard_users
和leaderboard_scores
表中获取所有行,从而导致用户表和分数自联接结果之间的交叉连接。这些交叉连接的临时结果是巨大的。因此它已经放慢了速度。随着向用户和分数表添加更多行数,它将变慢。
尝试以下查询:
"SELECT * FROM leaderboard_users u JOIN (
SELECT userId, MAX(score) FROM leaderboard_scores
WHERE gameId=" . intval($this->gameId) . " AND DATEDIFF(NOW(), added) <= " . intval($this->calcPeriod) . " GROUP BY userId) s
ON u.userId = s.userId"
如果可以以某种方式避免动态计算DATEDIFF
,则可以进一步提高查询速度。我无法为此提供通用解决方案,因为它取决于您的要求和数据库设计。
希望这有帮助!