Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4470

relations requests causes scanAndOrder queries

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0, 6.3.0
    • Affects Version/s: 6.2.0
    • Component/s: None
    • None

      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

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            alberto.ornaghi@gmail.com Alberto Ornaghi
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: