Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MongoDB中搜索两个字段比搜索一个字段要慢得多_Mongodb_Performance_Indexing - Fatal编程技术网

在MongoDB中搜索两个字段比搜索一个字段要慢得多

在MongoDB中搜索两个字段比搜索一个字段要慢得多,mongodb,performance,indexing,Mongodb,Performance,Indexing,我是MongoDB的新手,尝试用相当大的数据库设置1个节点。10亿行,140GB,10个字段。除一个字段外,所有字段都有单独的索引,因为我们计划了许多不同的查询,这些查询将组合2或3个字段 我的问题是MongoDB似乎没有在组合搜索中使用单独的索引 db.alldata.find({ p: "степанович" }) 此搜索的回答时间不到1秒 db.alldata.find({ n: "иван" }) 这个也是 但是db.alldata.find({n:“ааа”,p:“ааааааа

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

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

db.alldata.find({ p: "степанович" })
此搜索的回答时间不到1秒

db.alldata.find({ n: "иван" })
这个也是

但是db.alldata.find({n:“ааа”,p:“ааааааааач”})

要花很长时间才能完成。我做错什么了吗? 我不想使用组合索引,因为我们有太多不同的查询,甚至单独的索引几乎占用所有RAM(50GB)

将创建索引。而且有足够的内存来容纳它们。 OS是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
}
我的问题是MongoDB似乎没有在上使用单独的索引 联合搜索

在MongoDb 2.6之后,MongoDb支持

但如果需要,应该使用联合索引。因为它可以很好地处理

即使使用索引,查询也非常重要。因此,您应该使用基于的查询。因此,您应该选择将使用哪个索引,或者不使用索引进行查询


理解这些概念后,您可以根据自己的期望做出最佳选择。

请更新您的帖子,并解释结果<代码>db.alldata.find({n:“аааа”,p:“ааааааааааач”})。另外,如果您使用的是mmap或wiredtiger引擎,请说明您的MongoDB版本。官方回购和wiredtiger的“3.2.4”。添加了所有计划执行。谢谢。请将
collection.getIndexes()
的输出也发送给我。制作复合索引是浪费时间。没有任何变化,3个字段的复合索引为32GB,而3个单独的索引只占17。现在重新构建单独的索引(前台大约需要10个小时),并将使用提示进行实验。问题中描述了DB。10个字段,每个字段占用7-10GBs,所以我买不起复合索引。他们不适合RAM。难以置信,但这没有帮助。我做db.alldata.find(blabla.hint)(“姓氏”,“名字”)和查询工作几分钟。当然,在我的10亿行中,我有许多非唯一的值。我无法改变这一点。很有趣。MongoDB允许按名称指定带有两个索引的提示,但实际上不使用第二个索引。并且不允许按列名在提示中设置第二个索引。所以,我可能也必须使用复合索引。你的moongodb版本是什么?“版本”:“3.2.4”,“gitVersion”:“e2ee9ffcf9f5a94fad76802e28cc978718bb7a30”