【问题标题】:mongodb index find index sortmongodb索引查找索引排序
【发布时间】:2014-08-30 21:08:10
【问题描述】:

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

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

什么索引使用了这句话?

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

【问题讨论】:

    标签: mongodb sorting indexing find


    【解决方案1】:

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

    阅读更多内容后,您将在文档页面底部看到关于索引交叉的内容:http://docs.mongodb.org/manual/core/index-intersection/#index-intersection-and-sort

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

    所以不,即使 ypou 创建了 {c:1} 的索引,它也不能单独用于与 {a:1} 相交

    什么索引使用了这句话?

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

    【讨论】:

      【解决方案2】:

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

      创建多个单字段索引来提高您的查询和排序性能不会有太大帮助!。您应该改用复合索引

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

      如果您想了解如何为字段编制索引以及如何衡量查询的性能。

      并在 Youtube 上查看本教程:https://dplink.app/nxLgvk7lR

      【讨论】:

        【解决方案3】:

        假设我正在收集具有以下文档的人员集合:

        {
         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 中,Winning 计划是可以提前获取 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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-27
          • 2012-01-16
          • 2021-06-02
          • 1970-01-01
          相关资源
          最近更新 更多