我读过预分配记录可以提高性能,这应该是有益的,特别是在处理时间序列数据集的多个记录时。使用计数预分配记录
updateRefLog = function(_ref,year,month,day){
var id = _ref,"|"+year+"|"+month;
db.collection('ref_history').count({"_id":id},function(err,count){
// pre-allocate if needed
if(count < 1){
db.collection('ref_history').insert({
"_id":id
,"dates":[{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0},{"count":0}]
});
}
// update
var update={"$inc":inc['dates.'+day+'.count'] = 1;};
db.collection('ref_history').update({"_id":id},update,{upsert: true},
function(err, res){
if(err !== null){
//handle error
}
}
);
});
};
我有点担心,不必经过一个承诺可能会减缓下来,每次将否定预分配记录的性能优势可能检查计数。
有没有更好的方法来处理这个问题?
你使用的是什么版本的MongoDB ..如果MongoDB 3.0或更新的版本是什么存储引擎?预分配对于MMAP存储引擎来说是一种有用的优化技术,但会增加不支持就地更新的其他存储引擎(例如WiredTiger)的开销。 – Stennie