2016-08-24 3 views
1

나는 그런 MongoDB : 어떤 인덱스를 사용해야합니까?

{username:"Bob",score:10,category:"mostLikes"} {username:"John",score:32,category:"mostLikes"} {username:"Bob",score:2,category:"leastDeaths"}

목표는 특정 카테고리 (분류) 100 대를 가져올 것입니다

같은 문서를 포함하는 최고 점수 MongoDB의 표를 얻었다.

중요 : 특정 최고점 카테고리는 오름차순 (더 낮은 것이 예 : leastDeaths)이고 다른 것은 내림차순입니다 (예 : mostLikes). 즉, 범주에 따라 100 개의 가장 큰 점수 또는 가장 낮은 점수 100 개가 필요합니다.

db.highscore.find({category:category}, {}).limit(100).sort({ score: 1 /*or -1*/ });

db.highscore.find({username:username});

당신은 무엇 인덱스를 추천 할 것입니다 :

내 응용 프로그램에서 두 가지 쿼리가있다?

다른 테이블에서 오름차순 범주와 내림차순 범주를 유지하면 성능이 향상됩니까?

참고 : 카테고리 당 하나의 표를 갖고 싶지 않습니다.

+0

안녕하세요 @RainingChain이 도움이되고 확신한다고 생각하면 허용되는 답변을 표시해주세요. – user641887

답변

2

나는 일부 샘플 데이터 세트 내 지역에 몇 가지 테스트를하고 난 최선의 옵션은 다음과 같은 필드에 인덱스를 생성 "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.

관련 문제