简体   繁体   中英

Using seeded random sequence instead of 100M row relations table

We are hosting the backend of a social game for iPhone on a Ruby on Rails/postgres server.

Every game has 54 questions connected via a relation table in the database. Since we currently have about 2 Million ongoing games, the relation table contains about a 100M rows. This eats a lot of memory. We are now considering constructing an algorithm which generates 54 question ids using a pseudo-random sequence seeded by the game_id. The idea is to produce a seemingly random collection of question, without saving the explicit game-question-relations to the database. This way we can move load from the database to the application server. Is this a good idea?

Pseudo code:

r = Random.new(game_id)
q1_id = r.rand(n_questions)
q2_id = r.rand(n_questions)
... 

Yeah, all you need is a deterministic function that takes the game ID and produces a sequence. Something as simple as adding a salt to the gameId and hashing the result should do just fine, then it's just a matter of mapping the resultant value to the question IDs. (Take a look at combinatorics for that kind of thing.)

If you're then dealing with something that looks like a list of question identifiers keyed by game identifier, and you need persistence support for the answers, there are better options than an RDBMS. Take a look at Redis, for starters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM