2017-10-23 2 views
1

우리는 아래 쿼리몽고 날짜 범위 지수는

db.Comment.find(
    { 
     $and: [ 
      { reportCount: { $gt: 0 } }, 
      { assignee: { $exists: false } }, 
      { creationDate: { $gt: new Date(1507831097809) } }, 
      { creationDate: { $lt: new Date(1508522297966) } }, 
      { siteId: 'MAIN' }, 
      { parent: { $exists: false } }, 
      { status: 'ACTIVE' } 
     ] 
    }) 
    .sort({ creationDate: 1 }) 

이 우리가 인덱스 여기

{ 
    "v" : 2, 
    "key" : { 
     "creationDate" : 1, 
     "reportCount" : 1, 
     "label" : 1 
    } 
} 

을하는 것은 explain 결과입니다

{ 
"queryPlanner" : { 
    "plannerVersion" : 1, 
    "namespace" : "myNameSpace", 
    "indexFilterSet" : false, 
    "parsedQuery" : { 
     "$and" : [ 
      { 
       "siteId" : { 
        "$eq" : "MAIN" 
       } 
      }, 
      { 
       "status" : { 
        "$eq" : "ACTIVE" 
       } 
      }, 
      { 
       "creationDate" : { 
        "$lt" : ISODate("2017-10-20T17:58:17.966Z") 
       } 
      }, 
      { 
       "creationDate" : { 
        "$gt" : ISODate("2017-10-12T17:58:17.809Z") 
       } 
      }, 
      { 
       "reportCount" : { 
        "$gt" : 0.0 
       } 
      }, 
      { 
       "$nor" : [ 
        { 
         "assignee" : { 
          "$exists" : true 
         } 
        } 
       ] 
      }, 
      { 
       "$nor" : [ 
        { 
         "parent" : { 
          "$exists" : true 
         } 
        } 
       ] 
      } 
     ] 
    }, 
    "winningPlan" : { 
     "stage" : "FETCH", 
     "filter" : { 
      "$and" : [ 
       { 
        "siteId" : { 
         "$eq" : "MAIN" 
        } 
       }, 
       { 
        "status" : { 
         "$eq" : "ACTIVE" 
        } 
       }, 
       { 
        "$nor" : [ 
         { 
          "assignee" : { 
           "$exists" : true 
          } 
         } 
        ] 
       }, 
       { 
        "$nor" : [ 
         { 
          "parent" : { 
           "$exists" : true 
          } 
         } 
        ] 
       } 
      ] 
     }, 
     "inputStage" : { 
      "stage" : "IXSCAN", 
      "keyPattern" : { 
       "creationDate" : 1.0, 
       "reportCount" : 1.0, 
       "label" : 1.0 
      }, 
      "indexName" : "creationDate_1_reportCount_1_label_1", 
      "isMultiKey" : false, 
      "multiKeyPaths" : { 
       "creationDate" : [], 
       "reportCount" : [], 
       "label" : [] 
      }, 
      "isUnique" : false, 
      "isSparse" : false, 
      "isPartial" : false, 
      "indexVersion" : 2, 
      "direction" : "forward", 
      "indexBounds" : { 
       "creationDate" : [ 
        "(new Date(1507831097809), new Date(1508522297966))" 
       ], 
       "reportCount" : [ 
        "(0.0, inf.0]" 
       ], 
       "label" : [ 
        "[MinKey, MaxKey]" 
       ] 
      } 
     } 
    }, 
    "rejectedPlans" : [ 
     { 
      "stage" : "SORT", 
      "sortPattern" : { 
       "creationDate" : 1.0 
      }, 
      "inputStage" : { 
       "stage" : "SORT_KEY_GENERATOR", 
       "inputStage" : { 
        "stage" : "FETCH", 
        "filter" : { 
         "$and" : [ 
          { 
           "$nor" : [ 
            { 
             "parent" : { 
              "$exists" : true 
             } 
            } 
           ] 
          }, 
          { 
           "siteId" : { 
            "$eq" : "MAIN" 
           } 
          }, 
          { 
           "status" : { 
            "$eq" : "ACTIVE" 
           } 
          }, 
          { 
           "creationDate" : { 
            "$lt" : ISODate("2017-10-20T17:58:17.966Z") 
           } 
          }, 
          { 
           "creationDate" : { 
            "$gt" : ISODate("2017-10-12T17:58:17.809Z") 
           } 
          }, 
          { 
           "reportCount" : { 
            "$gt" : 0.0 
           } 
          }, 
          { 
           "$nor" : [ 
            { 
             "assignee" : { 
              "$exists" : true 
             } 
            } 
           ] 
          } 
         ] 
        }, 
        "inputStage" : { 
         "stage" : "IXSCAN", 
         "keyPattern" : { 
          "parent" : 1.0 
         }, 
         "indexName" : "parent_1", 
         "isMultiKey" : false, 
         "multiKeyPaths" : { 
          "parent" : [] 
         }, 
         "isUnique" : false, 
         "isSparse" : false, 
         "isPartial" : false, 
         "indexVersion" : 2, 
         "direction" : "forward", 
         "indexBounds" : { 
          "parent" : [ 
           "[null, null]" 
          ] 
         } 
        } 
       } 
      } 
     }, 
     { 
      "stage" : "SORT", 
      "sortPattern" : { 
       "creationDate" : 1.0 
      }, 
      "inputStage" : { 
       "stage" : "SORT_KEY_GENERATOR", 
       "inputStage" : { 
        "stage" : "FETCH", 
        "filter" : { 
         "$and" : [ 
          { 
           "$nor" : [ 
            { 
             "assignee" : { 
              "$exists" : true 
             } 
            } 
           ] 
          }, 
          { 
           "siteId" : { 
            "$eq" : "MAIN" 
           } 
          }, 
          { 
           "status" : { 
            "$eq" : "ACTIVE" 
           } 
          }, 
          { 
           "creationDate" : { 
            "$lt" : ISODate("2017-10-20T17:58:17.966Z") 
           } 
          }, 
          { 
           "creationDate" : { 
            "$gt" : ISODate("2017-10-12T17:58:17.809Z") 
           } 
          }, 
          { 
           "reportCount" : { 
            "$gt" : 0.0 
           } 
          }, 
          { 
           "$nor" : [ 
            { 
             "parent" : { 
              "$exists" : true 
             } 
            } 
           ] 
          } 
         ] 
        }, 
        "inputStage" : { 
         "stage" : "IXSCAN", 
         "keyPattern" : { 
          "assignee" : 1.0 
         }, 
         "indexName" : "assignee_1", 
         "isMultiKey" : false, 
         "multiKeyPaths" : { 
          "assignee" : [] 
         }, 
         "isUnique" : false, 
         "isSparse" : false, 
         "isPartial" : false, 
         "indexVersion" : 2, 
         "direction" : "forward", 
         "indexBounds" : { 
          "assignee" : [ 
           "[null, null]" 
          ] 
         } 
        } 
       } 
      } 
     }, 
     { 
      "stage" : "SORT", 
      "sortPattern" : { 
       "creationDate" : 1.0 
      }, 
      "inputStage" : { 
       "stage" : "SORT_KEY_GENERATOR", 
       "inputStage" : { 
        "stage" : "FETCH", 
        "filter" : { 
         "$and" : [ 
          { 
           "status" : { 
            "$eq" : "ACTIVE" 
           } 
          }, 
          { 
           "creationDate" : { 
            "$lt" : ISODate("2017-10-20T17:58:17.966Z") 
           } 
          }, 
          { 
           "creationDate" : { 
            "$gt" : ISODate("2017-10-12T17:58:17.809Z") 
           } 
          }, 
          { 
           "reportCount" : { 
            "$gt" : 0.0 
           } 
          }, 
          { 
           "$nor" : [ 
            { 
             "assignee" : { 
              "$exists" : true 
             } 
            } 
           ] 
          }, 
          { 
           "$nor" : [ 
            { 
             "parent" : { 
              "$exists" : true 
             } 
            } 
           ] 
          } 
         ] 
        }, 
        "inputStage" : { 
         "stage" : "IXSCAN", 
         "keyPattern" : { 
          "siteId" : 1.0, 
          "updatedDate" : 1.0, 
          "label" : 1.0 
         }, 
         "indexName" : "siteId_1_updatedDate_1_label_1", 
         "isMultiKey" : false, 
         "multiKeyPaths" : { 
          "siteId" : [], 
          "updatedDate" : [], 
          "label" : [] 
         }, 
         "isUnique" : false, 
         "isSparse" : false, 
         "isPartial" : false, 
         "indexVersion" : 2, 
         "direction" : "forward", 
         "indexBounds" : { 
          "siteId" : [ 
           "[\"MAIN\", \"MAIN\"]" 
          ], 
          "updatedDate" : [ 
           "[MinKey, MaxKey]" 
          ], 
          "label" : [ 
           "[MinKey, MaxKey]" 
          ] 
         } 
        } 
       } 
      } 
     }, 
     { 
      "stage" : "SORT", 
      "sortPattern" : { 
       "creationDate" : 1.0 
      }, 
      "inputStage" : { 
       "stage" : "SORT_KEY_GENERATOR", 
       "inputStage" : { 
        "stage" : "FETCH", 
        "filter" : { 
         "$and" : [ 
          { 
           "$nor" : [ 
            { 
             "parent" : { 
              "$exists" : true 
             } 
            } 
           ] 
          }, 
          { 
           "$nor" : [ 
            { 
             "assignee" : { 
              "$exists" : true 
             } 
            } 
           ] 
          }, 
          { 
           "siteId" : { 
            "$eq" : "MAIN" 
           } 
          }, 
          { 
           "status" : { 
            "$eq" : "ACTIVE" 
           } 
          }, 
          { 
           "creationDate" : { 
            "$lt" : ISODate("2017-10-20T17:58:17.966Z") 
           } 
          }, 
          { 
           "creationDate" : { 
            "$gt" : ISODate("2017-10-12T17:58:17.809Z") 
           } 
          }, 
          { 
           "reportCount" : { 
            "$gt" : 0.0 
           } 
          } 
         ] 
        }, 
        "inputStage" : { 
         "stage" : "AND_SORTED", 
         "inputStages" : [ 
          { 
           "stage" : "IXSCAN", 
           "keyPattern" : { 
            "parent" : 1.0 
           }, 
           "indexName" : "parent_1", 
           "isMultiKey" : false, 
           "multiKeyPaths" : { 
            "parent" : [] 
           }, 
           "isUnique" : false, 
           "isSparse" : false, 
           "isPartial" : false, 
           "indexVersion" : 2, 
           "direction" : "forward", 
           "indexBounds" : { 
            "parent" : [ 
             "[null, null]" 
            ] 
           } 
          }, 
          { 
           "stage" : "IXSCAN", 
           "keyPattern" : { 
            "assignee" : 1.0 
           }, 
           "indexName" : "assignee_1", 
           "isMultiKey" : false, 
           "multiKeyPaths" : { 
            "assignee" : [] 
           }, 
           "isUnique" : false, 
           "isSparse" : false, 
           "isPartial" : false, 
           "indexVersion" : 2, 
           "direction" : "forward", 
           "indexBounds" : { 
            "assignee" : [ 
             "[null, null]" 
            ] 
           } 
          } 
         ] 
        } 
       } 
      } 
     } 
    ] 
}, 
"executionStats" : { 
    "executionSuccess" : true, 
    "nReturned" : 19, 
    "executionTimeMillis" : 8, 
    "totalKeysExamined" : 533, 
    "totalDocsExamined" : 56, 
    "executionStages" : { 
     "stage" : "FETCH", 
     "filter" : { 
      "$and" : [ 
       { 
        "siteId" : { 
         "$eq" : "MAIN" 
        } 
       }, 
       { 
        "status" : { 
         "$eq" : "ACTIVE" 
        } 
       }, 
       { 
        "$nor" : [ 
         { 
          "assignee" : { 
           "$exists" : true 
          } 
         } 
        ] 
       }, 
       { 
        "$nor" : [ 
         { 
          "parent" : { 
           "$exists" : true 
          } 
         } 
        ] 
       } 
      ] 
     }, 
     "nReturned" : 19, 
     "executionTimeMillisEstimate" : 0, 
     "works" : 534, 
     "advanced" : 19, 
     "needTime" : 513, 
     "needYield" : 0, 
     "saveState" : 20, 
     "restoreState" : 20, 
     "isEOF" : 1, 
     "invalidates" : 0, 
     "docsExamined" : 56, 
     "alreadyHasObj" : 0, 
     "inputStage" : { 
      "stage" : "IXSCAN", 
      "nReturned" : 56, 
      "executionTimeMillisEstimate" : 0, 
      "works" : 533, 
      "advanced" : 56, 
      "needTime" : 476, 
      "needYield" : 0, 
      "saveState" : 20, 
      "restoreState" : 20, 
      "isEOF" : 1, 
      "invalidates" : 0, 
      "keyPattern" : { 
       "creationDate" : 1.0, 
       "reportCount" : 1.0, 
       "label" : 1.0 
      }, 
      "indexName" : "creationDate_1_reportCount_1_label_1", 
      "isMultiKey" : false, 
      "multiKeyPaths" : { 
       "creationDate" : [], 
       "reportCount" : [], 
       "label" : [] 
      }, 
      "isUnique" : false, 
      "isSparse" : false, 
      "isPartial" : false, 
      "indexVersion" : 2, 
      "direction" : "forward", 
      "indexBounds" : { 
       "creationDate" : [ 
        "(new Date(1507831097809), new Date(1508522297966))" 
       ], 
       "reportCount" : [ 
        "(0.0, inf.0]" 
       ], 
       "label" : [ 
        "[MinKey, MaxKey]" 
       ] 
      }, 
      "keysExamined" : 533, 
      "seeks" : 477, 
      "dupsTested" : 0, 
      "dupsDropped" : 0, 
      "seenInvalidated" : 0 
     } 
    } 
}, 

"ok" : 1.0 
} 
쿼리는 여전히 복용

