2016-06-20 6 views
-1

Ich habe Index für drei Attribute erstellt - a: 1, b: 1, c: -1MongoDB nicht Index verwenden, wenn der Wert null ist

Bei der Suche nach:

{a: false, b: null, c: {"$lt" : ISODate("2016-06-19T12:19:37.177Z")}} 

Mongo Verwendungen erstellt Index, aber muss immer noch "b" holen. Der Index wird also nur für a verwendet.

Aber wenn ich die Suche nach:

{a: false, b: "some value", c: {"$lt" : ISODate("2016-06-19T12:19:37.177Z")}} 

ich gesamten Index nicht verwendet.

Also die Frage ist, warum Mongo nicht ganzen Index verwendet, wenn der Wert null ist?

Sparse Option ändert nichts.

{ 
"queryPlanner" : { 
    "plannerVersion" : 1, 
    "namespace" : "db.collection", 
    "indexFilterSet" : false, 
    "parsedQuery" : { 
     "$and" : [ 
      { 
       "a" : { 
        "$eq" : false 
       } 
      }, 
      { 
       "b" : { 
        "$eq" : null 
       } 
      }, 
      { 
       "c" : { 
        "$lt" : ISODate("2016-06-19T12:19:37.177Z") 
       } 
      } 
     ] 
    }, 
    "winningPlan" : { 
     "stage" : "COUNT", 
     "inputStage" : { 
      "stage" : "KEEP_MUTATIONS", 
      "inputStage" : { 
       "stage" : "FETCH", 
       "filter" : { 
        "b" : { 
         "$eq" : null 
        } 
       }, 
       "inputStage" : { 
        "stage" : "IXSCAN", 
        "keyPattern" : { 
         "a" : NumberLong(1), 
         "b" : NumberLong(1), 
         "c" : NumberLong(-1) 
        }, 
        "indexName" : "a_1_b_1_c_-1", 
        "isMultiKey" : false, 
        "direction" : "forward", 
        "indexBounds" : { 
         "a" : [ 
          "[false, false]" 
         ], 
         "b" : [ 
          "[MinKey, MaxKey]" 
         ], 
         "c" : [ 
          "(new Date(1466338777177), true)" 
         ] 
        } 
       } 
      } 
     } 
    }, 
    "rejectedPlans" : [] 
}, 
"executionStats" : { 
    "executionSuccess" : true, 
    "nReturned" : 0, 
    "executionTimeMillis" : 3, 
    "totalKeysExamined" : 578, 
    "totalDocsExamined" : 578, 
    "executionStages" : { 
     "stage" : "COUNT", 
     "nReturned" : 0, 
     "executionTimeMillisEstimate" : 0, 
     "works" : 579, 
     "advanced" : 0, 
     "needTime" : 578, 
     "needFetch" : 0, 
     "saveState" : 4, 
     "restoreState" : 4, 
     "isEOF" : 1, 
     "invalidates" : 0, 
     "nCounted" : 577, 
     "nSkipped" : 0, 
     "inputStage" : { 
      "stage" : "KEEP_MUTATIONS", 
      "nReturned" : 577, 
      "executionTimeMillisEstimate" : 0, 
      "works" : 579, 
      "advanced" : 577, 
      "needTime" : 1, 
      "needFetch" : 0, 
      "saveState" : 4, 
      "restoreState" : 4, 
      "isEOF" : 1, 
      "invalidates" : 0, 
      "inputStage" : { 
       "stage" : "FETCH", 
       "filter" : { 
        "b" : { 
         "$eq" : null 
        } 
       }, 
       "nReturned" : 577, 
       "executionTimeMillisEstimate" : 0, 
       "works" : 579, 
       "advanced" : 577, 
       "needTime" : 1, 
       "needFetch" : 0, 
       "saveState" : 4, 
       "restoreState" : 4, 
       "isEOF" : 1, 
       "invalidates" : 0, 
       "docsExamined" : 578, 
       "alreadyHasObj" : 0, 
       "inputStage" : { 
        "stage" : "IXSCAN", 
        "nReturned" : 578, 
        "executionTimeMillisEstimate" : 0, 
        "works" : 579, 
        "advanced" : 578, 
        "needTime" : 0, 
        "needFetch" : 0, 
        "saveState" : 4, 
        "restoreState" : 4, 
        "isEOF" : 1, 
        "invalidates" : 0, 
        "keyPattern" : { 
         "a" : NumberLong(1), 
         "b" : NumberLong(1), 
         "c" : NumberLong(-1) 
        }, 
        "indexName" : "a_1_b_1_c_-1", 
        "isMultiKey" : false, 
        "direction" : "forward", 
        "indexBounds" : { 
         "c" : [ 
          "[false, false]" 
         ], 
         "b" : [ 
          "[MinKey, MaxKey]" 
         ], 
         "c" : [ 
          "(new Date(1466338777177), true)" 
         ] 
        }, 
        "keysExamined" : 578, 
        "dupsTested" : 0, 
        "dupsDropped" : 0, 
        "seenInvalidated" : 0, 
        "matchTested" : 0 
       } 
      } 
     } 
    } 
}, 
"serverInfo" : { 
    "host" : "vagrant-ubuntu-trusty-64", 
    "port" : 27017, 
    "version" : "3.0.10", 
    "gitVersion" : "1e0512f8453d103987f5fbfb87b71e9a131c2a60" 
}, 
"ok" : 1.0 

}

Antwort

0

Nach einigen Recherchen habe ich, dass die Indizierung gelernt und für null Suche funktioniert genauso wie für andere Werte.

Egal, wenn Sie erklären, können Sie feststellen, dass für "Null" Sie zusätzliche Bühne namens "FETCH", die ziemlich verwirrend sein kann. Trotzdem ist die Anzahl der geprüften Dokumente/Schlüssel genau gleich (Testdatenbank enthielt symmetrische Daten).

Verwandte Themen