Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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.

Other info:

[email protected]
[email protected]
[email protected]
like image 563
Rob Riddle Avatar asked May 02 '12 17:05

Rob Riddle


1 Answers

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)

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.

like image 141
Rob Riddle Avatar answered Oct 02 '22 18:10

Rob Riddle