简体   繁体   English

为什么我的mongodb打电话这么慢?

[英]Why is my mongodb call so slow?

Alright, so I'm building an application based in Node.js and I am using mongoose to handle my connection to mongodb. 好吧,所以我正在构建一个基于Node.js的应用程序,我正在使用mongoose来处理我与mongodb的连接。 I have an endpoint that is such: 我有一个端点是这样的:

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. 由于某种原因,此调用需要700毫秒+才能完成。 The same call without even applying a limit made from mongodb shell returns in about 4ms. 即使没有应用mongodb shell的限制,相同的调用也会在大约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. 我猜我错过了配置中某些显而易见的东西,但我不知道。

Thanks to anyone who can help on this. 感谢任何可以提供帮助的人。

Other info: 其他信息:

mongoose@2.6.0
mongodb@2.0.4
node@0.6.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. Mongoose很有用,但是当你请求很多项时它真的会变慢,如果你想要一个调用的速度,最好直接与node-mongodb-native接口。 (This was about a 50%+ speed increase for my scenario) (对于我的场景,这大约增加了50%+速度)

Using these techniques I can now process 4000 records in less time than I was processing 1000 before. 使用这些技术,我现在可以在比处理1000之前更短的时间内处理4000条记录。 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. 感谢任何评论的人,特别感谢盖茨副总裁指出猫鼬不适合这种电话。

The same call without even applying a limit made from mongodb shell returns in about 4ms. 即使没有应用mongodb shell的限制,相同的调用也会在大约4ms内返回。

The shell applies a limit of 30 or so by default. shell默认情况下应用30左右的限制。 Try doing from the shell with an actual limit? 尝试从具有实际限制的shell做起?

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 Banker的建议并查看分析器

checkout ensureIndex这将加快您的搜索速度

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

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