0

Let's suppose that there are 100 types of dogs scattered across the documents of my collection. If I need to group by types of dogs and then compute some aggregate statistic about each type of dog, why perform a query involving $group, which in principle processes each document sequentially, versus sending 100 separate, simultaneous queries to the database where each query filters based on the type of dog?

Wouldn't the 100 simultaneous, separate query be faster?

If it is faster, at scale, what is the drawback to not do it, if any?

1 Answer 1

1

why perform a query involving $group which in principle processes each document sequentially

This is not necessarily true. The database engine is designed to be optimized for such type of operations, when you architect your database properly. Parallelism and set-based operations of database engines are not the same as sequential processing.

The more information you provide it, i.e. a complete query, and reliant you are on database operations to be handled in the database layer, the more efficient it can potentially be in processing and serving the results. The engine comes up with a plan to execute in the most optimal way it believes, based on all of the information it has at hand.

Wouldn't the 100 simultaneous, separate query be faster?

Generally, no, because of what I said above. There is also overhead for those 100 simultaneous queries, both from a concurrency standpoint and from hardware bottlenecks such as returning more data across the network than if you only returned the final processed result set from a single query.

Then you'd also end up re-assembling those 100 queries results and doing your aggregations in the application layer which typically is less performant than letting an optimized database engine do that work for you in the most efficient way it knows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.