Why are my mongodb queries so slow (on Swisscom cloud)?

302 Views Asked by At

I am using a (small, 256 MB) MongoDB 3.2.9 service instance through Swisscom CloudFoundry. As long as our entire DB fits into the available RAM, we see somewhat acceptable query performance.

However, we are experiencing very long query times on aggregation operations when our DB does not fit into RAM. We have created indexes for the accessed fields, but as far as I can tell it doesn't help.

Example document entry:

_id: 5a31...
description: Object
    location: "XYZ"
    name: "ABC"
    status: "A"
    m_nr: null
    k_nr: null
    city: "QWE"
    high_value: 17
    right_value: 71
more_data: Object
    number: 101
    interval: 1
    next_date: "2016-01-16T00:00:00Z"
    last_date: null
    status: null
classification: Object
    priority_value: "?"
    redundancy_value: "?"
    active_value: "0"

Example Query:

db.getCollection('a').aggregate(
    [{ $sort:
        {"description.location": 1}
     },
     { $group:
        {_id: "$description.location"}
     }],
     { explain: true }
)

This query takes 25sec on a DB that only has 20k entries and produces 1k output fields.

The explain info for this query:

db.getCollection('a').aggregate([{ $group: {_id: "$description.location"} }], { explain: true }):

{
    "waitedMS" : NumberLong(0),
    "stages" : [ 
        {
            "$cursor" : {
                "query" : {},
                "fields" : {
                    "description.location" : 1,
                    "_id" : 0
                },
                "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "Z.a",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "$and" : []
                    },
                    "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                            "$and" : []
                        },
                        "direction" : "forward"
                    },
                    "rejectedPlans" : []
                }
            }
        }, 
        {
            "$group" : {
                "_id" : "$description.location"
            }
        }
    ],
    "ok" : 1.0
}

[UPDATE] Output of db.a.getIndexes():

/* 1 */
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "db.a"
    },
    {
        "v" : 1,
        "key" : {
            "description.location" : 1.0
        },
        "name" : "description.location_1",
        "ns" : "db.a"
    }
]
1

There are 1 best solutions below

10
On

Looks like it's doing a collection scan, have you tried adding an index on description.location?

db.a.createIndex({"description.location" : 1});