开发者

Need help with database/query design for gamer's rating calculation

开发者 https://www.devze.com 2023-04-05 02:42 出处:网络
I have a game, where for each round player gets a prize and rating by total prize is calculated. I use such schema:

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 tot开发者_高级运维al_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!


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号