简体   繁体   English

如何使这个个人最佳的MySQL高分查询更有效?

[英]How do I make this personal best highscore MySQL query more efficient?

I have a database for game leaderboard highscores holding currently about 30.000 entries, and the below query looks for the personal best highscore for a certain game and player, but it takes about 60 seconds to execute. 我有一个游戏排行榜高分数据库,目前拥有约30.000个条目,下面的查询查找特定游戏和玩家的个人最佳高分,但执行该过程大约需要60秒 I'm thinking there should be a much more efficient way to do this, maybe using a composite index, but which one would that be? 我在想应该有一种更有效的方法来执行此操作,也许使用复合索引,但是那是哪种呢?

SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h1
WHERE score = (
  SELECT MAX( score ) 
  FROM highscore h2
  WHERE name = h1.name && gamename = "asteroids" && name = "bob"
)

I currently have indexes for: 我目前有以下指标:

id
score
name
name-gamename (composite)

Any help is greatly appreciated! 任何帮助是极大的赞赏!

I would write the query using standard SQL syntax: 我将使用标准SQL语法编写查询:

SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h1
WHERE score = (SELECT MAX( score ) 
               FROM highscore h2
               WHERE h2.name = h1.name AND h2.gamename = 'asteroids' AND h2.name = 'bob'
              );

For this purpose, you want a composite index: highscore(name, gamename, score) . 为此,您需要一个综合索引: highscore(name, gamename, score)

If you are only looking for one row (even when there are ties), then this might be a wee bit faster: 如果你只是在寻找一个行(即使有关系),那么这可能是凌晨快一点:

SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h 
WHERE h.gamename = 'asteroids' AND h.name = 'bob'
ORDER BY score DESC
LIMIT 1;

The appropriate index is highscore(name, gamename, score) . 适当的索引是highscore(name, gamename, score)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM