Discipol
Discipol

Reputation: 3157

Does a projection decrease query and/or response time?

I keep seeing the projection parameter in find() queries and was wondering if adding it makes things faster. I assume it does for large documents, but what about small documents of, say, 10 values or so?

I am seeking to optimize a project where MANY MANY small queries are performed.

Upvotes: 3

Views: 220

Answers (1)

vinipsmaker
vinipsmaker

Reputation: 2397

You should profile. In MongoDB shell, try db.collection.find().explain().

If you have indexes, then it may help. Indexed queries are faster and if your queries only use fields stored in the index itself, you'll have a covered query (indexOnly in cursor.explain()). The projection parameter help to turn a query into a covered query.

If you insert documents more frequently than you read documents, then it might not be a good idea to use indexes, but you should always profile it.

Upvotes: 2

Related Questions