読者です 読者をやめる 読者になる 読者になる

かまたま日記3

プログラミングメイン、たまに日常

RailsコンソールでMongoDBのexplainをする via MongoMapper

MongoDBの実行計画を見るときにRailsアプリとMongoMapper*1を使ってたらRailsコンソールで実行計画を見れたりします。ppを使わないと見づらいです。

ざっくりした見方

  • winningPlanが今回採用された実行計画、rejectedPlans が採用されなかった計画*2
  • executionStatsが実行ステータス、何件スキャンしたかとか実際ヒットしたのは何件かとか、実行時間とかが見れる

細かい見方とか勘所は以下を参照 mknkisk.github.io

?> pp User.collection.find({email: "kamatama41@example.com"}).explain
{"queryPlanner"=>
  {"plannerVersion"=>1,
   "namespace"=>"example-db.users",
   "indexFilterSet"=>false,
   "parsedQuery"=>{"email"=>{"$eq"=>"kamatama41@example.com"}},
   "winningPlan"=>
    {"stage"=>"FETCH",
     "inputStage"=>
      {"stage"=>"IXSCAN",
       "keyPattern"=>{"email"=>1, "_id"=>1},
       "indexName"=>"email_1__id_1",
       "isMultiKey"=>false,
       "direction"=>"forward",
       "indexBounds"=>
        {"email"=>
          ["[\"kamatama41@example.com\", \"kamatama41@example.com\"]"],
         "_id"=>["[MinKey, MaxKey]"]}}},
   "rejectedPlans"=>[]},
 "executionStats"=>
  {"executionSuccess"=>true,
   "nReturned"=>1,
   "executionTimeMillis"=>0,
   "totalKeysExamined"=>1,
   "totalDocsExamined"=>1,
   "executionStages"=>
    {"stage"=>"FETCH",
     "nReturned"=>1,
     "executionTimeMillisEstimate"=>0,
     "works"=>2,
     "advanced"=>1,
     "needTime"=>0,
     "needFetch"=>0,
     "saveState"=>0,
     "restoreState"=>0,
     "isEOF"=>1,
     "invalidates"=>0,
     "docsExamined"=>1,
     "alreadyHasObj"=>0,
     "inputStage"=>
      {"stage"=>"IXSCAN",
       "nReturned"=>1,
       "executionTimeMillisEstimate"=>0,
       "works"=>2,
       "advanced"=>1,
       "needTime"=>0,
       "needFetch"=>0,
       "saveState"=>0,
       "restoreState"=>0,
       "isEOF"=>1,
       "invalidates"=>0,
       "keyPattern"=>{"email"=>1, "_id"=>1},
       "indexName"=>"email_1__id_1",
       "isMultiKey"=>false,
       "direction"=>"forward",
       "indexBounds"=>
        {"email"=>
          ["[\"kamatama41@example.com\", \"kamatama41@example.com\"]"],
         "_id"=>["[MinKey, MaxKey]"]},
       "keysExamined"=>1,
       "dupsTested"=>0,
       "dupsDropped"=>0,
       "seenInvalidated"=>0,
       "matchTested"=>0}},
   "allPlansExecution"=>[]},
 "serverInfo"=>
  {"host"=>"example-db.com",
   "port"=> 27017,
   "version"=>"3.0.5",
   "gitVersion"=>"8bc4ae20708dbb493cb09338d9e7be6698e4a3a3"},
 "executionTimeMillis"=>0}

*1:というかMongoDriver

*2:このクエリに関しては無し