Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Query Performance: Return all vs select fields

Tags:

mongodb

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?

like image 598
user3525134 Avatar asked May 13 '14 17:05

user3525134


People also ask

What method can we use to maximize performance and prevent MongoDB from returning more results than required for processing?

Use limit() to maximize performance and prevent MongoDB from returning more results than required for processing.

Why is MongoDB query faster?

Performance. Because the index contains all fields required by the query, MongoDB can both match the query conditions and return the results using only the index. Querying only the index can be much faster than querying documents outside of the index.

What are some tips to improve the performance of NoSQL queries?

Add Appropriate Indexes NoSQL databases require indexes, just like their relational cousins. An index is built from a set of one or more fields to make querying fast. For example, you could index the country field in a user collection.


1 Answers

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.

like image 110
Vitaly Greck Avatar answered Sep 19 '22 19:09

Vitaly Greck