改进mongodb查询的执行

时间:2016-09-10 17:37:08

标签: mongodb mongodb-query database

到目前为止,我有一个包含19,348,086个文档的Action集合。 Action在'rule'字段中有一个索引。

当我尝试使用这样的给定规则获取文档时:

 db.actions.find({ rule: ObjectId("57b47749e823f9a804df7c86")}).explain("executionStats");

检索18,904,044个文档需要大约40秒。

FYI db.actions.find({}).explain("executionStats");在16秒内执行。

你觉得这样吗?我该怎么做才能缩短执行时间?

db.actions.find({ rule: ObjectId("57b47749e823f9a804df7c86")}).explain("executionStats");

结果:

{  
"queryPlanner":{  
    "plannerVersion":1,
    "namespace":"test.actions",
    "indexFilterSet":false,
    "parsedQuery":{  
        "rule":{  
            "$eq":ObjectId("57b47749e823f9a804df7c86")
        }
    },
    "winningPlan":{  
        "stage":"FETCH",
        "inputStage":{  
            "stage":"IXSCAN",
            "keyPattern":{  
                "rule":-1
            },
            "indexName":"rule_-1",
            "isMultiKey":false,
            "isUnique":false,
            "isSparse":false,
            "isPartial":false,
            "indexVersion":1,
            "direction":"forward",
            "indexBounds":{  
                "rule":[  
                    "[ObjectId('57b47749e823f9a804df7c86'), ObjectId('57b47749e823f9a804df7c86')]"
                ]
            }
        }
    },
    "rejectedPlans":[  
        . 
  . 
  .
    ]
},
"executionStats":{  
    "executionSuccess":true,
    "nReturned":18904044,
    "executionTimeMillis":39937,
    "totalKeysExamined":18904044,
    "totalDocsExamined":18904044,
    "executionStages":{  
        "stage":"FETCH",
        "nReturned":18904044,
        "executionTimeMillisEstimate":7370,
        "works":18904045,
        "advanced":18904044,
        "needTime":0,
        "needYield":0,
        "saveState":147692,
        "restoreState":147692,
        "isEOF":1,
        "invalidates":0,
        "docsExamined":18904044,
        "alreadyHasObj":0,
        "inputStage":{  
            "stage":"IXSCAN",
            "nReturned":18904044,
            "executionTimeMillisEstimate":2040,
            "works":18904045,
            "advanced":18904044,
            "needTime":0,
            "needYield":0,
            "saveState":147692,
            "restoreState":147692,
            "isEOF":1,
            "invalidates":0,
            "keyPattern":{  
                "rule":-1
            },
            "indexName":"rule_-1",
            "isMultiKey":false,
            "isUnique":false,
            "isSparse":false,
            "isPartial":false,
            "indexVersion":1,
            "direction":"forward",
            "indexBounds":{  
                "rule":[  
                    "[ObjectId('57b47749e823f9a804df7c86'), ObjectId('57b47749e823f9a804df7c86')]"
                ]
            },
            "keysExamined":18904044,
            "dupsTested":0,
            "dupsDropped":0,
            "seenInvalidated":0
        }
    }
},
"serverInfo":{  
    "host":"MacBook-Pro-de-Adrien.local",
    "port":27017,
    "version":"3.2.0",
    "gitVersion":"45d947729a0315accb6d4f15a6b06be6d9c19fe7"
},
"ok":1
}

0 个答案:

没有答案