javascript - 使用计数预分配记录

标签 javascript node.js mongodb mongodb-query pre-allocation

我读到记录的预分配可以提高性能,这应该是有益的,尤其是在处理时间序列数据集的许多记录时。

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

我有点担心必须通过 promise 可能会减慢速度,并且每次都检查计数可能会抵消预分配记录的性能优势。

有没有更高效的方法来处理这个问题?

最佳答案

“预分配”的一般说法是关于导致文档“增长”的“更新”操作的潜在成本。如果这导致文档大小大于当前分配的空间,则文档将“移动”到磁盘上的另一个位置以容纳新空间。这可能代价高昂,因此一般建议最初编写适合其最终“大小”的文档。

老实说,处理此类操作的最佳方法是先执行“upsert”并分配所有数组元素,然后仅更新位置所需的元素。这将减少到“两次”潜在写入,并且您可以使用批量 API 方法进一步减少到单个“在线”操作:

var id = _ref,"|"+year+"|"+month;
var bulk = db.collection('ref_history').initializeOrderedBulkOp();

bulk.find({ "_id": id }).upsert().updateOne({
    "$setOnInsert": {
        "dates": Array.apply(null,Array(32)).map(function(el) { return { "count": 0 }})
   }
});

var update={"$inc":inc['dates.'+day+'.count'] = 1;};
bulk.find({ "_id": id }).updateOne(update);

bulk.execute(function(err,results) {
   // results would show what was modified or not
});

或者由于较新的驱动程序倾向于相互保持一致性,“批量”部分已降级为 WriteOperations 的常规数组:

var update={"$inc":inc['dates.'+day+'.count'] = 1;};

db.collection('ref_history').bulkWrite([
    { "updateOne": {
        "filter": { "_id": id },
        "update": {
            "$setOnInsert": {
                "dates": Array.apply(null,Array(32)).map(function(el) {
                    return { "count": 0 }
                })
            }
        },
        "upsert": true
    }},
    { "updateOne": {
        "filter": { "_id": id },
        "update": update
    }}
],function(err,result) {
    // same thing as above really
});

无论哪种情况,$setOnInsert因为唯一的 block 只会在实际发生“upsert”时做任何事情。主要情况是与服务器的唯一联系将是单个请求和响应,而不是等待网络通信的“来回”操作。

这通常是“批量”操作的用途。当您不妨向服务器发送一批请求时,它们会减少网络开销。结果显着加快了速度,除了“ordered”异常(exception),这两个操作都不真正依赖于另一个,这是后一种情况下的默认设置,并由遗留 .initializeOrderedBulkOp()显式设置.

是的,在“upsert”中有一个“小”开销,但比使用 .count() 测试并首先等待结果要“少”。


N.B 不确定您列表中的 32 个数组条目。您可能是说 24,但复制/粘贴占了上风。无论如何,有比硬编码更好的方法来做到这一点,正如所证明的那样。

关于javascript - 使用计数预分配记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759280/

相关文章:

javascript - 仅在使用 jquery 验证表单后创建 JSON

MySQL - 当一个表可能没有对应的行时如何在查询中合并 2 个表

c# - 使 ASP.NET 身份在 Mongo 中存储用户数据(托管在 Heroku 上)

javascript - 如何使用 jQuery 将单击时 div 的位置更改为父 div 的中心?

javascript - 如何在javascript中重复一个函数?

javascript - 在三个条件下无法在长度为三的数组上获得三个结果

node.js - Expressjs 的全局变量

javascript - mongoose - 如何在 getter 中获取对象而不是对象引用?

c# - 使用 mongodb 动态创建 linq

javascript - 查看元素的所有 dom 事件