AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

Explain プラン 結果の解釈

explain 結果を使用して、クエリに関する次の情報を判別できます。

  • クエリの完了までにかかった時間

  • クエリでインデックスが使用されているかどうか

  • クエリを実行するためにスキャンされたドキュメントとインデックス キーの数

注意

クエリの explain プランの結果は、MongoDB のバージョンによって変更される可能性があります。

cursor.explain("executionStats") メソッドと db.collection.explain("executionStats") メソッドは、クエリのパフォーマンスに関する統計を提供します。これらの統計は、クエリでインデックスが使用されているかどうか、またどのように使用されているかを測定するために役立ちます。詳細についてはdb.collection.explain()を参照してください。

以下のドキュメントを持つ inventory コレクションを考えてみましょう。

db.inventory.insertMany ( [ { _id: 1, item: "f1", type: "food", quantity: 500 }, { _id: 2, item: "f2", type: "food", quantity: 100 }, { _id: 3, item: "p1", type: "paper", quantity: 200 }, { _id: 4, item: "p2", type: "paper", quantity: 150 }, { _id: 5, item: "f3", type: "food", quantity: 300 }, { _id: 6, item: "t1", type: "toys", quantity: 500 }, { _id: 7, item: "a1", type: "apparel", quantity: 250 }, { _id: 8, item: "a2", type: "apparel", quantity: 400 }, { _id: 9, item: "t2", type: "toys", quantity: 50 }, { _id: 10, item: "f4", type: "food", quantity: 75 }] )

次のクエリは、quantity フィールドの値が 100 から 200 までの間のドキュメントを取得します。

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )

このクエリでは次のドキュメントが返されます。

{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }{ "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }{ "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }

選択したクエリ プランを表示するには、 cursor.explain("executionStats") カーソル メソッドを find コマンドの後に続けて記述します。

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } }).explain("executionStats")

explain() は、次の結果を返します。

{ queryPlanner: { ... winningPlan: { queryPlan: { stage: 'COLLSCAN', ... } } }, executionStats: { executionSuccess: true, nReturned: 3, executionTimeMillis: 0, totalKeysExamined: 0, totalDocsExamined: 10, executionStages: { stage: 'COLLSCAN', ... }, ... }, ...}
  • queryPlanner.winningPlan.queryPlan.stage はコレクションスキャンを示す COLLSCAN を表示します。

    コレクションスキャンは、 mongod が結果を特定するためにコレクション全体をドキュメントごとにスキャンする必要があったことを示しています。これは通常、コストのかかる操作であり、クエリが遅くなる可能性があります。

  • executionStats.nReturned は 3 を表示し、選出されたクエリプランが 3 つのドキュメントを返すことを示します。

  • executionStats.totalKeysExamined は、このクエリがインデックスを使用していないことを示す0 を表示します。

  • executionStats.totalDocsExamined は 10 を表示し、3 つの一致するドキュメントを検索するために MongoDB が 10 個のドキュメント(つまり、コレクション内のすべてのドキュメント)をスキャンしなければならなかったことを示します。

一致するドキュメントの数と検査されたドキュメントの数との差から、クエリの効率を向上させるためには、インデックスを使用すると効果があると考えられます。

quantity フィールドのクエリをサポートするには、quantity フィールドにインデックスを追加します。

db.inventory.createIndex( { quantity: 1 } )

クエリプランの統計を表示するには、explain() メソッドを使用します。

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } }).explain("executionStats")

explain() メソッドは次の結果を返します。

