Milan Babuškov
Milan Babuškov

Reputation: 61158

How to convert "top 10" list from SQL to NoSQL

I have an online game currently using MySQL. I have a Player table looking like this:

create table player (
    id integer primary key,
    name varchar(50),
    score integer
);

I have an index on "score" column and display the rankings like this:

select id, name, score from player order by score desc limit 100

I'd like to migrate my system to Redis (or, if some other NoSQL is more applicable to this kind of problem, please tell). So I wonder what is the way to display this kind of rankings table efficiently?

AFAICT, this could be a Map/Reduce job? I know next to nothing about Map/Reduce although I read some docs I still don't quite understand as I haven't been able to find any real-life examples.

Can someone please give me a rought example how to do the above query in Redis?

Upvotes: 2

Views: 499

Answers (2)

atomAltera
atomAltera

Reputation: 1781

In redis you can use Sorted sets ( http://redis.io/commands#sorted_set ) When you have scored items in sorted set you can get top N by invoke ZRANGE players 0 N

Upvotes: 3

Michael Manoochehri
Michael Manoochehri

Reputation: 7877

Good question - In MongoDB you would have to use the group() function to return this type of query:

select id, name, score from player order by score desc limit 100

Might look something like this:

db.player.group(
           {key: { id:true, name:true },
            reduce: function(obj,prev) { if(prev.cmax<obj.score) prev.cmax = obj.score; },
            initial: { cmax: 0 } // some initial value
            });

Using a MapReduce based approach is probably best, see:

Upvotes: 1

Related Questions