5

I run explain on the following query:

db.explain().find({ site_id:1, dimensions:[], create_date: { $gte: new Date(1452603948196) } )

The result contains a 'filter' object over the dimensions field, while it should have filtered that field using the index, what does it mean ? isn't it a redundant stage?

{ "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "dimensions" : {
                "$eq" : [ ]
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "site_id" : 1,
                "dimensions" : 1,
                "create_date" : 1
            }, }

as far as i understand it means that mongo filter the dimensions field again after it is scanning the index and fetch the documents into memory, is it correct?

thanks,

Shachar
  • 487
  • 5
  • 15
  • Tbh, I am unsure. It is filtering the IXSCAN by that which could also mean that for some reason MongoDB only evaluated that field of the index for the winning plan – Sammaye Jan 18 '16 at 13:09
  • This might have to do with how arrays are indexed, in particular because your query is for `dimensions: []` -- see https://stackoverflow.com/a/45830609/1218408. – ZachB Dec 10 '19 at 02:28

2 Answers2

1

Your results will be filtered based on the criteria that dimensions are equal to values given in the array.

Shalabh Raizada
  • 354
  • 2
  • 8
  • If that were the case then whole find condition would be in filter – Sammaye Jan 18 '16 at 13:06
  • does it mean its not using the index to filter dimensions? – Shachar Jan 18 '16 at 14:28
  • Please review this topic here - https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/. They limit the fields to return in a query result i.e. from the input stage results only dimensions will be returned after filter in winning plan. – Shalabh Raizada Jan 20 '16 at 06:29
0

Yes, you're right. From the documentation on explain

The explain results present the query plans as a tree of stages. Each stage passes its results (i.e. documents or index keys) to the parent node. The leaf nodes access the collection or the indices. The internal nodes manipulate the documents or the index keys that result from the child nodes. The root node is the final stage from which MongoDB derives the result set.

And also

explain.queryPlanner.winningPlan.stage A string that denotes the name of the stage.

Each stage consists of information specific to the stage. For instance, an IXSCAN stage will include the index bounds along with other data specific to the index scan. If a stage has a child stage or multiple child stages, the stage will have an inputStage or inputStages.

explain.queryPlanner.winningPlan.inputStage A document that describes the child stage, which provides the documents or index keys to its parent. The field is present if the parent stage has only one child.

In other words, IXSCAN is an child stage for FETCH (since it is an input stage) and the results of the IXSCAN is sent up to FETCH.

Source: https://docs.mongodb.com/manual/reference/explain-results/

YAPPO
  • 173
  • 1
  • 13
  • 2
    This info is true, but the question is about what appears to be a redundant `filter` after the index scan. – ZachB Dec 10 '19 at 02:30