U
U
un1t2016-02-18 16:16:46
MongoDB
un1t, 2016-02-18 16:16:46

How to speed up aggregation in MongoDB 3.2?

I have a query like:

db.foo.aggregate([{$group: {_id: 1, 'myavg': {$avg:'$value'}}}])

The database has a million records. Works within 2-3 seconds. This is too much, I would like it faster.
If I understand Monga 3.2 correctly, it can use regular indexes for aggregation.
Changed in version 3.2: Starting in MongoDB 3.2, indexes can cover an aggregation pipeline. In MongoDB 2.6 and 3.0, indexes could not cover an aggregation pipeline since even when the pipeline uses an index, aggregation still requires access to the actual documents.

https://docs.mongodb.org/manual/core/aggregation-p...
But building the index
db.foo.ensureIndex({value:1})
doesn't seem to affect the situation
explain looks like this:
{
  "waitedMS" : NumberLong(0),
  "stages" : [
    {
      "$cursor" : {
        "query" : {
          
        },
        "fields" : {
          "correct" : 1,
          "_id" : 0
        },
        "queryPlanner" : {
          "plannerVersion" : 1,
          "namespace" : "mydb.foo",
          "indexFilterSet" : false,
          "parsedQuery" : {
            "$and" : [ ]
          },
          "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
              "$and" : [ ]
            },
            "direction" : "forward"
          },
          "rejectedPlans" : [ ]
        }
      }
    },
    {
      "$group" : {
        "_id" : {
          "$const" : 1
        },
        "myavg" : {
          "$avg" : "$correct"
        }
      }
    }
  ],
  "ok" : 1
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-02-18
@lega

The database has a million records. Works within 2-3 seconds. This is too much, I would like it faster.
For this query, indexes will not help much, because all documents are processed (without filtering).
2-3sec can be normal - it depends on your hardware, when you need it really fast, you can make one document: { total: ..., count: ...} and you will get the result in 1ms.
You can also try "_id : null" instead of "_id: 1"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question