Mongo DB查询时间超出预期

时间:2018-06-25 10:12:05

标签: mongodb mongodb-query

我正在运行版本3.4的独立mongodb服务器。我在集合中使用以下查询,该查询包含大约180万个文档,其中大约100万个文档处于“已存档”状态。

db.tender_listing.find({ "tender_id" : { "$gt" : "d"} , "workflow_status" : { "$in" : [ "ARCHIVED"]}}).limit(4000).sort({tender_id:1}).hint({workflow_status:1, tender_id:1}).explain('executionStats')

每个查询阶段的executionTimeMillisEstimate不超过100ms,但是总executionTimeMillis为30992。

对于什么操作查询,这要花很多时间?另外,我该如何优化呢?

以下是输出

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "ofbTenders.tender_listing",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "workflow_status" : {
                        "$eq" : "ARCHIVED"
                    }
                },
                {
                    "tender_id" : {
                        "$gt" : "d"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "LIMIT",
            "limitAmount" : 4000,
            "inputStage" : {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "workflow_status" : 1,
                        "tender_id" : 1
                    },
                    "indexName" : "workflow_status_1_tender_id_1",
                    "isMultiKey" : false,
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "workflow_status" : [
                            "[\"ARCHIVED\", \"ARCHIVED\"]"
                        ],
                        "tender_id" : [
                            "(\"d\", {})"
                        ]
                    }
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 4000,
        "executionTimeMillis" : 30992,
        "totalKeysExamined" : 4000,
        "totalDocsExamined" : 4000,
        "executionStages" : {
            "stage" : "LIMIT",
            "nReturned" : 4000,
            "executionTimeMillisEstimate" : 90,
            "works" : 6129,
            "advanced" : 4000,
            "needTime" : 0,
            "needYield" : 2128,
            "saveState" : 2128,
            "restoreState" : 2128,
            "isEOF" : 1,
            "invalidates" : 0,
            "limitAmount" : 4000,
            "inputStage" : {
                "stage" : "FETCH",
                "nReturned" : 4000,
                "executionTimeMillisEstimate" : 80,
                "works" : 6128,
                "advanced" : 4000,
                "needTime" : 0,
                "needYield" : 2128,
                "saveState" : 2128,
                "restoreState" : 2128,
                "isEOF" : 0,
                "invalidates" : 0,
                "docsExamined" : 4000,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 4000,
                    "executionTimeMillisEstimate" : 10,
                    "works" : 4000,
                    "advanced" : 4000,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 2128,
                    "restoreState" : 2128,
                    "isEOF" : 0,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "workflow_status" : 1,
                        "tender_id" : 1
                    },
                    "indexName" : "workflow_status_1_tender_id_1",
                    "isMultiKey" : false,
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "workflow_status" : [
                            "[\"ARCHIVED\", \"ARCHIVED\"]"
                        ],
                        "tender_id" : [
                            "(\"d\", {})"
                        ]
                    },
                    "keysExamined" : 4000,
                    "seeks" : 1,
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0
                }
            }
        }
    },
    "serverInfo" : {
        "host" : "ofb59-Latitude-3450",
        "port" : 27017,
        "version" : "3.4.4",
        "gitVersion" : "888390515874a9debd1b6c5d36559ca86b44babd"
    },
    "ok" : 1
}

1 个答案:

答案 0 :(得分:0)

我不确定,但是您可以尝试以下选项。 1)您可以直接给“ workflow_status”:“ ARCHIVED”而不是使用$ in作为工作流 2)更改字段顺序,先查找工作流程状态,然后再查找bid_id。 3)运行没有提示的查询执行计划。让MongoDB决定使用哪个索引。