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

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

问题描述

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

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 驱动程序需要很长时间来处理结果(并且在过程).

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,使用 精益 选项适用于具有大型结果集的查询,您只需要纯 JavaScript 文档本身就不需要任何东西.这应该提供与直接使用本机驱动程序相当的性能.

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

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

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