与一个字段搜索相比,在MongoDB中搜索两个字段的速度非常慢

时间:2016-03-25 17:51:11

标签: mongodb performance indexing

我是MongoDB的新手,尝试使用相当大的数据库设置1个节点。 10亿行,140GB,10个字段。除了one之外的所有字段都有单独的索引,因为我们计划了许多不同的查询,这些查询将组合2或3个字段。

我的问题是MongoDB似乎没有在组合搜索上使用单独的索引。

db.alldata.find({ p: "степанович" })

此搜索的答案不到1秒。

db.alldata.find({ n: "иван" })

这个也是。

但是db.alldata.find({n:“иван”,p:“степанович”})

需要永远完成。难道我做错了什么? 我不想使用组合索引,因为我们有太多不同的查询,甚至单独的索引几乎占用所有RAM(50GB)

已创建索引 。还有 足够的RAM来适应它们。 操作系统是CentOS7,FS是xfs(如果重要的话)。

更新:

db.alldata.find({ n: "иван", p: "степанович"}).explain("allPlansExecution")

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "myDb.alldata",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "n" : {
                                                "$eq" : "иван"
                                        }
                                },
                                {
                                        "p" : {
                                                "$eq" : "степанович"
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "n" : {
                                        "$eq" : "иван"
                                }
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "p" : 1
                                },
                                "indexName" : "parentalName",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "p" : [
                                                "[\"степанович\", \"степанович\"]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "FETCH",
                                "filter" : {
                  "p" : {
                                                "$eq" : "степанович"
                                        }
                                },
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                                "n" : 1
                                        },
                                        "indexName" : "firstName",
                                        "isMultiKey" : false,
                                        "isUnique" : false,
                                        "isSparse" : false,
                                        "isPartial" : false,
                                        "indexVersion" : 1,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "n" : [
                                                        "[\"иван\", \"иван\"]"
                                                ]
                                        }
                                }
                        },
                        {
                                "stage" : "FETCH",
                                "filter" : {
                                        "$and" : [
                                                {
                                                        "n" : {
                                                                "$eq" : "иван"
                                                        }
                                                },
                                                {
          "p" : {
                                                                "$eq" : "степанович"
                                                        }
                                                }
                                        ]
                                },
                                "inputStage" : {
                                        "stage" : "AND_SORTED",
                                        "inputStages" : [
                                                {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "n" : 1
                                                        },
                                                        "indexName" : "firstName",
                                                        "isMultiKey" : false,
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 1,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "n" : [
                                                                        "[\"иван\", \"иван\"]"
                                                                ]
                                                        }
                                                },
                                                {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "p" : 1
                                                        },
                                                        "indexName" : "parentalName",
                                                        "isMultiKey" : false,
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 1,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "p" : [
                                                                        "[\"степанович\", \"степанович\"]"
                                                                ]
                                                        }
                                                }
                                        ]
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 31,
                "executionTimeMillis" : 213504,
                "totalKeysExamined" : 124244,
                "totalDocsExamined" : 124244,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "n" : {
                                        "$eq" : "иван"
                                }
                        },
                        "nReturned" : 31,
                        "executionTimeMillisEstimate" : 13160,
                      "works" : 124246,
                        "advanced" : 31,
                        "needTime" : 124213,
                        "needYield" : 0,
                        "saveState" : 11965,
                        "restoreState" : 11965,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 124244,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 124244,
                                "executionTimeMillisEstimate" : 240,
                                "works" : 124245,
                                "advanced" : 124244,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 11965,
                                "restoreState" : 11965,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "p" : 1
                                },
                                "indexName" : "parentalName",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "p" : [
                                                "[\"степанович\", \"степанович\"]"
                                        ]
                                },
                                "keysExamined" : 124244,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                },
                "allPlansExecution" : [
                        {
                                "nReturned" : 18,
                                "executionTimeMillisEstimate" : 196410,
                                "totalKeysExamined" : 124245,
                                "totalDocsExamined" : 124245,
                                "executionStages" : {
                                        "stage" : "FETCH",
                                        "filter" : {
                                                "p" : {
                                                        "$eq" : "степанович"
                                                }
                                        },
                                        "nReturned" : 18,
                                        "executionTimeMillisEstimate" : 196410,
                                        "works" : 124245,
                                        "advanced" : 18,
                                        "needTime" : 124227,
                                        "needYield" : 0,
                                        "saveState" : 11965,
                                        "restoreState" : 11965,
                                  "isEOF" : 0,
                                        "invalidates" : 0,
                                        "docsExamined" : 124245,
                                        "alreadyHasObj" : 0,
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "nReturned" : 124245,
                                                "executionTimeMillisEstimate" : 240,
                                                "works" : 124245,
                                                "advanced" : 124245,
                                                "needTime" : 0,
                                                "needYield" : 0,
                                                "saveState" : 11965,
                                                "restoreState" : 11965,
                                                "isEOF" : 0,
                                                "invalidates" : 0,
                                                "keyPattern" : {
                                                        "n" : 1
                                                },
                                                "indexName" : "firstName",
                                                "isMultiKey" : false,
                                                "isUnique" : false,
                                                "isSparse" : false,
                                                "isPartial" : false,
                                                "indexVersion" : 1,
                                                "direction" : "forward",
                                                "indexBounds" : {
                                                        "n" : [
                                                                "[\"иван\", \"иван\"]"
                                                        ]
                                                },
                                                "keysExamined" : 124245,
                                                "dupsTested" : 0,
                                                "dupsDropped" : 0,
                                                "seenInvalidated" : 0
                                        }
                                }
                        },
                        {
                                "nReturned" : 13,
                                "executionTimeMillisEstimate" : 540,
                                "totalKeysExamined" : 124245,
                                "totalDocsExamined" : 13,
                                "executionStages" : {
                                        "stage" : "FETCH",
                                        "filter" : {
                                                "$and" : [
                                                        {
                                                                "n" : {
                                                                        "$eq" : "иван"
                                                                }
                                                        },
                                                        {
                                                                "p" : {
                                                                        "$eq" : "степанович"
                                                                }
                                                        }
                                                ]
                                        },
                                        "nReturned" : 13,
                                        "executionTimeMillisEstimate" : 540,
                                        "works" : 124245,
                                        "advanced" : 13,
                                        "needTime" : 124232,
                                      "needYield" : 0,
                                        "saveState" : 11965,
                                        "restoreState" : 11965,
                                        "isEOF" : 0,
                                        "invalidates" : 0,
                                        "docsExamined" : 13,
                                        "alreadyHasObj" : 0,
                                        "inputStage" : {
                                                "stage" : "AND_SORTED",
                                                "nReturned" : 13,
                                                "executionTimeMillisEstimate" : 490,
                                                "works" : 124245,
                                                "advanced" : 13,
                                                "needTime" : 124232,
                                                "needYield" : 0,
                                                "saveState" : 11965,
                                                "restoreState" : 11965,
                                                "isEOF" : 0,
                                                "invalidates" : 0,
                                                "flagged" : 0,
                                                "failedAnd_0" : 29865,
                                                "failedAnd_1" : 29858,
                                                "inputStages" : [
                                                        {
                                                                "stage" : "IXSCAN",
                                                                "nReturned" : 74468,
                                                                "executionTimeMillisEstimate" : 250,
                                                                "works" : 74468,
                                                                "advanced" : 74468,
                                                                "needTime" : 0,
                                                                "needYield" : 0,
                                                                "saveState" : 11965,
                                                                "restoreState" : 11965,
                                                                "isEOF" : 0,
                                                                "invalidates" : 0,
                                                                "keyPattern" : {
                                                                        "n" : 1
                                                                },
                                                                "indexName" : "firstName",
                                                                "isMultiKey" : false,
                                                                "isUnique" : false,
                                                                "isSparse" : false,
                                                                "isPartial" : false,
                                                                "indexVersion" : 1,
                                                                "direction" : "forward",
                                                                "indexBounds" : {
                                                                        "n" : [
                                                                                "[\"иван\", \"иван\"]"
                                                                        ]
                                                                },
                                                                "keysExamined" : 74468,
                                                                "dupsTested" : 0,
                                                                "dupsDropped" : 0,
                                                                "seenInvalidated" : 0
                                                        },
                                                        {
                                                                "stage" : "IXSCAN",
                                                                "nReturned" : 49777,
                                                                "executionTimeMillisEstimate" : 150,
                                                                "works" : 49777,
                                                                "advanced" : 49777,
                                                                "needTime" : 0,
                                                                "needYield" : 0,
                                                                "saveState" : 11965,
                                                                "restoreState" : 11965,
                                                                "isEOF" : 0,
                                                                "invalidates" : 0,
                                                                "keyPattern" : {
                                                                        "p" : 1
                                                                },
                                                                "indexName" : "parentalName",
                                                                "isMultiKey" : false,
                                                                "isUnique" : false,
                                                                "isSparse" : false,
                                                                "isPartial" : false,
                                                                "indexVersion" : 1,
                                                                "direction" : "forward",
                                                                "indexBounds" : {
                                                                        "p" : [
                                                                                "[\"степанович\", \"степанович\"]"
                                                                        ]
                                                                },
                                                                "keysExamined" : 49777,
                                                                "dupsTested" : 0,
                                                                "dupsDropped" : 0,
                                                                "seenInvalidated" : 0
                                                        }
                                                ]
                                        }
                                }
                        },
                        {
                                "nReturned" : 31,
                                "executionTimeMillisEstimate" : 13160,
                                "totalKeysExamined" : 124244,
                                "totalDocsExamined" : 124244,
                                "executionStages" : {
                                        "stage" : "FETCH",
                                        "filter" : {
                                                "n" : {
                                                        "$eq" : "иван"
                                                }
                                        },
                                        "nReturned" : 31,
                                        "executionTimeMillisEstimate" : 13160,
                                        "works" : 124245,
                                        "advanced" : 31,
                                        "needTime" : 124213,
                                        "needYield" : 0,
                                        "saveState" : 11965,
                                        "restoreState" : 11965,
                                        "isEOF" : 1,
                                        "invalidates" : 0,
                                        "docsExamined" : 124244,
                                        "alreadyHasObj" : 0,
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "nReturned" : 124244,
                                                "executionTimeMillisEstimate" : 240,
                                                "works" : 124245,
                                                "advanced" : 124244,
                                                "needTime" : 0,
                                                "needYield" : 0,
                                                "saveState" : 11965,
                                                "restoreState" : 11965,
                                                "isEOF" : 1,
                                                "invalidates" : 0,
                                                "keyPattern" : {
                           "p" : 1
                                                },
                                                "indexName" : "parentalName",
                                                "isMultiKey" : false,
                                                "isUnique" : false,
                                                "isSparse" : false,
                                                "isPartial" : false,
                                                "indexVersion" : 1,
                                                "direction" : "forward",
                                                "indexBounds" : {
                                                        "p" : [
                                                                "[\"степанович\", \"степанович\"]"
                                                        ]
                                                },
                                                "keysExamined" : 124244,
                                                "dupsTested" : 0,
                                                "dupsDropped" : 0,
                                                "seenInvalidated" : 0
                                        }
                                }
                        }
                ]
        },
        "serverInfo" : {
                "host" : "cass",
                "port" : 27017,
                "version" : "3.2.4",
                "gitVersion" : "e2ee9ffcf9f5a94fad76802e28cc978718bb7a30"
        },
        "ok" : 1
}

1 个答案:

答案 0 :(得分:0)

  

我的问题是MongoDB似乎没有使用单独的索引   联合搜索。

在MongoDb 2.6之后,MongoDb支持Index Intersection

但是如果需要,你应该使用组合索引。因为prefixes可以处理得很好。

即使使用索引,查询也非常重要。因此,您应该使用基于Create Queries that Ensure Selectivity的查询。因此,您应该选择使用哪个索引,或者使用hint()查询不会使用索引。

在您理解了这些概念之后,您可以measure your indexes并最好地满足您的期望。