MongoDB_09——执行计划
索引的执行计划
-
1:分析查询性能(Analyze Query Performance)通常使用执行计划(解释计划,explain Plan)来查询的情况,如查询耗费的时间,是否基于索引查询等.
-
那么,通常,我们想知道,建立的索引是否有效,效果如何,都需要通过执行计划查看。
语法: db.collection.find(query,options).explain(options) -------------------------------------------------------------------- #根据userid查询数据的情况 db.comment.find({userid:"1003"}).explain() 执行: > db.comment.find({userid:"1003"}) { "_id" : "3", "articleid" : "100003", "content" : "我爱你,绩憨憨3", "userid" : "1003", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-28T14:45:54.235Z"), "likenum" : 10, "state" : null } > db.comment.find({userid:"1003"}).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "comment.comment", "indexFilterSet" : false, "parsedQuery" : { "userid" : { "$eq" : "1003" } }, "queryHash" : "37A12FC3", "planCacheKey" : "37A12FC3", "winningPlan" : { "stage" : "COLLSCAN", #集合扫描,全部扫描 "filter" : { "userid" : { "$eq" : "1003" } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "LAPTOP-ZJ", "port" : 27017, "version" : "4.2.3", "gitVersion" : "6874650b362138df74be53d366bbefc321ea32d4" }, "ok" : 1 } #加上索引,看效果 db.comment.createIndex({userid:1}) > db.comment.createIndex({userid:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.comment.find({userid:"1003"}).explain({}) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "comment.comment", "indexFilterSet" : false, "parsedQuery" : { "userid" : { "$eq" : "1003" } }, "winningPlan" : { "stage" : "FETCH", #这里是FETCH,拉取,有用了索引 "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "userid" : 1 }, "indexName" : "userid_1", "isMultiKey" : false, "multiKeyPaths" : { "userid" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "userid" : [ "[\"1003\", \"1003\"]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 3, "totalKeysExamined" : 1, "totalDocsExamined" : 1, "executionStages" : { "stage" : "FETCH", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 1, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "userid" : 1 }, "indexName" : "userid_1", "isMultiKey" : false, "multiKeyPaths" : { "userid" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "userid" : [ "[\"1003\", \"1003\"]" ] }, "keysExamined" : 1, "seeks" : 1, "dupsTested" : 0, "dupsDropped" : 0 } }, "allPlansExecution" : [ ] }, "serverInfo" : { "host" : "LAPTOP-ZJ", "port" : 27017, "version" : "4.2.3", "gitVersion" : "6874650b362138df74be53d366bbefc321ea32d4" }, "ok" : 1 } #先通过IXSCAN,再FETCH