db.collection.find(
{
ctu: "product", pe: "1234567890", pl: "ru-ru",
bl: { $elemMatch: { b: "main", l: "ru-ru" } },
ps: false
},
{uid: 1, pd: 1, v: 1, l: 1, mi: 1}
).sort({ updated_at: -1 })
.skip(6150).limit(50)
One Index:
{
"ctu" : 1,
"pe" : 1,
"pl" : 1,
"bl" : 1,
"ps" : 1,
"updated_at" : -1
}
Second index:
{
"updated_at" : 1
}
In this, it’s picking second one, in spite of having all keys in first and even though first is running fast than second because sort operation is using more than the limit. It is happening in my other collections also.
can anyone suggest better index?
I have added the index with all the field in find and sort in sequence. still not working.
2
Answers
It should work. If it doesn’t, try specifying the index as hint.
Suggestion
Change your index definition to use the nested fields of
bl
directly (and perhaps drop the existing index if it’s not being used by anything else):Preamble
Questions like this are best answered with an explain plan which helps us better understand how the database executed a query. In this case I think we can do a bit of investigation/testing with the information provided to figure out what might be happening here.
As @Noel suggests in their answer, it is possible to force index usage via hinting, but I would generally recommend only using that as a last result. It is usually much better to allow the database to do its job to select the most efficient plan to execute a query. The fact that the database is not currently selecting the "obvious" index suggests that something may be wrong.
I attempted to create a playground environment for this, but there appear to be some field reordering issues that impact index creation as well. For reference, that environment was here. You can see though that the
bl
field gets pulled into the first position which has somewhat of an impact on our analysis. I also had to use an (equivalent) aggregation there which just further complicates things.In my local (
6.0
) environment, I created the two indexes from the question. I also inserted a handful of documents, some which match the predicates and some which don’t.Index definition
Given an index on field
x
(e.g..createIndex({x: 1})
), a query that filters on the entire field can use the index:However, an index that filters on a nested value inside of
x
cannot use the index:This is manifesting in your particular setup because your index is on the
bl
field ("bl" : 1
) but you are filtering on other fields nested inside of that via the$elemMatch
(bl: { $elemMatch: { b: "main", l: "ru-ru" } }
). We can see this via theindexBounds
for the associated plan in the explain output:Specifically see that the scan on the
bl
field is not bounded:bl: [ '[MinKey, MaxKey]' ]
. Not only does this make the index scan itself less efficient, but it also causes another problem by preventing the index from being used to provide the requested sort. This is observable by the presence of a dedicated'SORT'
stage:Taken together, both of these inefficiencies result in the optimizer having somewhat of a preference for the single index hence its selection for usage. Modifying the index to replace the
bl
key with'bl.b'
and'bl.l'
fields resolves all of these issues and should result in the optimizer having a stronger preference for it over the single field index:Pagination (
skip
/limit
)I also notice that you appear to be doing pagination:
This is a very inefficient approach. Each operation will have to scan
skip
+limit
number of index keys. Consider other approaches instead.