user3525134
user3525134

Reputation: 133

MongoDB Query Performance: Return all vs select fields

I'm having some unexpected results with benchmarking the performance of various queries on a collection I made for testing purposes. The collection is somewhat mimicking my real needs with 10.000 documents, each with 20 fields (each with 5-30 characters). All the documents are exactly the same, and are having only the _id different (maybe this is somehow the problem?).

Contrary to what official MongoDB documentation suggests, specifying which fields to return does not result in better performance, but much much worse.

The plain find is done in around 5msec.

db.collection.find().explain()

The custom find is done in around 30msec.

db.collection.find({},{Field1:1,Field2:1,Field3:1,Field4:1,Field5:1,Field6:1,Field7:1},{}).explain()

Is the plain 'find all' and 'return all' query really faster or am I missing something?

Upvotes: 8

Views: 2724

Answers (1)

Vitaly Greck
Vitaly Greck

Reputation: 668

If you return whole doc - it's less overhead for the database cause it does not convert document to return partial one. All docs are stored at the database in BSON format. And returned the same way.

In your case, a small overhead is expected.

Field limitation is good for large amounts of data, when you have 10000 resulting docs, and it's much more faster to convert the document at the DBMS level rather than transmit over the socket layer.

Upvotes: 2

Related Questions