简体   繁体   English

Mongoose(node.js模块)导致高CPU使用率

[英]Mongoose (node.js module) causes high CPU usage

I'm using nodetime to analyze the high CPU usage of my node.js app. 我正在使用nodetime来分析我的node.js应用程序的高CPU使用率。 Over 30% of the CPU usage is coming from Mongoose: 超过30%的CPU使用来自Mongoose:

在此输入图像描述

The next biggest culprit, at a mere 5%, is the Garbage Collector. 垃圾收集器是下一个最大的罪魁祸首,仅为5%。

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. 相信之前我已经听说过Mongoose会导致高CPU使用率,并且最好跳过它并直接使用Mongo驱动程序。 Is this accurate? 这准确吗?

Here's the "Geocode.decodeMnay" function, triggered this particular hotspot... 这是“Geocode.decodeMnay”功能,触发了这个特定的热点......

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). 请注意 ,查询需要花费很长时间,正如您所看到的,而是需要很长时间来处理结果的Mongo驱动程序(并且在此过程中消耗了大量CPU)。

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. 使用Mongoose,将lean选项用于具有大型结果集的查询非常重要,除了纯JavaScript文档本身之外,您不需要任何其他内容。 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));

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

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