mongodb索引查找索引排序

时间:2014-08-30 21:08:11

标签: mongodb sorting indexing find

我想问一下mongodb索引。我可以在查找和排序中使用不同的索引。例如,我有两个索引:

    (a:-1)
    (b:1,c:1)

哪些索引使用这句话?

    ({a:[$gt30}},{a:[$lt50}}]}.sort({c:1})

3 个答案:

答案 0 :(得分:1)

  

我可以在查找和排序中使用不同的索引。

在阅读完之后,您将在文档页面底部看到索引交叉:http://docs.mongodb.org/manual/core/index-intersection/#index-intersection-and-sort

  

当sort()操作要求索引与查询谓词完全分离时,索引交集不适用。

所以不,即使ypou创建了{c:1}的索引,它也无法独立地用于交叉{a:1}

  

哪些索引使用这句话?

在这种情况下,只会使用{a:1}

答案 1 :(得分:0)

在名为单字段索引的单个字段上创建索引。

创建多个Single Field索引以增强查询,排序性能并没有多大帮助!您应该改用化合物索引

  

查看MongoDB上的文档:https://docs.mongodb.com/manual/core/index-compound/

如果您想学习如何索引字段以及如何衡量查询的性能。

  

然后在YouTube上查看本教程:https://dplink.app/nxLgvk7lR

答案 2 :(得分:0)

假设我正在收集具有如下文档的人:

{
 dob: 
  { age : 50} , 
 gender : "male" ,
 phone : ""
}

现在我创建索引如下。

1 : db.persons.createIndex({"dob.age" : -1})
2 : db.persons.createIndex({phone : 1 , gender : 1})
  1. 现在,如果我像您一样执行以下查询

db.persons.explain(“ executionStats”)。find({$ and:[{“ dob.age”:{$ lt:50}},    {“ dob.age”:{$ gt:30}}]}).sort({gender:1})

我将获得以下执行统计信息:

{
            "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "college.persons",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                            "$and" : [
                                    {
                                            "dob.age" : {
                                                    "$lt" : 50
                                            }
                                    },
                                    {
                                            "dob.age" : {
                                                    "$gt" : 30
                                            }
                                    }
                            ]
                    },
                    "queryHash" : "22FEA299",
                    "planCacheKey" : "5E8F38C1",
                    "winningPlan" : {
                            "stage" : "SORT",
                            "sortPattern" : {
                                    "phone" : 1
                            },
                            "inputStage" : {
                                    "stage" : "SORT_KEY_GENERATOR",
                                    "inputStage" : {
                                            "stage" : "FETCH",
                                            "inputStage" : {
                                                    "stage" : "IXSCAN",
                                                    "keyPattern" : {
                                                            "dob.age" : -1
                                                    },
                                                    "indexName" : "dob.age_-1",
                                                    "isMultiKey" : false,
                                                    "multiKeyPaths" : {
                                                            "dob.age" : [ ]
                                                    },
                                                    "isUnique" : false,
                                                    "isSparse" : false,
                                                    "isPartial" : false,
                                                    "indexVersion" : 2,
                                                    "direction" : "forward",
                                                    "indexBounds" : {
                                                            "dob.age" : [
                                                                    "(50.0, 30.0)"
                                                            ]
                                                    }
                                            }
                                    }
                            }
                    },
                    "rejectedPlans" : [ ]
            },
            "executionStats" : {
                    "executionSuccess" : true,
                    "nReturned" : 1734,
                    "executionTimeMillis" : 10,
                    "totalKeysExamined" : 1734,
                    "totalDocsExamined" : 1734,
                    "executionStages" : {
                            "stage" : "SORT",
                            "nReturned" : 1734,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 3471,
                            "advanced" : 1734,
                            "needTime" : 1736,
                            "needYield" : 0,
                            "saveState" : 27,
                            "restoreState" : 27,
                            "isEOF" : 1,
                            "sortPattern" : {
                                    "phone" : 1
                            },
                            "memUsage" : 1914799,
                            "memLimit" : 33554432,
                            "inputStage" : {
                                    "stage" : "SORT_KEY_GENERATOR",
                                    "nReturned" : 1734,
                                    "executionTimeMillisEstimate" : 0,
                                    "works" : 1736,
                                    "advanced" : 1734,
                                    "needTime" : 1,
                                    "needYield" : 0,
                                    "saveState" : 27,
                                    "restoreState" : 27,
                                    "isEOF" : 1,
                                    "inputStage" : {
                                            "stage" : "FETCH",
                                            "nReturned" : 1734,
                                            "executionTimeMillisEstimate" : 0,
                                            "works" : 1735,
                                            "advanced" : 1734,
                                            "needTime" : 0,
                                            "needYield" : 0,
                                            "saveState" : 27,
                                            "restoreState" : 27,
                                            "isEOF" : 1,
                                            "docsExamined" : 1734,
                                            "alreadyHasObj" : 0,
                                            "inputStage" : {
                                                    "stage" : "IXSCAN",
                                                    "nReturned" : 1734,
                                                    "executionTimeMillisEstimate" : 0,
                                                    "works" : 1735,
                                                    "advanced" : 1734,
                                                    "needTime" : 0,
                                                    "needYield" : 0,
                                                    "saveState" : 27,
                                                    "restoreState" : 27,
                                                    "isEOF" : 1,
                                                    "keyPattern" : {
                                                            "dob.age" : -1
                                                    },
                                                    "indexName" : "dob.age_-1",
                                                    "isMultiKey" : false,
                                                    "multiKeyPaths" : {
                                                            "dob.age" : [ ]
                                                    },
                                                    "isUnique" : false,
                                                    "isSparse" : false,
                                                    "isPartial" : false,
                                                    "indexVersion" : 2,
                                                    "direction" : "forward",
                                                    "indexBounds" : {
                                                            "dob.age" : [
                                                                    "(50.0, 30.0)"
                                                            ]
                                                    },
                                                    "keysExamined" : 1734,
                                                    "seeks" : 1,
                                                    "dupsTested" : 0,
                                                    "dupsDropped" : 0
                                            }
                                    }
                            }
                    }
            },
            "serverInfo" : {
                    "host" : "RGGYSLT-0483",
                    "port" : 27017,
                    "version" : "4.2.0",
                    "gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
            },
            "ok" : 1
    }

