为什么我的mongodb通话这么慢? [英] Why is my mongodb call so slow?

查看:37
本文介绍了为什么我的mongodb通话这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在构建一个基于Node.js的应用程序,并且我正在使用mongoose处理与mongodb的连接.我有一个这样的端点:

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:

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

由于某种原因,此呼叫需要700毫秒以上的时间才能完成.甚至没有应用mongodb shell进行的限制的相同调用将在大约4毫秒内返回.看起来像是一个简单的查询,那么到底是什么让它变慢了呢?我猜我错过了某些地方的明显配置,但我不知道.

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.

感谢任何可以提供帮助的人.

Thanks to anyone who can help on this.

其他信息:

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:

  • 我请求的对象很大,因此处理它们需要一些时间.对于大对象,修改查询以仅返回您现在需要的字段 .
  • 猫鼬很有用,但是当您请求很多物品时,它确实会减慢速度,如果您想提高通话速度,最好直接与node-mongodb-native交互.(在我的情况下,速度提高了约50%以上)

使用这些技术,我现在可以以比以前处理1000条更少的时间来处理4000条记录.感谢所有发表评论的人,特别感谢 Gates VP 指出猫鼬并不是一个很好的选择进行这种呼叫.

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.

这篇关于为什么我的mongodb通话这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