我正在使用nodetime来分析我的node.js应用程序的高CPU使用率。 超过30%的CPU使用来自Mongoose:
下一个最大的罪魁祸首,只有5%,是垃圾收集器。
我 相信 之前我听说过Mongoose会导致高CPU使用率,最好跳过它并直接使用Mongo驱动程序。这准确吗?
这是“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));
};
我怎样才能加快这个热点?
注意正如您所看到的那样,查询需要花费很长时间,而是需要很长时间来处理结果的Mongo驱动程序(并且消耗了大量的CPU)处理)。
答案 0 :(得分:11)
使用Mongoose,将lean选项用于具有大型结果集的查询非常重要,除了普通的JavaScript文档本身之外,您不需要任何其他内容。这应该提供与直接使用本机驱动程序相当的性能。
例如,在上面的情况中,它将是:
this.model.find({$or: or}, fields).lean().exec(function(e, matches) {
// ... excluded for brevity
}).bind(this));