Poor performance with mongo array index

86 Views Asked by At

Hi I know there has been much said regarding this but I'm unable to find an answer to my specific problem. I have the following JSON document and trying to create an efficient index for the questions.questionEntry.metaTags array:

{
  "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"
        ]
      }
    }
  ]
}

I then added 5000,000 duplicate documents to my DB and an additional document with different data fields to run some tests.

I ran the following query on the unindexed collection with an execution time of 640ms: db.questions1.find({"questions.questionEntry.metaTags" : "Season 1"},{'questions.$':1})._addSpecial( "$explain", 1 ).pretty()

Then I created the following index: db.questions1.createIndex( { "questions.questionEntry.metaTags" : 1 })

Now I ran the same query but now the execution time is 9070ms...!

Here is the explain() showing 500001 documents examined!:

{
        "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 is not my thing and I'm struggling to understand why execution is taking longer?

What would be the best method to index the string metaTags array?

Many thanks

0

There are 0 best solutions below