成都创新互联网站制作重庆分公司

mongodbExplainandIndex-创新互联

Explain and Index

创新互联建站成立于2013年,先为涟水等服务建站,涟水等地企业,进行企业商务咨询服务。为涟水企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

    explain有三种模式,分别是:queryPlanner、executionStats、allPlansExecution。现实开发中,常用executionStats模式,本例分析这种模式。

> db.createCollection("person")

{ "ok" : 1 }

> for(var i=0;i<2000000;i++) {

... db.person.insert({"name":"ryan"+i,"age":i})

... }

WriteResult({ "nInserted" : 1 })

> db.person.find({"age":{"$lte":2000}}).explain("executionStats")

{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.person", "indexFilterSet" : false, "parsedQuery" : { "age" : { "$lte" : 2000 } }, "winningPlan" : { "stage" : "COLLSCAN",                "COLLSCAN"表示全表扫描 "filter" : { "age" : { "$lte" : 2000 } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 2001,                    表示查询返回的条目数 "executionTimeMillis" : 741,           表示执行语句的执行时间 "totalKeysExamined" : 0, "totalDocsExamined" : 2000000,         表示 文档扫描的条目数 "executionStages" : { "stage" : "COLLSCAN", "filter" : { "age" : { "$lte" : 2000 } }, "nReturned" : 2001, "executionTimeMillisEstimate" : 530,    表示整体的查询时间 "works" : 2000002, "advanced" : 2001, "needTime" : 1998000, "needYield" : 0, "saveState" : 15625, "restoreState" : 15625, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 2000000 } }, "serverInfo" : { "host" : "meteor.yeecall.com", "port" : 27027, "version" : "3.2.8", "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0" }, "ok" : 1 }

> db.person.ensureIndex({age:1})    创建索引,“1”:表示按照age进行升序,“-1”:表示按照age进行降序

                         创建索引时ensureIndex({field:1},{name:"indexName"}) 可以指定索引的名称

                             也可以后台创建索引: db.collection.ensureIndex({name:-1},{background:true})

{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }

> db.person.find({"age":{"$lte":2001}}).explain("executionStats")

{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.person",    返回所查询的表 "indexFilterSet" : false,       针对该query是否有indexfilter "parsedQuery" : { "age" : { "$lte" : 2001 } }, "winningPlan" : {               查询优化器针对该query所返回的最优执行计划的详细内容 "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 1, "direction" : "forward", "indexBounds" : { "age" : [ "[-inf.0, 2001.0]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 2002,                    表示本次查询返回的条目数 "executionTimeMillis" : 28,            表示本次query整体查询的时间 "totalKeysExamined" : 2002,            表示索引扫描的条目数 "totalDocsExamined" : 2002,            表示文档扫描的条目数 "executionStages" : { "stage" : "FETCH", "nReturned" : 2002, "executionTimeMillisEstimate" : 10,    表示本次查询根据index检索document获得结果数据的时间 "works" : 2003, "advanced" : 2002, "needTime" : 0, "needYield" : 0, "saveState" : 15, "restoreState" : 15, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 2002, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 2002, "executionTimeMillisEstimate" : 0,    表示该查询扫描2002行index所用的时间 "works" : 2003, "advanced" : 2002, "needTime" : 0, "needYield" : 0, "saveState" : 15, "restoreState" : 15, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 1, "direction" : "forward", "indexBounds" : { "age" : [ "[-inf.0, 2001.0]" ] }, "keysExamined" : 2002, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0 } } }, "serverInfo" : { "host" : "meteor.yeecall.com", "port" : 27027, "version" : "3.2.8", "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0" }, "ok" : 1 }

>

影响 totalKeysExamined 和 totalDocsExamined 的是 stage 类型。

类型列举如下:

  COLLSCAN:全表扫描

  IXSCAN:索引扫描

  FETCH:根据索引去检索指定document

  SHARD_MERGE:将各个分片返回数据进行merge

  SORT:表明在内存中进行了排序

  LIMIT:使用limit限制返回数

  SKIP:使用skip进行跳过

  IDHACK:针对_id进行查询

  SHARDING_FILTER:通过mongos对分片数据进行查询

  COUNT:利用db.coll.explain().count()之类进行count运算

  COUNTSCAN:count不使用Index进行count时的stage返回

  COUNT_SCAN:count使用了Index进行count时的stage返回

  SUBPLA:未使用到索引的$or查询的stage返回

  TEXT:使用全文索引进行查询时候的stage返回

  PROJECTION:限定返回字段时候stage的返回

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:mongodbExplainandIndex-创新互联
文章链接:http://cxhlcq.com/article/hggsc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部