mongodb索引是否需要完全匹配查询才能高效?

时间:2016-08-10 20:37:47

标签: mongodb

对于具有属性a,b,c,d和e

的文档

如果我使用a和b运行查询,并且我有一个覆盖a,b和c的索引,我是否需要第二个索引只包含a和b,或者当前索引是否足够?

2 个答案:

答案 0 :(得分:1)

不,你不需要额外的索引,那是因为你按照索引的顺序查询(a,然后是b,然后是c)。如果您需要按b搜索,那么a,那么您最好使用新索引。

答案 1 :(得分:1)

a和b上的当前索引足以让您返回完全匹配。

这是一个例子

s1:PRIMARY> db.users.insert({"username":"anil","email":"abc@gmail.com","age":21})
WriteResult({ "nInserted" : 1 })
s1:PRIMARY> db.users.insert({"username":"sunil","email":"pqr@gmail.com","age":22})
WriteResult({ "nInserted" : 1 })

- 现在在所有3个属性上创建索引

s1:PRIMARY> db.users.createIndex({"username":1,"email":1,"age":1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

检查所有3个属性上是否存在索引

s1:PRIMARY> db.users.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "projects.users"
        },
        {
                "v" : 1,
                "key" : {
                        "username" : 1,
                        "email" : 1,
                        "age" : 1
                },
                "name" : "username_1_email_1_age_1",
                "ns" : "projects.users"
        }
]

现在使用executionStats选项

进行解释输出
s1:PRIMARY> db.users.find({"username":"sunil","email":"pqr@gmail.com"},{"username":1,"_id":0,"email":1}).explain("executionStats");
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "projects.users",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "email" : {
                                                "$eq" : "pqr@gmail.com"
                                        }
                                },
                                {
                                        "username" : {
                                                "$eq" : "sunil"
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "PROJECTION",
                        "transformBy" : {
                                "username" : 1,
                                "_id" : 0,
                                "email" : 1
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "username" : 1,
                                        "email" : 1,
                                        "age" : 1
                                },
                                "indexName" : "username_1_email_1_age_1",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "username" : [
                                                "[\"sunil\", \"sunil\"]"
                                        ],
                                        "email" : [
                                                "[\"pqr@gmail.com\", \"pqr@gmail.com\"]"
                                        ],
                                        "age" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 0,
                "executionStages" : {
                        "stage" : "PROJECTION",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "transformBy" : {
                                "username" : 1,
                                "_id" : 0,
                                "email" : 1
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 2,
                                "advanced" : 1,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "username" : 1,
                                        "email" : 1,
                                        "age" : 1
                                },
                                "indexName" : "username_1_email_1_age_1",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "username" : [
                                                "[\"sunil\", \"sunil\"]"
                                        ],
                                        "email" : [
                                                "[\"pqr@gmail.com\", \"pqr@gmail.com\"]"
                                        ],
                                        "age" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                },
                                "keysExamined" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "L4156409",
                "port" : 47018,
                "version" : "3.2.5",
                "gitVersion" : "34e65e5383f7ea1726332cb175b73077ec4a1b02"
        },
        "ok" : 1
}
s1:PRIMARY>

totalKeysExamined = 1和totalDocsExamined = 0且noOfDocuments返回= 1,这意味着检索到的文档来自索引本身。

这些被称为覆盖查询。

HTH。