2014년 12월 27일 토요일

mongo 2.6.5 aggregate explain result without nscanned

when I run an explain on a find I see the usual result:

db.entries.find( { "active": false }).explain()
{
        "cursor" : "BtreeCursor active_1",
        "isMultiKey" : false,
        "n" : 1,
        "nscannedObjects" : 1,
        "nscanned" : 1,
        "nscannedObjectsAllPlans" : 1,
        "nscannedAllPlans" : 1,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {
                "active" : [
                        [
                                false,
                                false
                        ]
                ]
        },
        "server" : "fang.home:27017",
        "filterSet" : false
}


can somebody tell me me why when I run an explain on an aggregate I don't the the same result format and instead:

> db.entries.aggregate( [ { "$match": { "active": false } }, { "$project": { "active": 1 } } ], { explain: true } )
{
        "stages" : [
                {
                        "$cursor" : {
                                "query" : {
                                        "active" : false
                                },
                                "fields" : {
                                        "active" : 1,
                                        "_id" : 1
                                },
                                "plan" : {
                                        "cursor" : "BtreeCursor ",
                                        "isMultiKey" : false,
                                        "scanAndOrder" : false,
                                        "indexBounds" : {
                                                "active" : [
                                                        [
                                                                false,
                                                                false
                                                        ]
                                                ]
                                        },
                                        "allPlans" : [
                                                {
                                                        "cursor" : "BtreeCursor ",
                                                        "isMultiKey" : false,
                                                        "scanAndOrder" : false,
                                                        "indexBounds" : {
                                                                "active" : [
                                                                        [
                                                                                false,
                                                                                false
                                                                        ]
                                                                ]
                                                        }
                                                }
                                        ]
                                }
                        }
                },
                {
                        "$project" : {
                                "active" : true
                        }
                }
        ],
        "ok" : 1
}



This is because explain on find actually executes the query and shows you results of objects it looked at.
Aggregation explain doesn't run the query but only prepares a plan.  Therefore it doesn't know how many documents would be examined, returned, etc.



Thanks for the reply Asya. Where did you find that info? I was looking at http://docs.mongodb.org/manual/reference/method/db.collection.aggregate/#example-aggregate-method-explain-option
Can I tell the aggregate to run the query I want explained?

I saw an answer with an aggregate result with nScannedObjects
(http://stackoverflow.com/questions/19591405/index-optimization-for-mongodb-aggregation-framework via runCommand on 2.4.5 I tried the runCommand but same result as above. Did that behavior change?



The explain facility wasn't really supported in 2.4.x (even though it
worked, it wasn't documented because parts of it didn't work
correctly).

2.6 is the version that added support to it - the difference between
explain on find and aggregate is a bit subtle but on find it shows how
the query runs (and that's only possible by running the query in 2.6
and earlier).  On aggregation it shows how the aggregation pipeline
itself is optimized
(http://docs.mongodb.org/manual/core/aggregation-pipeline-optimization/)
so its primary goal is to show the transformation of the stages and
the flow of data, and not really the actual results of running the
query part of the pipeline.


댓글 없음:

댓글 쓰기