从2.4迁移到2.6.11后,MongoDB聚合速度太慢

时间:2016-02-10 14:51:24

标签: mongodb

在2.4版本中,聚合在大约1分钟内工作,在迁移到2.6.11之后,聚合过程在大约3小时内结束。我重新创建了所有指数,但仍然很慢。此集合中有6549937个对象。

聚合脚本:

db.runCommand({
aggregate: "Profile",
pipeline:
[
    { $match : { "count" : { "$gt" : 0} } },
    { $group : 
        { "_id" : "$type", "sum" : { "$sum" : "$count"}, 
        "count" : { "$sum" : 1}, "min" : { "$min" : "$count"} , 
        "max" : { "$max" : "$count"}
        }           
    },
    { $project:
        { _id: 1,
          count: 1,
          sum: 1,
          min: 1,
          max: 1
        }
    }
]});

说明:

{
"stages" : [ 
    {
        "$cursor" : {
            "query" : {
                "count" : {
                    "$gt" : 0
                }
            },
            "fields" : {
                "count" : 1,
                "type" : 1,
                "_id" : 0
            },
            "plan" : {
                "cursor" : "BtreeCursor count_1",
                "isMultiKey" : false,
                "scanAndOrder" : false,
                "indexBounds" : {
                    "count" : [ 
                        [ 
                            0, 
                            Infinity
                        ]
                    ]
                },
                "allPlans" : [ 
                    {
                        "cursor" : "BtreeCursor count_1",
                        "isMultiKey" : false,
                        "scanAndOrder" : false,
                        "indexBounds" : {
                            "count" : [ 
                                [ 
                                    0, 
                                    Infinity
                                ]
                            ]
                        }
                    }
                ]
            }
        }
    }, 
    {
        "$group" : {
            "_id" : "$type",
            "sum" : {
                "$sum" : "$count"
            },
            "count" : {
                "$sum" : {
                    "$const" : 1
                }
            },
            "min" : {
                "$min" : "$count"
            },
            "max" : {
                "$max" : "$count"
            }
        }
    }, 
    {
        "$project" : {
            "_id" : true,
            "count" : true,
            "sum" : true,
            "min" : true,
            "max" : true
        }
    }
],
"ok" : 1 }

解释2.4版本中的查询:

{
"cursor" : "BtreeCursor count_1",
"isMultiKey" : false,
"n" : 5710623,
"nscannedObjects" : 5710623,
"nscanned" : 5710623,
"nscannedObjectsAllPlans" : 5710623,
"nscannedAllPlans" : 5710623,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 36445,
"nChunkSkips" : 0,
"millis" : 3556300,
"indexBounds" : {
    "count" : [ 
        [ 
            0, 
            1.797693134862316e+308
        ]
    ]
},
"allPlans" : [ 
    {
        "cursor" : "BtreeCursor count_1",
        "n" : 5710623,
        "nscannedObjects" : 5710623,
        "nscanned" : 5710623,
        "indexBounds" : {
            "count" : [ 
                [ 
                    0, 
                    1.797693134862316e+308
                ]
            ]
        }
    }
],
"oldPlan" : {
    "cursor" : "BtreeCursor count_1",
    "indexBounds" : {
        "count" : [ 
            [ 
                0, 
                1.797693134862316e+308
            ]
        ]
    }
}}

解释2.6.11版本中的查询

{
"cursor" : "BtreeCursor count_1",
"isMultiKey" : false,
"n" : 5710623,
"nscannedObjects" : 5710623,
"nscanned" : 5710623,
"nscannedObjectsAllPlans" : 5710623,
"nscannedAllPlans" : 5710623,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 2585642,
"nChunkSkips" : 0,
"millis" : 17121523,
"indexBounds" : {
    "count" : [ 
        [ 
            0, 
            Infinity
        ]
    ]
},
"allPlans" : [ 
    {
        "cursor" : "BtreeCursor count_1",
        "isMultiKey" : false,
        "n" : 5710623,
        "nscannedObjects" : 5710623,
        "nscanned" : 5710623,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "count" : [ 
                [ 
                    0, 
                    Infinity
                ]
            ]
        }
    }
],
"filterSet" : false,
"stats" : {
    "type" : "FETCH",
    "works" : 8293430,
    "yields" : 2585642,
    "unyields" : 2585642,
    "invalidates" : 2583,
    "advanced" : 5710623,
    "needTime" : 0,
    "needFetch" : 2580241,
    "isEOF" : 1,
    "alreadyHasObj" : 0,
    "forcedFetches" : 0,
    "matchTested" : 0,
    "children" : [ 
        {
            "type" : "IXSCAN",
            "works" : 5710623,
            "yields" : 2585642,
            "unyields" : 2585642,
            "invalidates" : 2583,
            "advanced" : 5710623,
            "needTime" : 0,
            "needFetch" : 0,
            "isEOF" : 1,
            "keyPattern" : "{ count: 1 }",
            "isMultiKey" : 0,
            "boundsVerbose" : "field #0['count']: (0.0, inf.0]",
            "yieldMovedCursor" : 0,
            "dupsTested" : 0,
            "dupsDropped" : 0,
            "seenInvalidated" : 0,
            "matchTested" : 0,
            "keysExamined" : 5710623,
            "children" : []
        }
    ]
}}

0 个答案:

没有答案