我讀過預分配記錄可以提高性能,這應該是有益的,特別是在處理時間序列數據集的多個記錄時。使用計數預分配記錄
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