{ queryPlanner: { ... winningPlan: { queryPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { quantity: 1 }, ... } } }, rejectedPlans: [ ] }, executionStats: { executionSuccess: true, nReturned: 3, executionTimeMillis: 0, totalKeysExamined: 3, totalDocsExamined: 3, executionStages: { ... }, ... }, ...}
  • queryPlanner.winningPlan.queryPlan.inputStage.stage はインデックスの使用を示すために IXSCAN を表示します。

  • executionStats.nReturned は 3 を表示し、選出されたクエリプランが 3 つのドキュメントを返すことを示します。

  • executionStats.totalKeysExamined は 3 を表示し、MongoDB が 3 つのインデックスエントリをスキャンしたことを示します。検査されたキーの数は、返されたドキュメントの数と一致します。つまり、mongod はインデックスキーを調べるだけで結果を返すことができました。mongod はすべてのドキュメントをスキャンする必要はなく、一致する 3 つのドキュメントのみをメモリに取り込む必要がありました。これにより、クエリが非常に効率的になります。

  • executionStats.totalDocsExamined は 3 を表示し、MongoDB が 3 つのドキュメントをスキャンしたことを示します。

インデックスがない場合、クエリは 10 個のドキュメントのコレクション全体をスキャンして、一致する 3 個のドキュメントを返します。クエリでは各ドキュメント全体をスキャンする必要があり、場合によってはドキュメントをメモリに取り込む必要もあります。その結果、クエリ操作にコストと時間がかかる可能性があります。

インデックスを使用して実行すると、クエリは 3 つのインデックスエントリと 3 つのドキュメントをスキャンして 3 つの一致するドキュメントを返すため、非常に効率的なクエリになりました。

インデックスのパフォーマンスを比較する

複数のインデックスを使用してクエリのパフォーマンスを手動で比較するには、hint() メソッドとexplain() メソッドを組み合わせて使用できます。

次のクエリを考えてみましょう。

db.inventory.find( { quantity: { $gte: 100, $lte: 300 }, type: "food"} )

このクエリでは次のドキュメントが返されます。

{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }{ "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }

クエリをサポートするために、複合インデックスを追加する。複合インデックスでは、フィールドの順序が重要です。

たとえば、次の2つの複合インデックスを追加してください。最初のインデックスは、まずquantityフィールドで並べ替え、次にtypeフィールドで並べ替えます。2番目のインデックスは、まずtypeで並べ替え、次にquantityフィールドで並べ替えます。

db.inventory.createIndex( { quantity: 1, type: 1 } )db.inventory.createIndex( { type: 1, quantity: 1 } )

クエリに対する最初のインデックスの影響を評価してください。

db.inventory.find( { quantity: { $gte: 100, $lte: 300 }, type: "food" }).hint({ quantity: 1, type: 1 }).explain("executionStats")

explain() メソッドは次の出力を返します。

{ queryPlanner: { ... winningPlan: { queryPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { quantity: 1, type: 1 }, ... } } } }, rejectedPlans: [ ] }, executionStats: { executionSuccess: true, nReturned: 2, executionTimeMillis: 0, totalKeysExamined: 5, totalDocsExamined: 2, executionStages: { ... } }, ...}

MongoDB は5個のインデックスキー(executionStats.totalKeysExamined)をスキャンして、2個の一致するドキュメント(executionStats.nReturned)を返しました。

クエリに対する第2インデックスの影響を評価する。

db.inventory.find( { quantity: { $gte: 100, $lte: 300 }, type: "food" }).hint({ type: 1, quantity: 1 }).explain("executionStats")

explain() メソッドは次の出力を返します。

{ queryPlanner: { ... queryPlan: { winningPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { type: 1, quantity: 1 }, ... } } }, rejectedPlans: [ ] }, executionStats: { executionSuccess: true, nReturned: 2, executionTimeMillis: 0, totalKeysExamined: 2, totalDocsExamined: 2, executionStages: { ... } }, ...}

MongoDB は2個のインデックスキー(executionStats.totalKeysExamined)をスキャンして、2個の一致するドキュメント(executionStats.nReturned)を返しました。

したがって、MongoDBサーバーは、複合インデックス { quantity: 1, type: 1 } を使用する場合の5 と比較して、2 index keys をスキャンするだけで、この複合インデックスを使用して一致するドキュメントをすべて見つけることができるため、2番目の複合インデックス { type: 1, quantity: 1 } の方が効率的なインデックスです。