Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:我应该使用什么索引?_Mongodb_Indexing - Fatal编程技术网

MongoDB:我应该使用什么索引?

MongoDB:我应该使用什么索引?,mongodb,indexing,Mongodb,Indexing,我得到了一个包含以下文档的高分mongodb表 {用户名:“Bob”,分数:10,类别:“mostLikes”} {用户名:“John”,分数:32,类别:“mostLikes”} {用户名:“Bob”,分数:2,类别:“leastdeath”} 目标是获取特定类别的前100名(已排序) 重要提示:某些高分类别在上升(例如,越低越好:leastdeath),而其他类别在下降(例如,越大越好:mostLikes)。这意味着,根据类别,我想要100个最高分数或100个最低分数 我的应用程序中有两个主

我得到了一个包含以下文档的高分mongodb表

{用户名:“Bob”,分数:10,类别:“mostLikes”}
{用户名:“John”,分数:32,类别:“mostLikes”}
{用户名:“Bob”,分数:2,类别:“leastdeath”}

目标是获取特定类别的前100名(已排序)

重要提示:某些高分类别在上升(例如,越低越好:
leastdeath
),而其他类别在下降(例如,越大越好:
mostLikes
)。这意味着,根据类别,我想要100个最高分数或100个最低分数

我的应用程序中有两个主要查询:

db.highscore.find({category:category},{}).limit(100).sort({score:1/*或-1*/})

db.highscore.find({username: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。因此,选择此索引将是第一次查询的最佳选择

对于第二个查询,在username字段上创建索引应该很简单,并且应该可以为您解决第二个查询


HTH.

hey@RainingChain如果您认为这有帮助且有说服力,请您将答案标记为已接受。