mongodb v3.0.7中$ where子句的性能问题

时间:2016-03-30 16:13:38

标签: mongodb mongodb-query

我收集了104444219条记录,当我执行完全扫描收集时,查询需要大约38038毫秒

db.ORDER_MASTER.find()

但在使用$where子句

执行查询时
db.ORDER_DETALS.find({ "$where" : "this.GLB_ROOT_ORDER_ID == this.SYS_ORDER_ID"})

此外,我们在GLB_ROOT_ORDER_IDSYS_ORDER_ID上有索引,在同一个集合上,在检查完全扫描的两个查询的解释计划时需要2064391毫秒,但为什么执行时间有差异?

$where条款是否有其他选择?

对于

db.ORDER_MASTER.find({ "$where" : "obj.GLB_ROOT_ORDER_ID == obj.SYS_ORDER_ID"})

解释结果

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "uat_test.ORDER_MASTER",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$where" : "obj.GLB_ROOT_ORDER_ID == obj.SYS_ORDER_ID"
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$where" : "obj.GLB_ROOT_ORDER_ID == obj.SYS_ORDER_ID"
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 93366348,
                "executionTimeMillis" : 2064391,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 104444219,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$where" : "obj.GLB_ROOT_ORDER_ID == obj.SYS_ORDER_ID"
                        },
                        "nReturned" : 93366348,
                        "executionTimeMillisEstimate" : 2007680,
                        "works" : 104444221,
                        "advanced" : 93366348,
                        "needTime" : 11077872,
                        "needFetch" : 0,
                        "saveState" : 816961,
                        "restoreState" : 816961,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 104444219
                }
        },

        "ok" : 1
}

对于

db.ORDER_MASTER.find()

解释结果

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "uat_test.ORDER_MASTER",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [ ]
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$and" : [ ]
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 104444219,
                "executionTimeMillis" : 38038,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 104444219,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$and" : [ ]
                        },
                        "nReturned" : 104444219,
                        "executionTimeMillisEstimate" : 26520,
                        "works" : 104444221,
                        "advanced" : 104444219,
                        "needTime" : 1,
                        "needFetch" : 0,
                        "saveState" : 815970,
                        "restoreState" : 815970,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 104444219
                }
        },

        "ok" : 1
}

0 个答案:

没有答案