what is the best practice leaderboard with php, mysql, memcached?

烂漫一生 提交于 2020-01-16 19:28:27

问题


Recently I have developed mobile game (LAMP + memcached).

The game has player's score table. The table has member_id, name, score column.

I want to show leaderboard (global rank) to our user.

Just query SELECT * FROM score ORDER BY score DESC, and show the resultset.

But I don't think it is good way. If users reach 20 million, this query seem to be terrible. What is the best practice in this case? Could you give me some advice?

And how can I check specific user's rank? I want to show each user rank where they are.


回答1:


Well, you don't want to display millions of users on the leader board do you?

SELECT * FROM score ORDER BY score DESC LIMIT 10

Would select top 10

Further, if you want to display a users rank on for instance a profile page this should do:

SELECT COUNT(*) + 1 AS rank FROM score WHERE score > (SELECT score FROM score WHERE member_id = :member_id);

You simply add the current users member_id to the last query and it will count all rows ahead of him and add 1 to that.



来源:https://stackoverflow.com/questions/21112852/what-is-the-best-practice-leaderboard-with-php-mysql-memcached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!