Mongodb查询执行时间

时间:2019-04-27 11:58:01

标签: mongodb mongodb-query

我在查询下面花了很多时间

查询

db.saleOrder.find({"currentStatus._id":"147"},{"_id":1}).limit(10).explain("executionStats")

ExecutionStats结果

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "db_erp_tube.saleOrder",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "currentStatus._id" : {
                "$eq" : "147"
            }
        },
        "winningPlan" : {
            "stage" : "LIMIT",
            "limitAmount" : 10,
            "inputStage" : {
                "stage" : "PROJECTION",
                "transformBy" : {
                    "_id" : 1
                },
                "inputStage" : {
                    "stage" : "COLLSCAN",
                    "filter" : {
                        "currentStatus._id" : {
                            "$eq" : "147"
                        }
                    },
                    "direction" : "forward"
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 10,
        "executionTimeMillis" : 8673,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 3458482,
        "executionStages" : {
            "stage" : "LIMIT",
            "nReturned" : 10,
            "executionTimeMillisEstimate" : 8460,
            "works" : 3458484,
            "advanced" : 10,
            "needTime" : 3458473,
            "needYield" : 0,
            "saveState" : 27019,
            "restoreState" : 27019,
            "isEOF" : 1,
            "invalidates" : 0,
            "limitAmount" : 10,
            "inputStage" : {
                "stage" : "PROJECTION",
                "nReturned" : 10,
                "executionTimeMillisEstimate" : 8450,
                "works" : 3458483,
                "advanced" : 10,
                "needTime" : 3458473,
                "needYield" : 0,
                "saveState" : 27019,
                "restoreState" : 27019,
                "isEOF" : 0,
                "invalidates" : 0,
                "transformBy" : {
                    "_id" : 1
                },
                "inputStage" : {
                    "stage" : "COLLSCAN",
                    "filter" : {
                        "currentStatus._id" : {
                            "$eq" : "147"
                        }
                    },
                    "nReturned" : 10,
                    "executionTimeMillisEstimate" : 8400,
                    "works" : 3458483,
                    "advanced" : 10,
                    "needTime" : 3458473,
                    "needYield" : 0,
                    "saveState" : 27019,
                    "restoreState" : 27019,
                    "isEOF" : 0,
                    "invalidates" : 0,
                    "direction" : "forward",
                    "docsExamined" : 3458482
                }
            }
        }
    },
    "serverInfo" : {
        "host" : "172.16.109",
        "port" : 27017,
        "version" : "4.0.0",
        "gitVersion" : "3b07af3d4f471ae89e8186d33bbb1d5259597d51"
    },
    "ok" : 1,
    "operationTime" : Timestamp(1556365275, 114),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1556365275, 114),
        "signature" : {
            "hash" : BinData(0,"ppu91nKmeiC//+UvdsEbjrBTDLU="),
            "keyId" : NumberLong("6633468944474701825")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

查询执行耗时超过8秒(8673毫秒),因为它必须在返回10个文档之前扫描saleOrder集合中的所有3458482文档。

这在解释输出的过滤器阶段中说明。

main_app.dashboard_index_path

COLLSCAN表示完全收集扫描,

"inputStage" : {
                "stage" : "COLLSCAN",
                "filter" : {
                    "currentStatus._id" : {
                        "$eq" : "147"
                    }
                },

这是返回结果之前扫描的文档数。

 "totalDocsExamined" : 3458482,

这是查询运行的总时间。

按照@the_mahasagar的建议,在currentStatus上建立索引。_id可以大大加快查询速度,请使用以下命令。

 "executionTimeMillis" : 8673,