Ranking with millions of entries

后端 未结 6 999
有刺的猬
有刺的猬 2020-12-12 14:31

I\'m working on a server for an online game which should be able to handle millions of players. Now the game needs leaderboards and wants to be able to show a players curren

6条回答
  •  失恋的感觉
    2020-12-12 15:03

    I can think of two ways to approach this problem:

    First approach: Update in batches:

    • Sort the scores, obtain the ranking
    • Divide the players by rank into batches like player0-player999, player1000-player1999, etc
    • For each batch, delete entries in the existing table that conflict with the new data. This means deleting existing entries belonging to players in the current batch or who currently rank in the range of ranks being updated in the current batch. Then you load the ranking data for the batch into the database, and jump to the next batch after say 0.1s.

    Second approach: New table

    • Create (or truncate) a new table just like your existing ranking table.
    • compute the new ranking and insert your data
    • Swap the tables (after preferably locking them). This should take less than a second.

提交回复
热议问题