0

I tried to bench 3 methods to group data: native js (with underscore), group and Aggregate with $group

I uses these datas (genre/position' trees in Paris) (237 168 rows, 35Mo)

This is my script test and the result is a bit surprising !

┌─────────────┬───────────────┐
│ Method      │ avg time (ms) │
├─────────────┼───────────────┤
│ Pure js     │ 897           │
├─────────────┼───────────────┤
│ Group       │ 3863          │
├─────────────┼───────────────┤
│ Aggregation │ 364           │
└─────────────┴───────────────┘

Why grouping with group is 10x slower than Aggregation ? For what is used "Group" ? And how can i optimise again my request ?

Thanks.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
PierrickP
  • 107
  • 7
  • Group may be slower due to the reduce function needing to be called fr every document in the collection. The best optimization would be to precompute the groups either directly or through a MapReduce. – WiredPrairie May 20 '13 at 10:54
  • It sohuld also be noted that it is probably a good idea to discourage the use of `group()` in MongoDB versions greater than 2.1 – Sammaye May 20 '13 at 13:05

1 Answers1

1

Group command uses the same framework as mapreduce and there are many resources for why MR is slower than aggregation framework. Main one is it runs in a separate JS thread, where agg framework runs natively on the server.

See details here MongoDB aggregation comparison: group(), $group and MapReduce

Community
  • 1
  • 1
Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • ok for group(). But it's still 2.3% slower than native ! – PierrickP May 20 '13 at 15:09
  • @lemulot Isn't group() greater than 300% slower? While the aggregation framework is 120% faster than native? – Sammaye May 20 '13 at 18:43
  • @lemulot it's slower because MongoDB has to convert bson to json do the group in separate thread, then return the results. "native" may be doing serialization to json faster and your algorithm may be more efficient than group algorithm. – Asya Kamsky May 20 '13 at 20:39