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

COUNT_SCAN not used for partial indexes

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.4
    • Component/s: Querying
    • None
    • Query
    • ALL

      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.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            andre.spiegel@mongodb.com Andre Spiegel
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: