【发布时间】:2016-03-02 22:17:02
【问题描述】:
我读过记录的预分配可以提高性能,这应该是有益的,尤其是在处理时间序列数据集的许多记录时。
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)的开销。
标签: javascript node.js mongodb mongodb-query pre-allocation