有效地在数据库中存储高分

时间:2015-09-15 07:30:18

标签: mysql sql elasticsearch architecture

如何在数据库中高效存储高分?

我有一个表player_score,有两列player_id(主键),得分(整数值)。

现在我想要一个玩家在高分榜中的位置,我创建了以下查询

SELECT player_score.player_id,
   (SELECT COUNT(*) FROM player_score ps WHERE ps.score >= player_score.score) AS position   
FROM player_score      
WHERE player_score.player_id = 19

我认为你有任何其他想法这是非常低效的吗?也可以考虑使用elasticsearch或任何其他技术来有效地存储这些数据。

1 个答案:

答案 0 :(得分:0)

您可以使用以下查询来获取用户在高分列表中的确切位置:

SELECT COUNT(*)+1 AS pos
FROM (SELECT *
      FROM player_score
      ORDER BY score DESC) AS ordered_scores, player_score
WHERE player_score.player_id=3 AND player_score.score < ordered_scores.score;