Hello,
In the process of diagnosing query slowness I detected that the wrong index is being used.
Hinting towards the right index yielded the wanted performance, Trying building different variations of the wanted index had no impact on the index selection.
As a temporary attempt - I have deleted the ‘wrong’ index and then the index was selected correctly. What happened next had left me confused, as when I restored the ‘wrong’ index, the ‘right’ index is still selected.
I am familiar with the fundamental concept in MongoDB that fields order within an index affects index selection, query planning and execution, but I cannot find any mention to that order of indexes affects index selection - which is my conclusion from my observation in this case.
Is this a documented behavior of MongoDB? Is it a bug? I will be grateful for any help.
Some additional info
- MongoDB Version 4.4.18
- Both indexes, ‘wrong’ and ‘right’ are compound
- The query is not very basic and contains some branching, something like this:
{$and: [
{
$and: [...], $or: [...]
},
{
$or: [
{
a:{$in: [...]},
b:{$in: [...]}
}
//.... etc
]}
]}