S
S
Shahelm2014-05-12 18:23:35
PHP
Shahelm, 2014-05-12 18:23:35

Mongodb aggregation - why is it so slow?

Good evening everyone!
There is a limited collection:

db.createCollection("Error", {capped:true, size:419430400, max:200000})

The collection stores different types of errors from different servers.
It is necessary to count the number of errors of each type on each server in the last 24 hours.
My query option:
db.Error.aggregate (
    [
        {
            "$match" : {
                "dateAdd" : {
                    "$gt" : ISODate("2014-05-11T13:16:52Z"),
                    "$lte" : ISODate("2014-05-12T13:16:52Z")
                }
            }
        },
        {
            "$group" : {
                "_id" : {
                    "server" : "$server",
                    "errorType" : "$errorType"
                },
                "count" : {
                    "$sum" : NumberLong(1)
                }
            }
        }
    ]
)

When the collection is filled to the maximum (200000 errors), the request is executed in 0.5 seconds.
The index, judging by explain, is picked up.
Existing indexes:
db.Error.ensureIndex({web:1, hash:1, dateAdd:1});
db.Error.ensureIndex({type:1, dateAdd:1})
db.Error.ensureIndex({web:1, type:1, dateAdd:1});
db.Error.ensureIndex({dateAdd:1})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly F., 2014-08-13
@FuN_ViT

solution - a background task that runs aggregation once a day and saves the results in an add. collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question