Bhavik Shah
Bhavik Shah

Reputation: 325

Mongodb db.collection.distinct() on aws documentdb doesn't use index

Transitioning to new AWS documentDB service. Currently, on Mongo 3.2. When I run db.collection.distinct("FIELD_NAME") it returns the results really quickly. I did a database dump to AWS document DB (Mongo 3.6 compatible) and this simple query just gets stuck.

Here's my .explain() and the indexes on the working instance versus AWS documentdb:

Explain function on working instance:

> db.collection.explain().distinct("FIELD_NAME")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "db.collection",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [ ]
                },
                "winningPlan" : {
                        "stage" : "PROJECTION",
                        "transformBy" : {
                                "_id" : 0,
                                "FIELD_NAME" : 1
                        },
                        "inputStage" : {
                                "stage" : "DISTINCT_SCAN",
                                "keyPattern" : {
                                        "FIELD_NAME" : 1
                                },
                                "indexName" : "FIELD_INDEX_NAME",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "FIELD_NAME" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },

Explain on AWS documentdb, not working:

rs0:PRIMARY> db.collection.explain().distinct("FIELD_NAME")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "db.collection",
                "winningPlan" : {
                        "stage" : "AGGREGATE",
                        "inputStage" : {
                                "stage" : "HASH_AGGREGATE",
                                "inputStage" : {
                                        "stage" : "COLLSCAN"
                                }
                        }
                }
        },
}

Index on both of these instances:

        {
                "v" : 1,
                "key" : {
                        "FIELD_NAME" : 1
                },
                "name" : "FIELD_INDEX_NAME",
                "ns" : "db.collection"
        }

Also, this database has a couple million documents but there are only about 20 distinct values for that "FIELD_NAME". Any help would be appreciated.

I tried it with .hint("index_name") and that didn't work. I tried clearing plan cache but I get Feature not supported: planCacheClear

Upvotes: 2

Views: 1634

Answers (1)

Mark
Mark

Reputation: 11

COLLSCAN and IXSCAN don't have too much difference in this case, both need to scan all the documents or index entries.

Upvotes: 1

Related Questions