简体   繁体   English

MongoDB多键索引写入性能下降

[英]MongoDB multikey index write performance degrading

In MongoDB I have a collection with documents having an array with subdocuments I would like to have an index on: 在MongoDB中,我有一个包含文档的集合,这些文档包含一个带有子文档的数组,我希望在其中建立索引:

{
    _id : ObjectId(),
    members : [
        { ref : ObjectId().str, ... },
        { ref : ObjectId().str, ... },
        ...
    ]
}

The index is on the ref field, such that I can quickly find all documents having a particular 'ref' in its members: 索引位于ref字段上,因此我可以快速找到其成员中具有特定“ ref”的所有文档:

db.test.ensureIndex({ "members.ref" : 1 });

I noticed that the performance of pushing an additional subdocument to the array degrades fast as the array length goes above a few thousand. 我注意到,当数组长度超过数千时,将另一个子文档推入数组的性能会迅速下降。 If I instead use an index on an array of strings, the performance does not degrade. 如果我改为在字符串数组上使用索引,则性能不会降低。

The following code demonstrates the behavior: 下面的代码演示了该行为:

var _id = ObjectId("522082310521b655d65eda0f");

function initialize () {
    db.test.drop();
    db.test.insert({ _id : _id, members : [], memberRefs : [] });
}

function pushToArrays (n) {
    var total, err, ref;

    total = Date.now();
    for (var i = 0; i < n; i++) {

        ref = ObjectId().str;
        db.test.update({ _id : _id }, { $push : { members : { ref : ref }, memberRefs : ref } });

        err = db.getLastError();
        if (err) {
            throw err;
        }

        if ((i + 1) % 1000 === 0) {
            print("pushed " + (i + 1));
        }
    }

    total = Date.now() - total;
    print("pushed " + n + " in " + total + "ms");
}

initialize();
pushToArrays(5000);

db.test.ensureIndex({ "members.ref" : 1 });
pushToArrays(10);
db.test.dropIndexes();

db.test.ensureIndex({ "memberRefs" : 1 });
pushToArrays(10);
db.test.dropIndexes();

Eg, using MongoDB 2.4.6 on my machine I see the following times used to push 10 elements on arrays of length 5000: 例如,在我的机器上使用MongoDB 2.4.6,我看到以下时间将10个元素压入长度为5000的阵列上:

  • Index on "members.ref": 37272ms 索引“ members.ref”:37272ms
  • Index on "memberRefs": 405ms 在“ memberRefs”上的索引:405ms

That difference seems unexpected. 这种差异似乎是出乎意料的。 Is this a problem with MongoDB or my use of the multikey index? 这是MongoDB的问题还是我对多键索引的使用? Is there a recommended way of handling this? 有建议的处理方式吗? Thanks. 谢谢。

Take a look at SERVER-8192 and SERVER-8193 . 看一下SERVER-8192SERVER-8193 Hopefully that will help answer your question! 希望这将有助于回答您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM