Carlota Viña
Carlota Viña

Reputation: 87

mongodb index find index sort

I would like to ask about mongodb indexes. Can I use a different index in the find and the sort. By example I have two indexes:

    (a:-1)
    (b:1,c:1)

What indexes uses this sentence?

    ({a:[$gt30}},{a:[$lt50}}]}.sort({c:1})

Upvotes: 0

Views: 223

Answers (3)

Vaneet Kataria
Vaneet Kataria

Reputation: 595

Suppose I am having persons collection having documents like below :

{
 dob: 
  { age : 50} , 
 gender : "male" ,
 phone : ""
}

Now i create indexes as below .

1 : db.persons.createIndex({"dob.age" : -1})
2 : db.persons.createIndex({phone : 1 , gender : 1})
  1. Now If i execute below query like yours

db.persons.explain("executionStats").find({$and : [ {"dob.age" : {$lt : 50} } , {"dob.age" : {$gt : 30} } ] } ).sort({gender : 1 })

I will get below execution stats :

{
            "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "college.persons",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                            "$and" : [
                                    {
                                            "dob.age" : {
                                                    "$lt" : 50
                                            }
                                    },
                                    {
                                            "dob.age" : {
                                                    "$gt" : 30
                                            }
                                    }
                            ]
                    },
                    "queryHash" : "22FEA299",
                    "planCacheKey" : "5E8F38C1",
                    "winningPlan" : {
                            "stage" : "SORT",
                            "sortPattern" : {
                                    "phone" : 1
                            },
                            "inputStage" : {
                                    "stage" : "SORT_KEY_GENERATOR",
                                    "inputStage" : {
                                            "stage" : "FETCH",
                                            "inputStage" : {
                                                    "stage" : "IXSCAN",
                                                    "keyPattern" : {
                                                            "dob.age" : -1
                                                    },
                                                    "indexName" : "dob.age_-1",
                                                    "isMultiKey" : false,
                                                    "multiKeyPaths" : {
                                                            "dob.age" : [ ]
                                                    },
                                                    "isUnique" : false,
                                                    "isSparse" : false,
                                                    "isPartial" : false,
                                                    "indexVersion" : 2,
                                                    "direction" : "forward",
                                                    "indexBounds" : {
                                                            "dob.age" : [
                                                                    "(50.0, 30.0)"
                                                            ]
                                                    }
                                            }
                                    }
                            }
                    },
                    "rejectedPlans" : [ ]
            },
            "executionStats" : {
                    "executionSuccess" : true,
                    "nReturned" : 1734,
                    "executionTimeMillis" : 10,
                    "totalKeysExamined" : 1734,
                    "totalDocsExamined" : 1734,
                    "executionStages" : {
                            "stage" : "SORT",
                            "nReturned" : 1734,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 3471,
                            "advanced" : 1734,
                            "needTime" : 1736,
                            "needYield" : 0,
                            "saveState" : 27,
                            "restoreState" : 27,
                            "isEOF" : 1,
                            "sortPattern" : {
                                    "phone" : 1
                            },
                            "memUsage" : 1914799,
                            "memLimit" : 33554432,
                            "inputStage" : {
                                    "stage" : "SORT_KEY_GENERATOR",
                                    "nReturned" : 1734,
                                    "executionTimeMillisEstimate" : 0,
                                    "works" : 1736,
                                    "advanced" : 1734,
                                    "needTime" : 1,
                                    "needYield" : 0,
                                    "saveState" : 27,
                                    "restoreState" : 27,
                                    "isEOF" : 1,
                                    "inputStage" : {
                                            "stage" : "FETCH",
                                            "nReturned" : 1734,
                                            "executionTimeMillisEstimate" : 0,
                                            "works" : 1735,
                                            "advanced" : 1734,
                                            "needTime" : 0,
                                            "needYield" : 0,
                                            "saveState" : 27,
                                            "restoreState" : 27,
                                            "isEOF" : 1,
                                            "docsExamined" : 1734,
                                            "alreadyHasObj" : 0,
                                            "inputStage" : {
                                                    "stage" : "IXSCAN",
                                                    "nReturned" : 1734,
                                                    "executionTimeMillisEstimate" : 0,
                                                    "works" : 1735,
                                                    "advanced" : 1734,
                                                    "needTime" : 0,
                                                    "needYield" : 0,
                                                    "saveState" : 27,
                                                    "restoreState" : 27,
                                                    "isEOF" : 1,
                                                    "keyPattern" : {
                                                            "dob.age" : -1
                                                    },
                                                    "indexName" : "dob.age_-1",
                                                    "isMultiKey" : false,
                                                    "multiKeyPaths" : {
                                                            "dob.age" : [ ]
                                                    },
                                                    "isUnique" : false,
                                                    "isSparse" : false,
                                                    "isPartial" : false,
                                                    "indexVersion" : 2,
                                                    "direction" : "forward",
                                                    "indexBounds" : {
                                                            "dob.age" : [
                                                                    "(50.0, 30.0)"
                                                            ]
                                                    },
                                                    "keysExamined" : 1734,
                                                    "seeks" : 1,
                                                    "dupsTested" : 0,
                                                    "dupsDropped" : 0
                                            }
                                    }
                            }
                    }
            },
            "serverInfo" : {
                    "host" : "RGGYSLT-0483",
                    "port" : 27017,
                    "version" : "4.2.0",
                    "gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
            },
            "ok" : 1
    }

