SQL Server和Oracle都有DENSE_RANK函数。除了其他方面,这允许您获得记录的全局排名,同时仅返回这些记录的子集,例如:
SELECT DENSE_RANK() OVER(ORDER BY SomeField DESC) SomeRank
在MongoDB中执行相同操作的最佳方法是什么?
答案 0 :(得分:3)
经过一些实验,我发现可以建立一个基于MapReduce的排名函数,假设结果集可以适合最大文档大小。
例如,假设我有一个这样的集合:
{ player: "joe", points: 1000, foo: 10, bar: 20, bang: "some text" }
{ player: "susan", points: 2000, foo: 10, bar: 20, bang: "some text" }
{ player: "joe", points: 1500, foo: 10, bar: 20, bang: "some text" }
{ player: "ben", points: 500, foo: 10, bar: 20, bang: "some text" }
...
我可以像这样执行DENSE_RANK的粗略等效:
var m = function() {
++g_counter;
if ((this.player == "joe") && (g_scores.length != g_fake_limit)) {
g_scores.push({
player: this.player,
points: this.points,
foo: this.foo,
bar: this.bar,
bang: this.bang,
rank: g_counter
});
}
if (g_counter == g_final)
{
emit(this._id, g_counter);
}
}}
var r = function (k, v) { }
var f = function(k, v) { return g_scores; }
var test_mapreduce = function (limit) {
var total_scores = db.scores.count();
return db.scores.mapReduce(m, r, {
out: { inline: 1 },
sort: { points: -1 },
finalize: f,
limit: total_scores,
verbose: true,
scope: {
g_counter: 0,
g_final: total_scores,
g_fake_limit: limit,
g_scores:[]
}
}).results[0].value;
}
为了比较,这里是其他地方提到的“天真”方法:
var test_naive = function(limit) {
var cursor = db.scores.find({player: "joe"}).limit(limit).sort({points: -1});
var scores = [];
cursor.forEach(function(score) {
score.rank = db.scores.count({points: {"$gt": score.points}}) + 1;
scores.push(score);
});
return scores;
}
我使用以下代码在MongoDB 1.8.2的单个实例上对这两种方法进行了基准测试:
var rand = function(max) {
return Math.floor(Math.random() * max);
}
var create_score = function() {
var names = ["joe", "ben", "susan", "kevin", "lucy"]
return { player: names[rand(names.length)], points: rand(1000000), foo: 10, bar: 20, bang: "some kind of example text"};
}
var init_collection = function(total_records) {
db.scores.drop();
for (var i = 0; i != total_records; ++i) {
db.scores.insert(create_score());
}
db.scores.createIndex({points: -1})
}
var benchmark = function(test, count, limit) {
init_collection(count);
var durations = [];
for (var i = 0; i != 5; ++i) {
var start = new Date;
result = test(limit)
var stop = new Date;
durations.push(stop - start);
}
db.scores.drop();
return durations;
}
虽然MapReduce比我预期的要快,但是天真的方法将它从水中吹出来以获得更大的收集尺寸,特别是在缓存预热后:
> benchmark(test_naive, 1000, 50);
[ 22, 16, 17, 16, 17 ]
> benchmark(test_mapreduce, 1000, 50);
[ 16, 15, 14, 11, 14 ]
>
> benchmark(test_naive, 10000, 50);
[ 56, 16, 17, 16, 17 ]
> benchmark(test_mapreduce, 10000, 50);
[ 154, 109, 116, 109, 109 ]
>
> benchmark(test_naive, 100000, 50);
[ 492, 15, 18, 17, 16 ]
> benchmark(test_mapreduce, 100000, 50);
[ 1595, 1071, 1099, 1108, 1070 ]
>
> benchmark(test_naive, 1000000, 50);
[ 6600, 16, 15, 16, 24 ]
> benchmark(test_mapreduce, 1000000, 50);
[ 17405, 10725, 10768, 10779, 11113 ]
所以现在看来,这种天真的方法似乎还有很长的路要走,虽然我很想知道今年晚些时候故事是否会发生变化,因为MongoDB团队会继续提高MapReduce的性能。
答案 1 :(得分:0)
如果您的分数字段直接在您的文档中,则密集等级只是按特定排序顺序排列的文档的索引。
假设您有一系列游戏分数,例如:
{user: "dcrosta", score: 10}
{user: "someone", score: 18}
{user: "another", score: 5}
...
然后(假设你有一个得分指数)来获得排名你可以查询按分数排序(在pymongo语法中显示):
scores = db.scores.find().sort('score', pymongo.DESCENDING)
for rank, record in enumerate(scores, start=1):
print rank, record['user']
# prints
1 someone
2 dcrosta
3 another
如果您不熟悉Python,enumerate
函数会创建一个返回(index
,element
)对的迭代器。
编辑:我认为你想要一个排名表 - 如果你正在寻找特定用户的排名,理查德的答案,或类似的东西,就是你想要的。