How to read Verbose Output from MongoDB-explain(1)

让人想犯罪 __ 提交于 2019-12-13 04:01:10

问题


I have the following query.explain(1)-Output. It is a verbose output and my question is how to read that. How is the order of the operations? Does it starts with GEO_NEAR_2DSPHERE or with LIMIT? What does the field advanced express?

And most important, where is this documented? Could not find this in the mongoDB-manual :(

Query:

db.nodesWays.find(
    {
        geo:{
            $nearSphere:{
                $geometry:{
                    type: "Point",
                    coordinates:  [lon, lat]
                }
            }
        }, 
        "amenity":"restaurant"
    }, 
    {name:1}
).limit(10).explain(1)   

The output:

{                                                                                                                                                       
    "cursor" : "S2NearCursor",                                                                                                                      
    "isMultiKey" : false,                                                                                                                           
    "n" : 10,                                                                                                                                       
    "nscannedObjects" : 69582,                                                                                                                      
    "nscanned" : 69582,                                                                                                                             
    "nscannedObjectsAllPlans" : 69582,                                                                                                              
    "nscannedAllPlans" : 69582,                                                                                                                     
    "scanAndOrder" : false,                                                                                                                         
    "indexOnly" : false,                                                                                                                            
    "nYields" : 543,                                                                                                                                
    "nChunkSkips" : 0,                                                                                                                              
    "millis" : 606,                                                                                                                                 
    "indexBounds" : {                                                                                                                               

    },                                                                                                                                              
    "allPlans" : [                                                                                                                                  
        {                                                                                                                                       
            "cursor" : "S2NearCursor",                                                                                                      
            "isMultiKey" : false,                                                                                                           
            "n" : 10,                                                                                                                       
            "nscannedObjects" : 69582,                                                                                                      
            "nscanned" : 69582,                                                                                                             
            "scanAndOrder" : false,                                                                                                         
            "indexOnly" : false,                                                                                                            
            "nChunkSkips" : 0,                                                                                                              
            "indexBounds" : {                                                                                                               

            }                                                                                                                               
        }                                                                                                                                       
    ],                                                                                                                                              
    "server" : "DBTest:27017",                                                                                                                      
    "filterSet" : false,                                                                                                                            
    "stats" : {                                                                                                                                     
        "type" : "LIMIT",                                                                                                                       
        "works" : 69582,                                                                                                                        
        "yields" : 543,                                                                                                                         
        "unyields" : 543,                                                                                                                       
        "invalidates" : 0,                                                                                                                      
        "advanced" : 10,                                                                                                                        
        "needTime" : 69572,                                                                                                                     
        "needFetch" : 0,                                                                                                                        
        "isEOF" : 1,                                                                                                                            
        "children" : [                                                                                                                          
            {                                                                                                                               
                "type" : "PROJECTION",                                                                                                  
                "works" : 69582,                                                                                                                        
                "yields" : 543,                                                                                                         
                "unyields" : 543,                                                                                                       
                "invalidates" : 0,                                                                                                      
                "advanced" : 10,                                                                                                                 
                "needTime" : 0,                                                                                                         
                "needFetch" : 0,                                                                                                        
                "isEOF" : 0,                                                                                                            
                "children" : [                                                                                                                    
                    {                                                                                                               
                        "type" : "FETCH",                                                                                                      
                        "works" : 69582,                                                                                        
                        "yields" : 543,
                        "unyields" : 543,                                                        
                        "invalidates" : 0,
                        "advanced" : 10,                                                                                        
                        "needTime" : 69572,                                                                                     
                        "needFetch" : 0,                                                                                        
                        "isEOF" : 0,                                                                                            
                        "alreadyHasObj" : 4028,                                                                                 
                        "forcedFetches" : 0,                                                                                    
                        "matchTested" : 10,                                                                                     
                        "children" : [                                                                                          
                            {                                                                                               
                                "type" : "GEO_NEAR_2DSPHERE",                                                           
                                "works" : 69582,                                                                        
                                "yields" : 0,                                                                           
                                "unyields" : 0,                                                                         
                                "invalidates" : 0,                                                                      
                                "advanced" : 4028,                                                                      
                                "needTime" : 0,                                                                         
                                "needFetch" : 0,                                                                        
                                "isEOF" : 0,                                                                            
                                "children" : [ ]                                                                        
                            }                                                                                               
                        ]                                                                                                       
                    }                                                                                                               
                ]
            }                                                                                                                               
        ]                                                                                                                                       
    }                                                                                                                                               
}   

回答1:


By looking at the stats array, the sequence should be

  1. GEO_NEAR_2DSPHERE -> scans 69582 index objects.
  2. Fetch and limit -> Fetches matched documents up to limited number of documents.
  3. Projection -> Project to return only required fields.

The reason why MongoDB wrap all actions in LIMIT is to align with the query's syntax for easier interpretation.

The query uses an unknown index of type S2NearCursor. In addition to the index, it also retrieved whole document for further reduction on amenity. You may want to explore indexing that as well.

BTW, this is a known bug in MongoDB. It misses the index name when using S2NearCursor index.

As for detailed documentation, I myself also don't find much, but a few online blogs you can browse around.

explain.explain() – Understanding Mongo Query Behavior

Speeding Up Queries: Understanding Query Plans

I especially want to recommend you to pay attention to the last paragraph of the two blog posts. Tune, generate the query plan and try to explain the plan yourself. Doing this a number of rounds, you'll get some idea how it works.

Happy explaining. : )



来源:https://stackoverflow.com/questions/24012802/how-to-read-verbose-output-from-mongodb-explain1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!