6

I'm using nodetime to analyze the high CPU usage of my node.js app. Over 30% of the CPU usage is coming from Mongoose:

enter image description here

The next biggest culprit, at a mere 5%, is the Garbage Collector.

I believe I've heard, before, that Mongoose can cause high CPU usage, and that it can be preferable to skip it and directly use the Mongo driver. Is this accurate?

Here's the "Geocode.decodeMnay" function, triggered this particular hotspot...

Geocode.prototype.decodeMany = function(strs, callback)
{
    var or = [],
        map = {},
        fields = {'woeid': 1, 'matched_queries': 1, 'latitude': 1, 'longitude': 1, 'radius': 1, 'name': 1},
        unique = [];

    strs = _.uniq(strs);
    for(var k=0; k<strs.length; k++)
        or.push({'matched_queries':strs[k].trim()});    

    this.model.find({$or: or}, fields, (function(e,matches){
        // ... excluded for brevity
    }).bind(this));
};

How else might I speed up this hotspot?

note that it is not the query taking a long time, as you can see, but rather the Mongo driver which is taking a long time to process the results (and consuming lots of CPU in the process).

1 Answer 1

18

With Mongoose, it's important to use the lean option for queries with large result sets where you don't need anything but the plain JavaScript documents themselves. That should provide performance comparable to using the native driver directly.

For example, in the case above it would be:

this.model.find({$or: or}, fields).lean().exec(function(e, matches) {
    // ... excluded for brevity
}).bind(this));
Sign up to request clarification or add additional context in comments.

3 Comments

I have no idea how I missed this in the documentation. Thanks!
Using lean() on large find queries dropped execution time by 70-80% and eliminated the 100% CPU spikes. Hugely useful option that I wish I knew about sooner!
I would upvote 20 times if I could. I was out of ideas and that literally saved me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.