【问题标题】:MongoDB: What index should I use?MongoDB:我应该使用什么索引?
【发布时间】:2016-08-24 02:29:54
【问题描述】:

我得到了一个高分 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});

你会推荐什么索引?

在不同的表中保持升序类别和降序类别会带来更好的性能吗?

注意:我不想每个类别有一个表。

【问题讨论】:

  • 嘿@RainingChain,如果您认为这有帮助并令人信服,您能否将答案标记为已接受。

标签: mongodb indexing


【解决方案1】:

我用一些示例数据集在本地进行了一些测试,我认为最好的选择是在“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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2011-01-16
    • 2011-04-25
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    相关资源
    最近更新 更多