나는 일부 샘플 데이터 세트 내 지역에 몇 가지 테스트를하고 난 최선의 옵션은 다음과 같은 필드에 인덱스를 생성 "category_1_score_1_username_1"
인덱스는 당신에게 이렇게 덮여 쿼리 및 제공을 만들 수있을 거라고 생각 문서는 색인에서 직접 리턴됩니다. 그것은 덮여 쿼리를 반환하기 위해 이제 약간 쿼리를 수정할 수 있습니다
> db.usr.getIndexes();
{
"v" : 1,
"key" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"name" : "category_1_score_1_username_1",
"ns" : "test.usr"
}
]
>
:
내 분석 아래
> db.usr.find();
{ "_id" : ObjectId("57bd20630744bd376277a795"), "username" : "Bob", "score" : 10, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a796"), "username" : "John", "score" : 32, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a797"), "username" : "Bob1", "score" : 2, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a798"), "username" : "John2", "score" : 132, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a799"), "username" : "Bob3", "score" : 20, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79a"), "username" : "John4", "score" : 132, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79b"), "username" : "Bob5", "score" : 22, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79c"), "username" : "John6", "score" : 322, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79d"), "username" : "Bob7", "score" : 232, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a79e"), "username" : "John8", "score" : 3112, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a79f"), "username" : "Bob4", "score" : 222, "category" : "leastDeaths" }
{ "_id" : ObjectId("57bd20630744bd376277a7a0"), "username" : "John22", "score" :, "category" : "mostLikes" }
{ "_id" : ObjectId("57bd20630744bd376277a7a1"), "username" : "Bob33", "score" : 2111, "category" : "leastDeaths" }
인덱스를 찾을 수 있습니다.
db.usr.find({"category":"mostLikes"},{"_id":0,"score":-1,"category":1,"username":1}).sort({"score":1}).explain("executionStats");
Output of Execution Stats:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.usr",
"indexFilterSet" : false,
"parsedQuery" : {
"category" : {
"$eq" : "mostLikes"
}
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"_id" : 0,
"score" : -1,
"category" : 1,
"username" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"indexName" : "category_1_score_1_username_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"category" : [
"[\"mostLikes\", \"mostLikes\"]"
],
"score" : [
"[MinKey, MaxKey]"
],
"username" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 7,
"executionTimeMillis" : 0,
"totalKeysExamined" : 7,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 7,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 7,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"_id" : 0,
"score" : -1,
"category" : 1,
"username" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 7,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 7,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"category" : 1,
"score" : 1,
"username" : 1
},
"indexName" : "category_1_score_1_username_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"category" : [
"[\"mostLikes\", \"mostLikes\"]"
],
"score" : [
"[MinKey, MaxKey]"
],
"username" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 7,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "L4156409",
"port" : 27017,
"version" : "3.2.5",
"gitVersion" : "34e65e5383f7ea1726332cb175b73077ec4a1b02"
},
"ok" : 1
}
>
따라서 당신은 레코드가 인덱스에서 직접 인출하는 동안 스캔 한 문서의 어떤이 0 출력을 볼 수 있습니다. 따라서이 인덱스를 선택하는 것이 첫 번째 쿼리에 대한 최선의 방법 일 것입니다.
두 번째 쿼리의 경우 사용자 이름 필드에 인덱스를 만드는 것이 간단해야하며 두 번째 쿼리가 해결되어야합니다.
HTH.
안녕하세요 @RainingChain이 도움이되고 확신한다고 생각하면 허용되는 답변을 표시해주세요. – user641887