简体   繁体   中英

How to 'explain' a runCommand?

Is there a way to run explain with runCommand ? I have the following query:

db.runCommand({geoNear:"Locations", near:[50,50], spherical:true})

How can I run an explain on it? I want to get the execution time.

As far as I know, explain is a method on cursors. You, however, can enable the integrated mongodb profiler :

db.setProfilingLevel(2); // log all operations
db.setProfilingLevel(1, 50); // log all operations longer than 50msecs

This will log details like nscanned , nreturned to a capped collection called system.profile , but does not provide as much detail as an explain() call does.

In this case , however, I think it might be possible to change the runCommand to a $near -query instead? That would give you full access to explain .

I guess we can't do explain for runCommand. Some of the runCommand give you the stats automatically ( eg. distinct command : db.runCommand({distinct : 'test', key : 'a'}) )

But, you can take help of the query profiler.

db.setProfilingLevel(2)

Once you run that query, switch off the profiler, and check the system.profile collection for this query.

According to explain docs you can simply:

db.runCommand({
    explain: {geoNear:"Locations", near:[50,50], spherical:true}
})

Both answers (from mnemosyn and Abhishek Kumar ) are correct.

However, if you just want to see the execution time (like me), this is provided in the results along with some extra stats:

...
"stats" : {
        "time" : 2689,
        "btreelocs" : 0,
        "nscanned" : 1855690,
        "objectsLoaded" : 979,
        "avgDistance" : 0.006218027001875209,
        "maxDistance" : 0.006218342348749806
},
"ok" : 1

Well, I should have looked closer before posting the question :).

Maybe not a runCommand but I use something like this:

expStats = function() { 
    var exp = db.collection.find({"stats.0.age" : 10},{"stats.age" : 1}).explain("allPlansExecution");

    print ("totalDocsExamined = "+exp.executionStats.totalDocsExamined);    
    print ("nReturned = "+exp.executionStats.nReturned);    
    return print ("execTime = "+exp.executionStats.executionTimeMillis);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM