需要帮助进行玩家评分计算的数据库/查询设计

发布于 2024-12-04 18:47:47 字数 567 浏览 1 评论 0原文

我有一个游戏,每轮玩家都会获得奖励,并按总奖励计算评级。 我使用这样的模式:

1)表game_result(user_id,prize,game_date)用于单独存储游戏结果(我需要它进行统计)

2)表 rating(user_id,total_prize,total_games)用于存储评级数据并且total_prize、total_games值在每场游戏结束时更新

3)我需要快速找到用户的总奖金和评级位置(以在游戏期间显示)。现在我使用这种查询的一种

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, user_id, total_prize, total_games FROM rating order by total_prize where user_id = ? 

,但它为每个查询使用评级表,而且速度太慢。问题是,在每场比赛之后,单个玩家只更改一行(使用total_prize、total_games),但要找到他的新订单位置,我需要诉诸整个表,并且性能不可行。

是否可以重新组织表以提高此操作的性能?

谢谢!

I have a game, where for each round player gets a prize and rating by total prize is calculated.
I use such schema:

1) table game_result (user_id, prize, game_date) is used to store results of the games separately (I need it for my statistics)

2) table rating (user_id, total_prize, total_games) is used to store rating data and total_prize, total_games values are updated at the end of each game

3) I need to find user's total prize and position in rating fast (to display it during the game). Now I use a kind of this query

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, user_id, total_prize, total_games FROM rating order by total_prize where user_id = ? 

but it resorts rating table for each query and is too slow. The issue is that after each game only one row is changed (with total_prize, total_games) of a single player, but to find his new order place I need to resort the whole table, and performance is not feasible.

Is it possible to reorganize tables to improve performance of this operation?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-12-11 18:47:47
SELECT COUNT(*)
FROM rating
WHERE total_prize <= (SELECT total_prize FROM rating WHERE user_id = ?)

(或者进行单独的查询并获取用户的total_prize,加上您需要的其他列,然后对其进行计数。)

当然,请确保在total_prize上有一个索引。

(您可能需要 >= 来代替,具体取决于您所说的排名的含义。)

SELECT COUNT(*)
FROM rating
WHERE total_prize <= (SELECT total_prize FROM rating WHERE user_id = ?)

(Or do a separate query and get the total_prize for the user, plus the other columns you need, then count it.)

Make sure to have an index on total_prize of course.

(You may need >= instead, depending on what you mean by rank.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文