我有一个包含文档的高分mongodb表,例如
{username:"Bob",score:10,category:"mostLikes"}
{username:"John",score:32,category:"mostLikes"}
{username:"Bob",score:2,category:"leastDeaths"}
目标是获取特定类别的前100个(已排序)。
重要提示:某些高分类别正在升序(例如,越低越好:),leastDeaths
而其他降序类别(例如,越好越好:)mostLikes
。这意味着根据类别,我想要100个最高分或100个最低分。
我的应用程序中有两个主要查询:
db.highscore.find({category:category}, {}).limit(100).sort({ score: 1 /*or -1*/ });
db.highscore.find({username:username});
您会推荐什么指数?
将升序类别和降序类别保留在不同的表中会导致更好的性能吗?
注意:我不想每个类别都有一张表。
我用一些样本数据集在本地进行了测试,我认为最好的选择是在“ category_1_score_1_username_1”上创建索引
在以下字段上创建索引将为您提供涵盖的查询,因此可以直接从索引返回文档。
在我的分析下面找到
> db.usr.find();
{ "_id" : ObjectId("57bd20630744bd376277a795"), "username" : "Bob", "score" : 10, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a796"), "username" : "John", "score" : 32, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a797"), "username" : "Bob1", "score" : 2, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a798"), "username" : "John2", "score" : 132, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a799"), "username" : "Bob3", "score" : 20, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79a"), "username" : "John4", "score" : 132, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79b"), "username" : "Bob5", "score" : 22, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79c"), "username" : "John6", "score" : 322, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79d"), "username" : "Bob7", "score" : 232, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79e"), "username" : "John8", "score" : 3112, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79f"), "username" : "Bob4", "score" : 222, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a7a0"), "username" : "John22", "score" : 3210, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a7a1"), "username" : "Bob33", "score" : 2111, "category" : "leastDeaths" }
索引:
> db.usr.getIndexes();
{
"v" : 1,
"key" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"name" : "category_1_score_1_username_1",
"ns" : "test.usr"
}
]
>
现在,您可以稍微修改查询,使其返回覆盖的查询。
db.usr.find({"category":"mostLikes"},{"_id":0,"score":-1,"category":1,"username":1}).sort({"score":1}).explain("executionStats");
Output of Execution Stats:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.usr",
"indexFilterSet" : false,
"parsedQuery" : {
"category" : {
"$eq" : "mostLikes"
}
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"_id" : 0,
"score" : -1,
"category" : 1,
"username" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"indexName" : "category_1_score_1_username_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"category" : [
"[\"mostLikes\", \"mostLikes\"]"
],
"score" : [
"[MinKey, MaxKey]"
],
"username" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 7,
"executionTimeMillis" : 0,
"totalKeysExamined" : 7,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 7,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 7,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"_id" : 0,
"score" : -1,
"category" : 1,
"username" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 7,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 7,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"indexName" : "category_1_score_1_username_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"category" : [
"[\"mostLikes\", \"mostLikes\"]"
],
"score" : [
"[MinKey, MaxKey]"
],
"username" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 7,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "L4156409",
"port" : 27017,
"version" : "3.2.5",
"gitVersion" : "34e65e5383f7ea1726332cb175b73077ec4a1b02"
},
"ok" : 1
}
>
因此,您可以看到输出,而直接从索引中获取记录时,扫描的文档数为0。因此,选择此索引将是第一个查询的最佳选择。
对于第二个查询,在用户名字段上创建索引应该很简单,并且可以为您解决第二个查询。
HTH。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句