Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-29247

COUNT_SCAN not used for partial indexes

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 3.4.4
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL

      Description

      Performing a count on a collection with a partial index leads to IXSCAN/FETCH/COUNT, rather than the more efficient COUNT_SCAN. By contrast, a count on a regular index does lead to a COUNT_SCAN.

      For example:

      db.coll.createIndex({"value":1}, {"partialFilterExpression":{"flag":true}})
       
      db.coll.explain(true).count({"flag":true,"value":10})
      ...
      		"winningPlan" : {
      			"stage" : "COUNT",
      			"inputStage" : {
      				"stage" : "FETCH",
      				"filter" : {
      					"flag" : {
      						"$eq" : true
      					}
      				},
      				"inputStage" : {
      					"stage" : "IXSCAN",
      					"keyPattern" : {
      						"value" : 1
      					},
      					"indexName" : "value_1",
      					"isMultiKey" : false,
      					"multiKeyPaths" : {
      						"value" : [ ]
      					},
      					"isUnique" : false,
      					"isSparse" : false,
      					"isPartial" : true,
      					"indexVersion" : 2,
      					"direction" : "forward",
      					"indexBounds" : {
      						"value" : [
      							"[10.0, 10.0]"
      						]
      					}
      				}
      			}
      		},
      ...
      

      Here is the same example with a compound index on (flag, value):

      db.coll.createIndex({"flag":1, "value":1})
       
      db.coll.explain(true).count({"flag":true,"value":10})
      ...
      		"winningPlan" : {
      			"stage" : "COUNT",
      			"inputStage" : {
      				"stage" : "COUNT_SCAN",
      				"keyPattern" : {
      					"flag" : 1,
      					"value" : 1
      				},
      				"indexName" : "flag_1_value_1",
      				"isMultiKey" : false,
      				"multiKeyPaths" : {
      					"flag" : [ ],
      					"value" : [ ]
      				},
      				"isUnique" : false,
      				"isSparse" : false,
      				"isPartial" : false,
      				"indexVersion" : 2,
      				"indexBounds" : {
      					"startKey" : {
      						"flag" : true,
      						"value" : 10
      					},
      					"startKeyInclusive" : true,
      					"endKey" : {
      						"flag" : true,
      						"value" : 10
      					},
      					"endKeyInclusive" : true
      				}
      			}
      		},		
      

      This issue is related to, but distinct from, other cases where partial indexes are not utilized to the full extent, such as SERVER-23808, SERVER-28889.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: