we've upgraded from mongoid 5.x to 6.2 and noticed a sudden spike in the scanAndOrder metric on my server.
after an investigation of the issue we've noticed that the 6.2 version performs a sort on _id for relations between parent-child models (belongs_to have_many).
> author.posts.first
translates to:
{"find"=>"posts", "filter"=>
{"author_id"=>BSON::ObjectId('57e8e2012afb656bac345f49')}, "sort"=>
{"_id"=>1}, "limit"=>1, "singleBatch"=>true}
but since the index used for the query is the id of the relation, the sort on the _id causes a scanAndOrder.
this behaviour was not present in 5.x since the sort was not added to the query.
as a workaround we monkey-patched the gem to add opts[:id_sort] = :none, but this is not optimal. can we have a global option for this or fixing it to go back to the previous behaviour?
having a rate of 400/s scanAndOrder in the graphs is not something we will get used to... it should be always near to zero.
thank you
- is related to
-
MONGOID-4547 Implement #take / #take! for AR feature parity
- Closed
-
MONGOID-4830 Remove #one method from has_many associations
- Closed