MongoDB平凡的聚合真的很慢

时间:2017-01-02 15:49:50

标签: mongodb mongodb-query aggregation-framework

我有URL视图的集合数据包含创建者URL的ID,访问的日期时间,浏览器,引用者,平台,设备等...现在我想按小时/天/月制作访问图。我有字段用户索引(asc)和创建(desc)。在集合中有大约1.5M的文档,在聚合中我计算大约800k文档,但它花了大约2秒! :/你能帮我加速聚合吗?我有浏览器,平台等视图访问的另一个聚合..它也很慢。我想着按小时将计数保存到图表的外部集合中,你怎么看?

聚合:

db.LinkView.aggregate([
    {
        "$match": {
            "created": {
                "$gte": new ISODate("2016-12-24T00:00:00"),
                "$lte": new ISODate("2017-01-02T00:00:00")
            },
            "user": ObjectId("585a7c2c1ccd5604f37238d2"),
        }
    },
    {
        "$group": {
            "_id" : {
                "year": {
                    "$year":"$created"
                },
                "month": {
                    "$month":"$created"
                },
                "day": {
                    "$dayOfMonth":"$created"
                },
                "hour": {
                    "$hour":"$created"
                }
            },
            "value": {
                "$sum":1
            }
        }
    }
], {
    explain:true
});

说明:

{
    "waitedMS" : NumberLong(0),
    "stages" : [
    {
        "$cursor" : {
            "query" : {
                "created" : {
                    "$gte" : ISODate("2016-12-24T00:00:00Z"),
                    "$lte" : ISODate("2017-01-02T00:00:00Z")
                },
                "user" : ObjectId("585a7c2c1ccd5604f37238d2")
            },
            "fields" : {
                "created" : 1,
                "_id" : 0
            },
            "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "shortener.LinkView",
                "indexFilterSet" : false,
                "parsedQuery" : {
                    "$and" : [
                        {
                            "user" : {
                                "$eq" : ObjectId("585a7c2c1ccd5604f37238d2")
                            }
                        },
                        {
                            "created" : {
                                "$lte" : ISODate("2017-01-02T00:00:00Z")
                            }
                        },
                        {
                            "created" : {
                                "$gte" : ISODate("2016-12-24T00:00:00Z")
                            }
                        }
                    ]
                },
                "winningPlan" : {
                    "stage" : "PROJECTION",
                    "transformBy" : {
                        "created" : 1,
                        "_id" : 0
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "user" : 1,
                            "created" : -1
                        },
                        "indexName" : "analytics_all",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "user" : [
                                "[ObjectId('585a7c2c1ccd5604f37238d2'), ObjectId('585a7c2c1ccd5604f37238d2')]"
                            ],
                            "created" : [
                                "[new Date(1483315200000), new Date(1482537600000)]"
                            ]
                        }
                    }
                },
                "rejectedPlans" : [ ]
            }
        }
    },
    {
        "$group" : {
            "_id" : {
                "year" : {
                    "$year" : [
                        "$created"
                    ]
                },
                "month" : {
                    "$month" : [
                        "$created"
                    ]
                },
                "day" : {
                    "$dayOfMonth" : [
                        "$created"
                    ]
                },
                "hour" : {
                    "$hour" : [
                        "$created"
                    ]
                }
            },
            "value" : {
                "$sum" : {
                    "$const" : 1
                }
            }
        }
    }
],
    "ok" : 1
}

0 个答案:

没有答案