This Means Data was fetched with IXScan on single field first and then sorted on third field separately .

But the moment i change the query to sort on fields for which the index is already created things change . Now {"dob.age" : -1} index gets rejected .

In Mongo db Winning plan is the one for which 100 docs can be fetched early and Mongo db caches that plan for a query . Now this cache will be purged after 1000 docs insertions , index rebuild , server restarts or new index insertions.

Hence which index will be used depends upon winning plans .

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "college.persons",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "dob.age" : {
                                                "$lt" : 50
                                        }
                                },
                                {
                                        "dob.age" : {
                                                "$gt" : 30
                                        }
                                }
                        ]
                },
                "queryHash" : "DA8248FA",
                "planCacheKey" : "E779554F",
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$and" : [
                                        {
                                                "dob.age" : {
                                                        "$lt" : 50
                                                }
                                        },
                                        {
                                                "dob.age" : {
                                                        "$gt" : 30
                                                }
                                        }
                                ]
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "indexName" : "gender_1_phone_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "gender" : [ ],
                                        "phone" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "gender" : [
                                                "[MinKey, MaxKey]"
                                        ],
                                        "phone" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "SORT",
                                "sortPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "inputStage" : {
                                        "stage" : "SORT_KEY_GENERATOR",
                                        "inputStage" : {
                                                "stage" : "FETCH",
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "dob.age" : -1
                                                        },
                                                        "indexName" : "dob.age_-1",
                                                        "isMultiKey" : false,
                                                        "multiKeyPaths" : {
                                                                "dob.age" : [ ]
                                                        },
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "dob.age" : [
                                                                        "(50.0, 30.0)"
                                                                ]
                                                        }
                                                }
                                        }
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1734,
                "executionTimeMillis" : 12,
                "totalKeysExamined" : 5002,
                "totalDocsExamined" : 5002,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$and" : [
                                        {
                                                "dob.age" : {
                                                        "$lt" : 50
                                                }
                                        },
                                        {
                                                "dob.age" : {
                                                        "$gt" : 30
                                                }
                                        }
                                ]
                        },
                        "nReturned" : 1734,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 5003,
                        "advanced" : 1734,
                        "needTime" : 3268,
                        "needYield" : 0,
                        "saveState" : 41,
                        "restoreState" : 41,
                        "isEOF" : 1,
                        "docsExamined" : 5002,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 5002,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 5003,
                                "advanced" : 5002,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 41,
                                "restoreState" : 41,
                                "isEOF" : 1,
                                "keyPattern" : {
                                        "gender" : 1,
                                        "phone" : 1
                                },
                                "indexName" : "gender_1_phone_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "gender" : [ ],
                                        "phone" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "gender" : [
                                                "[MinKey, MaxKey]"
                                        ],
                                        "phone" : [
                                                "[MinKey, MaxKey]"
                                        ]
                                },
                                "keysExamined" : 5002,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "RGGYSLT-0483",
                "port" : 27017,
                "version" : "4.2.0",
                "gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
        },
        "ok" : 1
}

Upvotes: 0

SOufiane Fadil
SOufiane Fadil

Reputation: 163

Creating an index on a single field called Single Field Index.

Creating multiple Single Field indexes to boost your query and the sort performance won't help much!. You should use Compound Indexes instead.

Check the documentation on MongoDB: https://docs.mongodb.com/manual/core/index-compound/

If you want to learn how to index your fields and how to measure the performance of your queries.

And Check this tutorial on Youtube: https://dplink.app/nxLgvk7lR

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

Can I use a different index in the find and the sort.

After reading some more into this you will see at the bottom of the documentation page on index intersectioning: http://docs.mongodb.org/manual/core/index-intersection/#index-intersection-and-sort

Index intersection does not apply when the sort() operation requires an index completely separate from the query predicate.

So no, even if ypou created an index of {c:1} it could not be used independantly to intersect {a:1}

What indexes uses this sentence?

In this case only {a:1} will be used.

Upvotes: 1

Related Questions