一、explain(),语句分析工具
MongoDB 3.0之后,explain的返回与使用方法与之前版本有了很大的变化,介于3.0之后的优秀特色和我们目前所使用给的是3.0.7版本,本文仅针对MongoDB 3.0+的explain进行讨论。3.0+的explain有三种模式,分别是:queryPlanner、executionStats、allPlansExecution。现实开发中,常用的是executionStats模式,主要分析这种模式。
给这个person集合创建age键的索引:db.person.createIndex({"age":1})
db.getCollection('person').find({"age":{"$lte":2000}}).explain("executionStats")
{"queryPlanner": {"plannerVersion" : 1,"namespace" : "personmap.person","indexFilterSet" : false,"parsedQuery": {"age": {"$lte" : 2000.0}
},"winningPlan": {"stage" : "FETCH","inputStage": {"stage" : "IXSCAN","keyPattern": {"age" : 1.0},"indexName" : "age_1","isMultiKey" : false,"direction" : "forward","indexBounds": {"age": ["[-1.#INF, 2000.0]"]
}
}
},"rejectedPlans": []
},"executionStats": {"executionSuccess" : true,"nReturned" : 2001,"executionTimeMillis" : 143,"totalKeysExamined" : 2001,"totalDocsExamined" : 2001,"executionStages": {"stage" : "FETCH","nReturned" : 2001,"executionTimeMillisEstimate" : 0,"works" : 2002,"advanced" : 2001,"needTime" : 0,"needFetch" : 0,"saveState" : 16,"restoreState" : 16,"isEOF" : 1,"invalidates" : 0,"docsExamined" : 2001,"alreadyHasObj" : 0,"inputStage": {"stage" : "IXSCAN","nReturned" : 2001,"executionTimeMillisEstimate" : 0,"works" : 2002,"advanced