데이터를 반환하려면 700-800 밀리 초. 쿼리를 더 빠르게 실행할 수 있도록 인덱스를 어떻게 변경합니까? 고려하지 마십시오 "keysExamined" : 533, "seeks" : 477,이 데이터입니다. 이것은 단지 테스트 데이터입니다.

색인을 사용하지만 색인의 첫 번째 필드 만 보이는 것처럼 보이십니까? 또한 multuKey은 거짓입니까? Explain 스 플랜 출력에서 ​​

답변

2

몇 가지 핵심 포인트 :

  • 쿼리는 다음과 같은 속성 주소 : siteId, status, creationDate, reportCount, assignee, parent
  • 이기는 계획은 두 단계가 있습니다
    • IX_SCAN이 creationDate_1_reportCount_1_label_1를 사용을,이 색인 사용을 creationDatereportCount에서 조회하여 56 개 문서를 식별 한 다음 FETCH 단계로 전달합니다.
    • FETCH 수신자 IX_SCAN 단계에서 56 개의 문서를 작성한 다음이 문서를 조사하여 siteId, status, assigneeparent 필터를 적용합니다. 이 심문을 통해 37 개의 문서가 폐기되고 19 개의 문서가 반환됩니다.

그래서, 당신의 인덱스는 문서하지 인덱스을 검사하여 적용되는 단지 2 쿼리에서 6 개 특성 및 쿼리의 나머지 4 개 속성을 설명합니다. 이 자리에서이 인덱스를 다시 실행할 경우

