Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Associations::OneToMany.get leads to bizarre performance characteristics when invoked from a 1-sized collection #262

Open
twhaples opened this issue Nov 7, 2013 · 0 comments

Comments

@twhaples
Copy link

twhaples commented Nov 7, 2013

At https://github.com/datamapper/dm-core/blob/master/lib/dm-core/associations/one_to_many.rb#L94

This code is called to eager-load an associated resource. It has bizarre performance characteristics, so that a collection with 2 resources may be more efficient to work with than a collection with 1 resource.

Example:

We have two resources. Call them Thing and ThingClaim. A thing has 1, :thing_claim. We have an index route. It does something like

things = Thing.all(:blah => 'blah blah')
json = things.map do |t|
  {
    :name => t.name,
    :claim_time => t.claim.create_time,
    :expire_time => t.claim.expire_time,
    :claimed_by => t.claim.claimant_id,
    :claim_note => t.claim.note,
   }

If Thing.all returns 2 or more objects, this ends up issuing 2 queries (load all things, load all thing claims.) If Thing.all returns exactly 1 object, this code ends up issuing 5 queries (one to load things, one to load the claim and get the create time, one to load the claim and get the expire time...) because the claim is not stored.

This is freakish, bizarre, and unexpected. I propose that this comparison be changed to > 0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant