#581 ✓resolved
Jamie Macey

Empty associations causing multiple duplicate queries

Reported by Jamie Macey | October 2nd, 2008 @ 10:43 AM


class Profile
  has n, :photos
end

Profile.all.each do |profile|
  puts profile.photos.first.url
end

When the first profile hits the loop, DM asks the database for all photos belonging to the profiles in the result set. Yay.

But, if any profiles have no photos yet, when the loop reaches them, DM will again ask the database for all photos belonging to all the profiles in the result set, again.

It really should remember that it's asked that question already for that particular result set and not repeat itself immediately.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) December 12th, 2008 @ 04:34 PM

    • Assigned user cleared.
    • State changed from “new” to “confirmed”

    I can confirm this is an issue with the attached stand-alone script.

    However, unless someone wants to take this on, it probably won't be fixed in sam/dm-core as most of my effort is going into dkubb/dm-core and fixing SEL there. For various reasons we're not re-using any of the SEL code from sam/dm-core either, so it's not like the effort could be transferable.

    Marking this as confirmed for now.

  • Michael Opitz

    Michael Opitz January 5th, 2009 @ 04:34 PM

    Hi,

    I've attached a small patch which seems to fix this issue for me for the following example:

    http://pastebin.com/f553a8b56

    However, when leaving out the row "profile.photos.inspect" another query will be made for each existing photo, even when creating a second photo for the second profile :/. It seems that queries made with some methods (at least "empty?") won't be cached, however, some others will (e.g. inspect, each, ...).

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 04:48 AM

    • State changed from “confirmed” to “accepted”
    • Assigned user set to “Dan Kubb (dkubb)”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 28th, 2009 @ 03:30 AM

    • State changed from “accepted” to “resolved”

    I can confirm this is resolved in dm-core/next. Attached is a script demonstrating the fix.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Attachments

Pages