MongoDBでは、cursor.explain()を使用できます メソッドまたはdb.collection.explain() クエリがインデックスを使用するかどうかを判断するメソッド。
これらのメソッドを使用すると、クエリのクエリプランを表示できます。これには、インデックスを使用しているかどうかが含まれます。
例
petsというコレクションがあるとします。 、および次のドキュメントが含まれています:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
{ "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 }
{ "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }
そして、そのnameに次のインデックスを作成するとします。 フィールド:
db.pets.createIndex( { "name" : 1 } ) 次のクエリを実行すると、そのインデックスが使用されます。
db.pets.find( { "name" : "Scratch" } ) 結果:
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } ただし、インデックスを使用したかどうかは、結果を見ただけではわかりません。
ここでexplain() メソッドが入ります。explain()を追加できます クエリプランを取得するには、クエリの最後まで移動します。インデックスを使用したかどうかがわかります。
db.pets.find( { "name" : "Scratch" } ).explain() 結果:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "PetHouse.pets",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "Scratch"
}
},
"queryHash" : "01AEE5EC",
"planCacheKey" : "4C5AEA2C",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"name" : 1
},
"indexName" : "name_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"name" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"Scratch\", \"Scratch\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"ok" : 1
}
IXSCANと書かれている部分でわかります クエリがインデックススキャンを使用して結果を生成すること。
対照的に、インデックスに含まれていないクエリに対して同じことを行うと、コレクションスキャン(COLLSCAN)が使用されていることがわかります。 ):
db.pets.find( { "type" : "Dog" } ).explain() 結果:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "PetHouse.pets",
"indexFilterSet" : false,
"parsedQuery" : {
"type" : {
"$eq" : "Dog"
}
},
"queryHash" : "2A1623C7",
"planCacheKey" : "2A1623C7",
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"type" : {
"$eq" : "Dog"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"ok" : 1
} db.collection.explain() 方法
db.collection.explain() メソッドはcursor.explain()に似ています 、ただしdb.collection.explain()を使用する場合を除く 、追加のクエリ修飾子をクエリにチェーンできます(find()の後 方法)。
私たちの目的のために、私たちは次のことを行うことができます:
db.pets.explain().find( { "name": "Scratch" } ) 結果:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "PetHouse.pets",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "Scratch"
}
},
"queryHash" : "01AEE5EC",
"planCacheKey" : "4C5AEA2C",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"name" : 1
},
"indexName" : "name_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"name" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"Scratch\", \"Scratch\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"ok" : 1
}
次のコマンドを実行して、このメソッドで使用できるクエリ修飾子のリストを取得できます。
db.collection.explain().find().help()