Lab 3.1

In this lab you're going to determine which index was used to satisfy a query given its explain output.
The following query was ran:
> var exp = db.restaurants.explain("executionStats")
> exp.find({ "address.state": "NY", stars: { $gt: 3, $lt: 4 } }).sort({ name: 1 }).hint(REDACTED)
Which resulted in the following output:
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "m201.restaurants",
    "indexFilterSet": false,
    "parsedQuery": "REDACTED",
    "winningPlan": {
      "stage": "SORT",
      "sortPattern": {
        "name": 1
      },
      "inputStage": {
        "stage": "SORT_KEY_GENERATOR",
        "inputStage": {
          "stage": "FETCH",
          "inputStage": {
            "stage": "IXSCAN",
            "keyPattern": "REDACTED",
            "indexName": "REDACTED",
            "isMultiKey": false,
            "isUnique": false,
            "isSparse": false,
            "isPartial": false,
            "indexVersion": 1,
            "direction": "forward",
            "indexBounds": "REDACTED"
          }
        }
      }
    },
    "rejectedPlans": [ ]
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 3335,
    "executionTimeMillis": 20,
    "totalKeysExamined": 3335,
    "totalDocsExamined": 3335,
    "executionStages": "REDACTED"
  },
  "serverInfo": "REDACTED",
  "ok": 1
}
Given the redacted explain output above, select the index that was passed to hint.

Note: The hint() method is used to force the query planner to select a particular index for a given query. You can learn more about hint by visiting its documentation.

Choose the best answer.

Answer:
  • { "address.state": 1, "name": 1, "stars": 1 } - No - if this index was used, then there would be no SORT stage.
  • { "address.state": 1, "stars": 1, "name": 1 } - Yes - It wouldn't need to examine any extra index keys, so since nReturned and totalKeysExamined are both 3,335 we know this index was used.
  • { "address.state": 1, "name": 1 } - No - if this index was used, then there would be no SORT stage.
  • { "address.state": 1 } - No - if this index was used, then we would expect that we'd have to examine some unnecessary documents and index keys. Since there are 50 states in the US, and we have 1,000,000 documents we'd expect to examine about 20,000 documents, not the 3,335 we see in the output.


Comments

  1. Could you please explain be so kind to explain a few details to me? I would really appreciate it!

    1.- Why "... { "address.state": 1, "stars": 1, "name": 1 } - Yes ..." is not using the index to sort?

    2.- Why "... { "address.state": 1, "name": 1, "stars": 1 } - No ..." there wouldn't be a SORT stage when using this index?

    Thank you!

    ReplyDelete

Post a Comment

Popular posts from this blog

M201 MongoDB Performance - Labs & Final Exam Q&A

Question 1

Question 7