游戏hiscores的mysql查询真的很慢

时间:2014-07-23 09:17:45

标签: mysql

DB就像4000行,需要约45秒。索引是游戏。每个用户可以存在多个和重复的核心条目(不是重复的行,存在主键ID)。

SELECT id, username, score, comment, playtime, stage, difficulty, platform
FROM hiscores WHERE game='game_1' AND score
IN (SELECT MAX(score) FROM hiscores WHERE game='game_1' GROUP BY username,difficulty,platform)
GROUP BY username,difficulty,platform ORDER BY score DESC;

这是4000 * 4000的选择操作,对吗?难怪它很慢。我该如何解决这个问题?有什么建议吗?

3 个答案:

答案 0 :(得分:0)

从这个位开始......

SELECT username
     , difficulty
     , platform
     , MAX(score) max_score 
  FROM hiscores 
 WHERE game = 'game_1' 
 GROUP 
    BY username
     , difficulty
     , platform

......现在,知道下一步该怎么做?

答案 1 :(得分:0)

摆脱in

SELECT id, username, score, comment, playtime, stage, difficulty, platform
FROM hiscores h WHERE game='game_1'
  AND id = 
    (SELECT id FROM hiscores WHERE game='game_1' and (username,difficulty,platform)=(h.username,h.difficulty,h.platform) order by score desc limit 1)
GROUP BY username,difficulty,platform ORDER BY score DESC;

答案 2 :(得分:0)

我不确定,但我认为这对你有用。试试这个

SELECT id, username, score, comment, playtime, stage, difficulty, platform
FROM hiscores h1 JOIN 
(
   SELECT MAX(score) FROM hiscores WHERE game='game_1' GROUP BY username,difficulty,platform
)SUB_TABLE ON h1.score = SUB_TABLE.score
WHERE game='game_1' 
ORDER BY score DESC;