db.collection.createIndex(
    {siteId: 1, status: 1, creationDate: 1, reportCount: 1, assignee: 1, parent: 1} 
) 

은 다음 찾아야한다 그 (a)는 MongoDB를이 인덱스와 (b)의 수를 선택 : 완전히 인덱스가 덮여 수 있도록이 쿼리를 원하는 경우 다음 인덱스를 생성 IX_SCAN 단계에서 전달 된 문서의 수는 찾기 호출에서 반환 한 문서 수와 같습니다.

""은 ""을 찾을 수 있습니다. 여기에 MongoDB가 다른 색인을 선택하게하는 다른 측면이 있기 때문입니다. $nor 및 정렬 단계 (creationDate: 1)의 사용. 나는 인덱스를 조정하고 각 후 '에'설명 실행 조정할와 executionStats 하위 문서에 다음과 같은 주요 항목을 찾고 추천 :

  • "nReturned"
  • 을 "totalDocsExamined" "totalKeysExamined"

엄지 손가락의 간단한 규칙이있다 : 가까이 totalKeysExaminednReturned로하고 totalDocsExamined 가까이가 제로입니다 ...더 나은 색인 범위.

쓰기 시간 및 색인 저장에 미치는 영향에 대한 색인 비용의 문제도 있습니다. 따라서 귀하의 비 기능 요구 사항을 고려하는 것이 좋습니다. 전체 색인 범위없이 원하는 경과 시간을 달성 할 수 있습니까? 그렇지 않다면 경험적 테스트를 진행해야하지만 explain() 산출물에 대한 응답으로 선택을 조정할 준비가되어 있어야합니다. 6893, "totalDocsExamined":

+0

내가 조금 인덱스를 변경하고 이러한 결과가 있습니다 ' "nReturned"INDEX1 : 276, "totalKeysExamined를"(276),' Index2 \t ' "nReturned": 276 , "totalKeysExamined": 6894, "totalDocsExamined": 6894,' \t [지수는 당신이 제안] INDEX3 ' "nReturned": 276, "totalKeysExamined": 5799, "totalDocsExamined"(3615),'INDEX1인가 Index3가 더 빠를거야? 'totalDocsExamined' 란 무엇입니까? – user1324887

+1

문서를 검사 할 때 키를 검사하는 것보다 비용이 많이 들기 때문에 totalDocsExamined가 작을수록 좋습니다. 엄지 손가락의 간단한 규칙은 이것입니다 : 가까운 totalKeysExamined가 nReturned에 가까울수록 totalDocsExamined가 더 가까워 질수록 ... 색인 범위가 좋아집니다. – glytching