使用mongo数组索引表现不佳

时间:2015-06-05 06:25:33

标签: arrays mongodb indexing

嗨,我知道有很多关于这方面的说法,但我无法找到我的具体问题的答案。我有以下JSON文档,并尝试为questions.questionEntry.metaTags数组创建一个有效的索引:



{
  "questions": [
    {
      "questionEntry": {
        "id": 1,
        "info": {
          "seasonNumber": 1,
          "episodeNumber": 1,
          "episodeName": "Days Gone Bye"
        },
        "questionItem": {
          "theQuestion": "",
          "attachedElement": {
            "type": 1,
            "value": ""
          }
        },
        "options": [
          {
            "type": 1,
            "value": ""
          },
          {
            "type": 1,
            "value": ""
          }
        ],
        "answer": {
          "questionId": 1,
          "answer": 1
        },
        "metaTags": [
          "Season 1",
          "Episode 1"
        ]
      }
    }
  ]
}




然后我向我的数据库添加了5000,000个重复文档,并添加了一个包含不同数据字段的附加文档来运行一些测试。

我在unindexed集合上运行了以下查询,执行时间为640ms: db.questions1.find({"questions.questionEntry.metaTags" : "Season 1"},{'questions.$':1})._addSpecial( "$explain", 1 ).pretty()

然后我创建了以下索引: db.questions1.createIndex( { "questions.questionEntry.metaTags" : 1 })

现在我运行了相同的查询,但现在执行时间是9070毫秒......!

这是explain(),显示了500001个检查过的文件!:



{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.questions1",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "questions.questionEntry.metaTags" : {
                                "$eq" : "Season 1"
                        }
                },
                "winningPlan" : {
                        "stage" : "PROJECTION",
                        "transformBy" : {
                                "questions.$" : 1
                        },
                        "inputStage" : {
                                "stage" : "FETCH",
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                                "questions.questionEntry.metaTa
s" : 1
                                        },
                                        "indexName" : "questions.questionEntry.
etaTags_1",
                                        "isMultiKey" : true,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "questions.questionEntry.metaTa
s" : [
                                                        "[\"Season 1\", \"Seaso
 1\"]"
                                                ]
                                        }
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 500001,
                "executionTimeMillis" : 11255,
                "totalKeysExamined" : 500001,
                "totalDocsExamined" : 500001,
                "executionStages" : {
                        "stage" : "PROJECTION",
                        "nReturned" : 500001,
                        "executionTimeMillisEstimate" : 10750,
                        "works" : 500002,
                        "advanced" : 500001,
                        "needTime" : 0,
                        "needFetch" : 0,
                        "saveState" : 3907,
                        "restoreState" : 3907,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "transformBy" : {
                                "questions.$" : 1
                        },
                        "inputStage" : {
                                "stage" : "FETCH",
                                "nReturned" : 500001,
                                "executionTimeMillisEstimate" : 9310,
                                "works" : 500002,
                                "advanced" : 500001,
                                "needTime" : 0,
                                "needFetch" : 0,
                                "saveState" : 3907,
                                "restoreState" : 3907,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "docsExamined" : 500001,
                                "alreadyHasObj" : 0,
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "nReturned" : 500001,
                                        "executionTimeMillisEstimate" : 8970,
                                        "works" : 500001,
                                        "advanced" : 500001,
                                        "needTime" : 0,
                                        "needFetch" : 0,
                                        "saveState" : 3907,
                                        "restoreState" : 3907,
                                        "isEOF" : 1,
                                        "invalidates" : 0,
                                        "keyPattern" : {
                                                "questions.questionEntry.metaTa
s" : 1
                                        },
                                        "indexName" : "questions.questionEntry.
etaTags_1",
                                        "isMultiKey" : true,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "questions.questionEntry.metaTa
s" : [
                                                        "[\"Season 1\", \"Seaso
 1\"]"
                                                ]
                                        },
                                        "keysExamined" : 500001,
                                        "dupsTested" : 500001,
                                        "dupsDropped" : 0,
                                        "seenInvalidated" : 0,
                                        "matchTested" : 0
                                }
                        }
                },
                "allPlansExecution" : [ ]
        },
        "serverInfo" : {
                "host" : "Voltage",
                "port" : 27017,
                "version" : "3.0.3",
                "gitVersion" : "b40106b36eecd1b4407eb1ad1af6bc60593c6105"
        }
}




Mongo db不是我的事,而且我很难理解为什么执行需要更长的时间?

索引字符串metaTags数组的最佳方法是什么?

非常感谢

0 个答案:

没有答案