mongodb:如何查看聚合命令的执行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14021605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb: how can I see the execution time for the aggregate command?
提问by alessio1985
I execute the follow mongodb command in mongo shell
我在 mongo shell 中执行 follow mongodb 命令
db.coll.aggregate(...)
and i see the list of result. but is it possible to see the query execution time? Is there any equivalent function for explain method for aggregation queries.
我看到了结果列表。但是可以看到查询执行时间吗?是否有用于聚合查询的解释方法的等效函数。
采纳答案by Sammaye
I will write an answer to explain this better.
我会写一个答案来更好地解释这一点。
Basically there is no explain() functionality for the aggregation framework yet: https://jira.mongodb.org/browse/SERVER-4504
基本上聚合框架还没有解释()功能:https: //jira.mongodb.org/browse/SERVER-4504
However there is a way to measure client side but not without its downsides:
然而,有一种方法可以衡量客户端,但并非没有缺点:
- You are not measuring the database
- You are measuring the application
- There are too many unknowns about the in between parts to be able to get an accurate reading, i.e. you can't say that it took 0.04ms for the document result to be formulated by the MongoDB server, serialised, sent over the wire, de-serialised by the app and then stored into a hash allowing you subtract that sum from the total to get a aggregation benchmark.
- 你不是在测量数据库
- 您正在测量应用程序
- 关于中间部分的未知数太多,无法获得准确的读数,即您不能说 MongoDB 服务器制定文档结果、序列化、通过网络发送、de 花费了 0.04 毫秒- 由应用程序序列化,然后存储到一个散列中,允许您从总数中减去该总和以获得聚合基准。
However that being said, you might be able to get a slightly accurate result by doing it in MongoDB console on the same server as the mongos
/ mongod
. This will create very little in betweens, still too many but enough to maybe get a reading you could roughly trust. As such you could use @Zagorulkin's answer in that position.
但是,话虽如此,您可能可以通过在与mongos
/相同的服务器上的 MongoDB 控制台中执行此操作来获得稍微准确的结果mongod
。这将在两者之间产生很少的影响,仍然太多但足以获得您可以大致信任的读数。因此,您可以在该位置使用@Zagoulkin 的答案。
回答by Dmitry Zagorulkin
var before = new Date()
#aggregation query
var after = new Date()
execution_mills = after - before
回答by alessio1985
i see that in mongodb there is a possibility to use this two command:
我看到在 mongodb 中可以使用这两个命令:
db.setProfilingLevel(2)
and so after the query you can use db.system.profile.find() to see the query execution time and other
db.setProfilingLevel(2)
所以在查询之后你可以使用 db.system.profile.find() 来查看查询执行时间和其他
回答by Lukasz Wiktor
You can add a time
function to your .mongorc.js
file (in your home directory):
您可以time
向.mongorc.js
文件中添加一个函数(在您的主目录中):
function time(command) {
const t1 = new Date();
const result = command();
const t2 = new Date();
print("time: " + (t2 - t1) + "ms");
return result;
}
and then you can use it like so:
然后你可以像这样使用它:
time(() => db.coll.aggregate(...))
Caution
警告
This method doesn't give relevant results for db.collection.find()
这种方法没有给出相关结果 db.collection.find()
回答by Dan Dascalescu
Or you can install the excellent mongo-hacker, which automatically times every query, pretty()
fies it, colorizes the output, sorts the keys, and more:
或者,您可以安装出色的mongo-hacker,它会自动对每个查询pretty()
计时、进行查询、对输出进行着色、对键进行排序等等:
回答by acdcjunior
Try .explain("executionStats")
:
尝试.explain("executionStats")
:
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
Which returns a very detailed JSON with the time in "executionTimeMillis"
like:
它返回一个非常详细的 JSON,时间"executionTimeMillis"
如下:
{
"queryPlanner" : {
...
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 3,
"executionTimeMillis" : 0,
...
More details: https://docs.mongodb.com/manual/tutorial/analyze-query-plan/#evaluate-the-performance-of-a-query
更多详情:https: //docs.mongodb.com/manual/tutorial/analyze-query-plan/#evaluate-the-performance-of-a-query