为什么我的mongodb查询速度如此之慢(在Swisscom云上)?

时间:2017-12-13 17:00:33

标签: mongodb aggregation-framework swisscomdev

我通过Swisscom CloudFoundry使用(小型,256 MB)MongoDB 3.2.9服务实例。只要我们的整个数据库适合可用的RAM,我们就会看到一些可接受的查询性能。

但是,当我们的数据库不适合RAM时,我们在聚合操作上遇到很长的查询时间。我们为访问过的字段创建了索引,但据我所知,它没有帮助。

示例文档条目:

_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"

示例查询:

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

此查询在只有 20k 条目的数据库上 25秒,并生成 1k 输出字段。

此查询的说明信息:

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
}

[更新] 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 个答案:

答案 0 :(得分:2)

看起来它正在进行收集扫描,您是否尝试在description.location上添加索引?

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