简体   繁体   中英

Does the size of a document affect performance of a find() query?

Can the size of a MongoDB document affect the performance of a find() query?

I'm running the following query on a collection, in the MongoDB shell

r.find({_id:ObjectId("5552966b380c2dbc29472755")})

The entire document is 3MB. When I run this query the operation takes about 8 seconds to perform. The document has a "salaries" property which makes up the bulk of the document's size (about 2.9MB). So when I ommit the salaries property and run the following query it takes less than a second.

r.find({_id:ObjectId("5552966b380c2dbc29472755")},{salaries:0})

I only notice this performance difference when I run the find() query only. When I run a find().count() query there is no difference. It appears that performance degrades only when I want to fetch the entire document.

The collection is never updated (never changes in size), an index is set on _id and I've run repairDatabase() on the database. I've searched around the web but can't find a satisfactory answer to why there is a performance difference. Any insight and recommendations would be appreciated. Thanks.

I think the experiments you've just ran are an answer to your own question.

Mongo will index the _id field by default, so document size shouldn't affect the length of time it takes to locate the document, but if its 3MB then you will likely notice a difference in actually downloading that data. I imagine that's why its taking less time if you omit some of the fields.

To get a better sense of how long your query is actually taking to run, try this:

r.find({
    _id: ObjectId("5552966b380c2dbc29472755")
})
    .explain(function(err, explaination) {
        if (err) throw err;
        console.log(explaination);
    });

If salaries is the 3MB culprit, and its structured data, then to speed things up you could try A) splitting it up into separate mongo documents or B) querying based on sub-properties of that document, and in both cases A and B you can build indexes to keep those queries fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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