这意味着首先使用IXScan在单个字段上获取数据,然后在 第三场分开。

但是当我更改查询以对已经为其创建索引的字段进行排序时,事情就发生了变化。现在{“ dob.age”:-1}索引被拒绝了。

在Mongo db中,胜出计划是一种可以提前获取100个文档的计划,而Mongo db缓存可以为查询计划。现在,将在插入1000个文档,重建索引,重新启动服务器或插入新索引后清除此缓存。

因此,将使用哪个索引取决于制胜计划。

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "college.persons",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "dob.age" : {
                                                "$lt" : 50
                                        }
                                },
                                {
                                        "dob.age" : {
                                                "$gt" : 30
                                        }
                                }
                        ]
                },
                "queryHash" : "DA8248FA",
                "planCacheKey" : "E779554F",
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$and" : [
                                        {
                                                "dob.age" : {
                                                        "$lt" : 50
                                                }
                                        },
                                        {
                                                "dob.age" : {
                                                        "$gt" : 30
                                                }
                                        }
                                ]
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "indexName" : "gender_1_phone_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "gender" : [ ],
                                        "phone" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "gender" : [
                                                "[MinKey, MaxKey]"
                                        ],
                                        "phone" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "SORT",
                                "sortPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "inputStage" : {
                                        "stage" : "SORT_KEY_GENERATOR",
                                        "inputStage" : {
                                                "stage" : "FETCH",
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "dob.age" : -1
                                                        },
                                                        "indexName" : "dob.age_-1",
                                                        "isMultiKey" : false,
                                                        "multiKeyPaths" : {
                                                                "dob.age" : [ ]
                                                        },
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "dob.age" : [
                                                                        "(50.0, 30.0)"
                                                                ]
                                                        }
                                                }
                                        }
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1734,
                "executionTimeMillis" : 12,
                "totalKeysExamined" : 5002,
                "totalDocsExamined" : 5002,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$and" : [
                                        {
                                                "dob.age" : {
                                                        "$lt" : 50
                                                }
                                        },
                                        {
                                                "dob.age" : {
                                                        "$gt" : 30
                                                }
                                        }
                                ]
                        },
                        "nReturned" : 1734,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 5003,
                        "advanced" : 1734,
                        "needTime" : 3268,
                        "needYield" : 0,
                        "saveState" : 41,
                        "restoreState" : 41,
                        "isEOF" : 1,
                        "docsExamined" : 5002,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 5002,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 5003,
                                "advanced" : 5002,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 41,
                                "restoreState" : 41,
                                "isEOF" : 1,
                                "keyPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "indexName" : "gender_1_phone_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "gender" : [ ],
                                        "phone" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "gender" : [
                                                "[MinKey, MaxKey]"
                                        ],
                                        "phone" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                },
                                "keysExamined" : 5002,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "RGGYSLT-0483",
                "port" : 27017,
                "version" : "4.2.0",
                "gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
        },
        "ok" : 1
}