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

Distinct not uses indexes

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.0.6, 3.0.7
    • Component/s: Querying
    • Labels:
      None
    • ALL

      Hi!
      I have noticed that MongoDB won't use indexes when querying for a distinct value on a field. I will use it on some fields, but won't on others.

      Here's the example:

      db.product.createIndex({"_indexed.preventieve_mondzorg-max_bedrag_p_jr": 1});
      db.runCommand({distinct: "product", key:"_indexed.preventieve_mondzorg-max_bedrag_p_jr"});
      

      And that's what it produces:

      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 50,
      	"numIndexesAfter" : 50,
      	"note" : "all indexes already exist",
      	"ok" : 1
      }
      {
      	"values" : [
      		"€ 250,- | 75%",
      		"Geen dekking",
      		"...",
      	],
      	"stats" : {
      		"n" : 33660,
      		"nscanned" : 0,
      		"nscannedObjects" : 33660,
      		"timems" : 12531,
      		"planSummary" : "COLLSCAN"
      	},
      	"ok" : 1
      }
      

      On the other hand

      db.product.createIndex({"free_choice.value": 1});
      db.runCommand({distinct: "product", key:"free_choice.value"});
      

      Will the index:

      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 50,
      	"numIndexesAfter" : 50,
      	"note" : "all indexes already exist",
      	"ok" : 1
      }
      {
      	"values" : [
      		"gedeeltelijk",
      		"geen",
      		"ja"
      	],
      	"stats" : {
      		"n" : 4,
      		"nscanned" : 4,
      		"nscannedObjects" : 4,
      		"timems" : 2,
      		"planSummary" : "DISTINCT { free_choice.value: 1.0 }"
      	},
      	"ok" : 1
      }
      

      So... what could be the difference between those two fields?
      Is it a bug, or I am doing something wrong?

            Assignee:
            Unassigned Unassigned
            Reporter:
            qstyler Konstantin Bodnia
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: