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

Mongoid 4 regression: eager-loading doesn't have the same effect as a unit of work

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 4.0.0 final
    • Component/s: None
    • Labels:
      None

      Description

      Consider an Artist, Artwork and Partner model where an artist has multiple artworks, and each artwork belongs to a partner.

      Mongoid 3.x

      Caused the last line to use the eager-loaded partners where available.

       ruby
      Mongoid.unit_of_work(enable: :current) do
         artworks = Artist.find("andy-warhol").artworks
        partners = Partner.find(artworks.pluck(:partner_id)) # eager-load partners
        artworks.map { |a| a.partner.name if a.partner }.count
      end
      

        MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (0.7188ms)
        MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=artists selector={"_slugs"=>{"$in"=>["andy-warhol"]}} flags=[:slave_ok] limit=1 skip=0 batch_size=nil fields=nil (0.5782ms)
        MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=artworks selector={"artist_id"=>"4d8b92b34eb68a1b2c0003f4"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields={"partner_id"=>1} (1.4071ms)
        MOPED: 127.0.0.1:27017 GET_MORE     database=gravity_development collection=artworks limit=0 cursor_id=272109786984 (9.3160ms)
        MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"_id"=>{"$in"=>["4ddafd136ec77c00010004b6", "5204f93b8b3b81eb610001a9", "5242ed69b504f50afc00000d", "4e7b980aa5673d00010025a9", "52b0d06e139b21e5ce000088", "52b4b398c9dc24266e00017c", "51fa692c8b3b813da80001e6", "52fa46c5c9dc247177000173", "5339bddd9c18db3e38000091", "4d8b92c44eb68a1b2c0004cb", "52556044139b215651000786", "52ab84fd9c18dbe4ea00036e", "527a75d3ebad6442e1000005", "50abcb3025e77810fd000033", "52b09b789c18dbb386000086", "52e17f8fcb4c2789ce00003d", "50abcb3025e77810fd000039", "51bba18c8b3b813937000259", "4ddd6bb279456d0001000535", "51756dd95109cdcf8c000075", "50a17dd5818df27de3001281", "51c9e564275b24b865000101", "4def99647726380001000cf0", "52e26e6e4b8480d49100012a", "5239cbe2c9dc248d910000d4", "52385aee275b24a2a000088e", "5256e044275b247996000345", "4feb7b5d8df4380001004320", "4dd165327568e5000100045d", "4dd178eb7568e500010015a6", "50776d01fdab610002000208", "4df39f31eabe38000100fc93", "4ea58123062b790001006479", "530d0056221393cae8000072", "4f20504785ecd00001000212", "4f1e35b99b4d830001000719", "50776d01fdab610002000202", "4ddffe359cd377000100181b", "517af95f97eba13839000142", "504e612bb8fb02000200080d", "50c555214822d31509000060", "4d8b934e4eb68a1b2c001311", "50475d4ab08eaa0002000333", "506b0c24dae1430002000610", "50475d94b08eaa0002000340", "52276822ebad64407900019b", "50776d01fdab61000200020a", "50776d03fdab61000200021e", "4e668a4662090d0001002769", "4f441a8d62470600010004bb", "52f9592e275b243fad00006d", "517a99237c186521e500002a", "5227681aebad644079000137", "524b44a69c18db9efa0000f2", "5217acf0a09a67625b000023", "52fb4541c9dc243d9f0002b2", "52b3407bcd530ee4ff00020f", "4de67edb4be72a0001002667", "52eacd97cd530e72680000ac", "5227682debad644079000227", "51420186ce7bba81bb00007d", "510047229fa5d115a0000041", "4df16c544c0f4600010069e2", "50d1a72b1272308197000445", "50776d02fdab61000200021a", "5286a67b9c18db3430000103", "4d8b92cc4eb68a1b2c0005ed", "4ea71bb3dfe62100010018fc", "504a229f8d00490002000166", "4df39b3f900008000100bdfb", "4fce49af62fb0100010008f9", "5226697eebad64d4a60000f5", "4d8b92ce4eb68a1b2c000620", "4d8d104d876c697ae1000020", "53060a8c9c18db65bc000183"]}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (6.4640ms)
        MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=artworks selector={"artist_id"=>"4d8b92b34eb68a1b2c0003f4"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (54.3151ms)
        MOPED: 127.0.0.1:27017 GET_MORE     database=gravity_development collection=artworks limit=0 cursor_id=271790928482 (893.4462ms)
        MOPED: 127.0.0.1:27017 GET_MORE     database=gravity_development collection=artworks limit=0 cursor_id=271790928482 (233.5098ms)
      => 955
      

      Mongoid 4.x

      Doesn't eager load the partners.

      ...
      > artworks.map { |a| a.partner.name if a.partner }.count
      D, [2014-05-30T11:32:40.256365 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('4ddafd136ec77c00010004b6')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.4720ms
      D, [2014-05-30T11:32:40.258824 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('4ddafd136ec77c00010004b6')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3940ms
      D, [2014-05-30T11:32:40.259989 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('5204f93b8b3b81eb610001a9')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3760ms
      D, [2014-05-30T11:32:40.261043 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('5204f93b8b3b81eb610001a9')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3350ms
      D, [2014-05-30T11:32:40.262026 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('5204f93b8b3b81eb610001a9')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3140ms
      D, [2014-05-30T11:32:40.262982 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('5204f93b8b3b81eb610001a9')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3350ms
      D, [2014-05-30T11:32:40.263908 #13600] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=gravity_development collection=partners selector={"$query"=>{"_id"=>BSON::ObjectId('5204f93b8b3b81eb610001a9')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3020ms
      

      Note this example is contrived, we rely on the fact that we can eager-load things in about a 100 places in our API. This obviously kills performance

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: