Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - distinct with query doesn't use indexes

Using Mongo 3.2.

Let's say I have a collection with this schema:

{ _id: 1, type: a, source: x },
{ _id: 2, type: a, source: y },
{ _id: 3, type: b, source: x },
{ _id: 4, type: b, source: y }

Of course that my db is much larger and with many more types and sources.

I have created 4 indexes combinations of type and source (even though 1 should be enough):

{type: 1}
{source: 1},
{type: 1, source: 1},
{source: 1, type: 1}

Now, I am running this distinct query:

db.test.distinct("source", {type: "a"})

The problem is that this query takes much more time that it should take. If I run it with runCommand:

db.runCommand({distinct: 'test', key: "source", query: {type: "a"}})

this is the result i get:

{
    "waitedMS": 0,
    "values": [
        "x",
        "y"
    ],
    "stats": {
        "n": 19400840,
        "nscanned": 19400840,
        "nscannedObjects": 19400840,
        "timems": 14821,
        "planSummary": "IXSCAN { type: 1 }"
    },
    "ok": 1
}

For some reason, mongo use only the type: 1 index for the query stage. It should use the index also for the distinct stage. Why is that? Using the {type: 1, source: 1} index would be much better, no? right now it is scanning all the type: a documents while it has an index for it.

Am I doing something wrong? Do I have a better option for this kind of distinct?

like image 751
TomG Avatar asked Jan 21 '26 00:01

TomG


1 Answers

As Alex mentioned, apparently MongoDB doesn't support this right now. There is an open issue for it: https://jira.mongodb.org/browse/SERVER-19507

like image 193
TomG Avatar answered Jan 23 '26 19:01

TomG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!