Chm
Chm

Reputation: 23

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:

{
    _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:

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

E.g., using MongoDB 2.4.6 on my machine I see the following times used to push 10 elements on arrays of length 5000:

That difference seems unexpected. Is this a problem with MongoDB or my use of the multikey index? Is there a recommended way of handling this? Thanks.

Upvotes: 2

Views: 576

Answers (1)

Amalia
Amalia

Reputation: 936

Take a look at SERVER-8192 and SERVER-8193. Hopefully that will help answer your question!

Upvotes: 1

Related Questions