简体   繁体   English

Mongodb 查询执行时间

[英]Mongodb query execution time

I used Query below its take to much time我在下面使用 Query 需要很长时间

Query询问

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

ExecutionStats result 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")
        }
    }
}

The query took more than 8 seconds(8673ms) to execute because it had to scan all 3458482 documents in the saleOrder collection before returning 10 documents.该查询执行时间超过 8 秒(8673 毫秒),因为它必须在返回 10 个文档之前扫描 saleOrder 集合中的所有 3458482 个文档。

This is stated in the filter stage of the explain output.这在解释输出的过滤阶段中有说明。

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

COLLSCAN indicate full collection scan, COLLSCAN 表示全集合扫描,

 "totalDocsExamined" : 3458482,

This is the number of document scanned before an result is returned.这是在返回结果之前扫描的文档数。

 "executionTimeMillis" : 8673,

This is the total time taken for the query to run.这是运行查询所花费的总时间。

As @the_mahasagar suggested, build a index on currentStatus._id can speed up the query a lot, use the command below.正如@the_mahasagar 建议的那样,在 currentStatus._id 上建立索引可以大大加快查询速度,请使用以下命令。

 db.saleOrder.createIndex({ "currentStatus._id": 1})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM