简体   繁体   English

如何“解释”runCommand?

[英]How to 'explain' a runCommand?

Is there a way to run explain with runCommand ?有没有办法用runCommand运行explain I have the following query:我有以下查询:

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

How can I run an explain on it?我怎样才能对其进行explain I want to get the execution time.我想获得执行时间。

As far as I know, explain is a method on cursors.据我所知, explain是一种关于游标的方法。 You, however, can enable the integrated mongodb profiler :但是,您可以启用集成的 mongodb 分析器

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.这将记录诸如nscannednreturned类的详细信息到一个名为system.profile有上限的集合中,但不会提供像explain()调用那样多的详细信息。

In this case , however, I think it might be possible to change the runCommand to a $near -query instead?但是,在这种情况下,我认为可以将runCommand改为$near runCommand吗? That would give you full access to explain .这将使您完全访问explain

I guess we can't do explain for runCommand.我想我们无法解释 runCommand。 Some of the runCommand give you the stats automatically ( eg. distinct command : db.runCommand({distinct : 'test', key : 'a'}) )一些 runCommand 会自动为您提供统计信息(例如不同的命令: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.运行该查询后,关闭分析器,并检查该查询的 system.profile 集合。

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.两个答案(来自mnemosynAbhishek Kumar )都是正确的。

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:也许不是runCommand但我使用这样的东西:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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