首页 > 解决方案 > Mongo db 查询时间超出预期

问题描述

我正在运行 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 不超过 100 毫秒,但总的 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
}

标签: mongodbmongodb-query

解决方案


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


推荐阅读