Alright, so I'm building an application based in Node.js and I am using mongoose to handle my connection to mongodb. I have an endpoint that is such:
我正在构建一个基于Node的应用程序。js和我使用mongoose来处理我与mongodb的连接。我有一个端点:
getTestStream : function(req, res, conditions, callback) {
Activity.find()
.limit(1000)
.run(function(err, activities) {
if (err){
util.sendError(req, res, "Query Error", err);
} else if (activities) {
res.send(activities);
} else {
util.send('nope');
}
});
}
For some reason this call takes 700ms+ to complete. The same call without even applying a limit made from mongodb shell returns in about 4ms. It seems like such a simple query, so what's slowing it down so much? I'm guessing I've missed something obvious in configuration somewhere, but I have no idea.
出于某种原因,这个调用需要700ms+才能完成。同样的调用甚至不需要使用mongodb shell在4毫秒内返回的限制。这似乎是一个简单的查询,那么是什么让它慢下来了呢?我猜我在配置中漏掉了一些明显的东西,但我不知道。
Thanks to anyone who can help on this.
感谢所有能在这方面提供帮助的人。
Other info:
其他信息:
mongoose@2.6.0
mongodb@2.0.4
node@0.6.9
3 个解决方案
#1
9
After experimenting for a while, I've found several contributions to slowness, hopefully this helps anyone with a similar issue:
在做了一段时间的实验后,我发现了一些对慢度的贡献,希望这能帮助任何有类似问题的人:
- The objects I'm requesting are large, so processing them takes some time. For large objects modify the query to only return the fields you need right now.
- 我请求的对象很大,所以处理它们需要一些时间。对于大型对象,请修改查询以仅返回您现在需要的字段。
- Mongoose is useful, but it can really slow down when you request a lot of items, its better to just directly interface with node-mongodb-native if you want speed for a call. (This was about a 50%+ speed increase for my scenario)
- Mongoose是非常有用的,但当你请求很多项时,它确实会减慢速度,如果你想要通话的速度,最好直接与node-mongodb-native进行交互。(对于我的场景来说,这是大约50%+的速度增长)
Using these techniques I can now process 4000 records in less time than I was processing 1000 before. Thanks for anyone who commented, and special thanks to Gates VP for pointing out that mongoose wasn't really a good fit for this kind of call.
使用这些技术,我现在可以在比以前处理1000条记录更短的时间内处理4000条记录。感谢所有发表评论的人,特别感谢盖茨的副总裁指出mongoose并不是一个适合这种电话的人。
#2
0
The same call without even applying a limit made from mongodb shell returns in about 4ms.
同样的调用甚至不需要使用mongodb shell在4毫秒内返回的限制。
The shell applies a limit of 30 or so by default. Try doing from the shell with an actual limit?
默认情况下,shell的限制是30左右。尝试用实际的极限来做壳层?
Also, you may want to try a .explain()
from the Shell.
另外,您可能需要尝试Shell中的.explain()。
If none of that works, then you can take @Kyle Banker's suggestion and check out the profiler.
如果这些都不奏效,那么您可以采纳@Kyle银行家的建议,查看剖析器。
#1
9
After experimenting for a while, I've found several contributions to slowness, hopefully this helps anyone with a similar issue:
在做了一段时间的实验后,我发现了一些对慢度的贡献,希望这能帮助任何有类似问题的人:
- The objects I'm requesting are large, so processing them takes some time. For large objects modify the query to only return the fields you need right now.
- 我请求的对象很大,所以处理它们需要一些时间。对于大型对象,请修改查询以仅返回您现在需要的字段。
- Mongoose is useful, but it can really slow down when you request a lot of items, its better to just directly interface with node-mongodb-native if you want speed for a call. (This was about a 50%+ speed increase for my scenario)
- Mongoose是非常有用的,但当你请求很多项时,它确实会减慢速度,如果你想要通话的速度,最好直接与node-mongodb-native进行交互。(对于我的场景来说,这是大约50%+的速度增长)
Using these techniques I can now process 4000 records in less time than I was processing 1000 before. Thanks for anyone who commented, and special thanks to Gates VP for pointing out that mongoose wasn't really a good fit for this kind of call.
使用这些技术,我现在可以在比以前处理1000条记录更短的时间内处理4000条记录。感谢所有发表评论的人,特别感谢盖茨的副总裁指出mongoose并不是一个适合这种电话的人。
#2
0
The same call without even applying a limit made from mongodb shell returns in about 4ms.
同样的调用甚至不需要使用mongodb shell在4毫秒内返回的限制。
The shell applies a limit of 30 or so by default. Try doing from the shell with an actual limit?
默认情况下,shell的限制是30左右。尝试用实际的极限来做壳层?
Also, you may want to try a .explain()
from the Shell.
另外,您可能需要尝试Shell中的.explain()。
If none of that works, then you can take @Kyle Banker's suggestion and check out the profiler.
如果这些都不奏效,那么您可以采纳@Kyle银行家的建议,查看剖析器。