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?
Use limit() to maximize performance and prevent MongoDB from returning more results than required for processing.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With