Reputation: 11095
I am curious about the performance difference between calling findOne()
ten times and calling find()
once with ten arguments. Is the latter sufficiently better?
Upvotes: 1
Views: 1823
Reputation: 46301
In general, it makes sense to test this in your environment with your data, but a theoretical answer is: it depends, but generally, yes - it improves performance since it reduces the number of round-trips to the database, and network lag is usually the dominant factor unless your query is highly inefficient. This is more prominent in production and with multiple data centers and less of an issue on localhost.
The question is where your ten arguments come from - if you wanted to select a set of 10 elements by id, an $in
-query is more elegant and blazing fast. However, you could also use ten different queries in an $or
-query and still reduce the number of round-trips. However, there are some pitfalls with $or
and indexes that are described well in the documentation.
The topic of reducing the number of round-trips is also known as the N+1 problem in the context of pseudo-joins and there's a wide agreement that reducing the number of round-trips is key.
Upvotes: 1