猫鼬(node.js模块)导致较高的CPU使用率 [英] Mongoose (node.js module) causes high CPU usage

查看:80
本文介绍了猫鼬(node.js模块)导致较高的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodetime分析我的node.js应用程序的高CPU使用率. 猫鼬的CPU使用率超过30%:

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:

仅次于5%的第二大罪魁祸首是垃圾收集器.

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

相信 ,我以前曾听说过,Mongoose会导致较高的CPU使用率,最好跳过它并直接使用Mongo驱动程序.这是正确的吗?

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?

这是"Geocode.decodeMnay"功能,触发了此特定热点...

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?

请注意,正如您所看到的,查询不是花费很长时间,而是花费大量时间来处理结果的Mongo驱动程序(并且消耗了很多CPU).过程).

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

推荐答案

对于Mongoose,使用

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

这篇关于猫鼬(node.js模块)导致较高的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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