Hi community,
I’m working on the index building.
I read the mongoDB documentation that talked about best practice on building index.
For example, I got that you should follow the ESR rule or that is better put in the first place of the index the attribute that have an higher cardinality.
I thought I’ve grasp the concept but when I put in practice there were a few things that did not work as I expected.
Let me explain better.
The query under-study is the following (do not pay attention to eventually mistakes and how the query is written):
{
$and[
{
“a”:1
},
{
“b”:2
},
{
“c”:
{$in:[3,4,5]}
},
{
“d”: true // this is always fixed
}
],
$or[
{
e: {$regex: “something”, $options: I}
}
]
$sort:{
{ f: 1}
}
}
I created also the following index (I used as I said the ESR and the cardinality rules):
a:1, b:1, f:1, c:1, e:1
I used also the partial index on the d field that is fixed for all the query and the collation to get better performance with the case insensitive regex.
But, if I run the query with the explain I get that the index used is another one that have some fields of the query and other one that is not present, for example:
b:1, c:1, a:1, h:1, i:1
(c is a range and it should go after the sort field, h and i are not even present in the query)
So, my questions are:
- how is it possible that an index with field not present in the query have an higher score than mine?
- Another strange thing that I have observed is that the index chose change with the number of result returned by the query. I mean, if the query returns 6 documents mongodb uses an index but if it returns 100 documents mongodb uses a different index, how is it possible?
- Is there a best practice to understand what is the best field to use in an index?
Thanks in advance!