MongoDBのドキュメントには次のように書かれています:
... クエリ オプティマイザーは、別のクエリ プランをときどき実行し、クエリの種類ごとに応答時間が最適なプランを選択することで、経験的にインデックスを選択します...
MongoDB 2.0.2 で私が経験している問題は、Mongo が特定のクエリに対して常に別のクエリ プランを実行しているように見えることです。
これによりプロセス全体が非常に遅くなるだけでなく (explain({verbose:true}) は 'allPlans' 配列に 10 ~ 15 個の追加テストを示します)、Mongo は最適なインデックスも選択しません。そのクエリに対して正確に作成したインデックスを使用してヒント コマンドを提供すると、はるかに高速な結果が得られます。
次に例を示します。
> db.posts.find({"project.id" : 2, "project.sections" : 1, "reading" : {"$in" : [0,1]}, "publicate" : 1}).sort({"date":-1}).explain()
"cursor" : "BtreeCursor project.id_1_reading_1_publicate_1_public_1_last_comment_time_1 multi",
"nscanned" : 13347,
"nscannedObjects" : 13346,
"n" : 7619,
"millis" : 2371,
"nYields" : 7,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
verbose:true を指定した同じクエリでは、これらのインデックスが「allPlans」配列に表示されます。
"BtreeCursor reading_1_publicate_1_last_comment_time_1 multi",
"BtreeCursor date_-1_community_id_1",
"BtreeCursor project.id_1_reading_1_publicate_1_public_1_last_comment_time_1 multi",
"BtreeCursor project.id_1_reading_1_publicate_1_last_comment_time_1 multi",
"BtreeCursor project.id_1_project.sections_1_reading_1_publicate_1_public_1_date_-1 multi",
"BtreeCursor timeline_main_liked_all",
"BtreeCursor timeline_main_liked_user",
"BtreeCursor timeline_main_commented_all",
"BtreeCursor timeline_main_commented_user",
"BtreeCursor project.id_1_reading_1_publicate_1_public_1_project.sections_1 multi",
"BtreeCursor publicate_1_public_1_date_-1_reading_1_type_1 multi",
"BtreeCursor publicate_1_timeline_visibility_all_1_short.formatted_1_short.show_1_short.commercial_1_date_-1",
"BtreeCursor publicate_1_timeline_visibility_user_1_short.formatted_1_short.show_1_short.commercial_1_date_-1",
"BasicCursor",
- Mongo がこれらのインデックス チェックを常に実行しないようにするにはどうすればよいですか?
- Mongo に実際の最適なインデックスを使用させて固執させるにはどうすればよいですか?