2016-06-20 9 views
-1

私は3つの属性のインデックス作成されました - :1、B:1、cは:作成し使用しMongoDBはインデックスを使用していない

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

モンゴ:-1

を検索する場合インデックスを作成しますが、引き続き "b"を取得する必要があります。したがって、索引はaにのみ使用されます。

しかし、私はを検索したとき:

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

私は、全体のインデックスを使用しません。

なぜ、Mongoがvalueがnullの場合にインデックス全体を使用しないのですか?

スパースオプションは何も変更しません。

{ 
"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 

}

答えて

0

は、いくつかの研究の後、私はそのインデックスを学んだし、nullを検索すると、他の値のようなまったく同じように動作しています。

とにかく「null」の場合、「FETCH」と呼ばれる余分なステージがあり、それはかなり混乱することがあります。検査されたドキュメント/キーの数はまったく同じです(テストデータベースには対称データが含まれています)。

関連する問題