我在该模式中的文章中有文档:
{
"schedule": {
"day_of_week": {
"0": {"hour": {"3": [60], "6": [45, 60], "9": [60]}},
"2": {"hour": {"2": [60], "3": [45, 60], "5": [60]}},
"3": {"hour": {"1": [15, 45], "4": [30], "5": [60], "6": [30, 60], "7": [45]}},
"6": {"hour": {"1": [30], "2": [15, 45, 60], "5": [30], "9": [15, 45]}}
}
}
}
我有查询要查找这些文件:
db.my_coll.find({"schedule.day_of_week.0.hour.3":60})
我应该创建哪些索引来优化我的查询?
我已经创建了这些索引:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.my_coll"
},
{
"v" : 1,
"key" : {
"schedule.day_of_week.0.hour" : 1
},
"name" : "schedule.day_of_week.0.hour_1",
"ns" : "db.my_coll"
},
{
"v" : 1,
"key" : {
"schedule.day_of_week.0" : 1
},
"name" : "schedule.day_of_week.0_1",
"ns" : "db.my_coll"
},
{
"v" : 1,
"key" : {
"schedule.day_of_week" : 1
},
"name" : "schedule.day_of_week_1",
"ns" : "db.my_coll"
},
{
"v" : 1,
"key" : {
"schedule" : 1
},
"name" : "schedule_1",
"ns" : "db.my_coll"
}
但它没有任何效果,查询仍在使用COLLSCAN而不使用索引。
db.my_coll.explain(1).find的输出({" schedule.day_of_week.0.hour.3":60})
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 64,
"nscanned" : 64,
"nscannedObjectsAllPlans" : 64,
"nscannedAllPlans" : 64,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"allPlans" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 64,
"nscanned" : 64,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0
}
],
"server" : "server:27017",
"filterSet" : false,
"stats" : {
"type" : "COLLSCAN",
"works" : 66,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 2,
"needTime" : 63,
"needFetch" : 0,
"isEOF" : 1,
"docsTested" : 64,
"children" : [ ]
}
}
收集64份文件