【问题标题】:Search on two fields in MongoDB are incredibly slow comparing to one field search与一个字段搜索相比,在 MongoDB 中搜索两个字段非常慢
【发布时间】:2016-07-13 12:23:52
【问题描述】:

我是 MongoDB 的新手,正在尝试使用相当大的数据库设置 1 个节点。 10 亿行,140GB,10 个字段。除了一个之外的所有字段都有单独的索引,因为我们计划了许多不同的查询,它们将组合 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
}

【问题讨论】:

  • 请用解释结果更新您的帖子。 db.alldata.find({ n: "иван", p: "степанович" }).explain("allPlansExecution ")。另外请提及您的 MongoDB 版本,以及您是否使用 mmap 或 Wiredtiger 引擎。
  • "3.2.4" 来自官方 repo 和wiredTiger。添加了 allPlansExecution。
  • 谢谢。请也将collection.getIndexes() 的输出发送给我。
  • 制作复合索引是浪费时间。没有任何变化,3 个字段的复合索引为 32GB,而 3 个单独的索引只用了 17 个。现在重建单独的索引(这在前台大约需要 10 小时)并将尝试提示。
  • DB 在问题中有所描述。 10 个字段,每个占用 7-10GB,所以我买不起复合索引。它们不适合 RAM。

标签: mongodb performance indexing


【解决方案1】:

我的问题是 MongoDB 似乎没有在 组合搜索。

MongoDb 2.6 之后,MongoDb 支持Index Intersection

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

即使有索引,查询也很重要。因此,您应该使用基于Create Queries that Ensure Selectivity 的查询。因此,您应该使用hint() 选择将使用哪个索引或不使用任何索引。

了解这些概念后,您可以measure your indexes 并根据您的期望做出最好的决定。

【讨论】:

  • 难以置信,但没有帮助。我做了 db.alldata.find(blabla).hint("surname","firstname") 并且查询工作了几分钟。在我的 10 亿行中,我当然有许多非唯一值。我无法改变这一点。
  • 有趣。 MongoDB 允许按名称指定带有两个索引的提示,但实际上并不使用第二个。并且不允许按列名在提示中设置第二个索引。所以,可能我也必须使用复合索引。
  • 你的moongodb版本是多少?
  • “版本”:“3.2.4”,“gitVersion”:“e2ee9ffcf9f5a94fad76802e28cc978718bb7a30”
猜你喜欢
  • 2013-03-07
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 2022-01-25
  • 1970-01-01
  • 2018-07-13
相关资源
最近更新 更多