#247 open
Tom Locke

Unused DB fetch in Agility

Reported by Tom Locke | August 27th, 2008 @ 05:23 PM | in Beyond Hobo 1.0

After adding searching and filtering of stories, the log is showing:


  Story Load (0.000989)   SELECT * FROM "stories" WHERE ("stories".project_id = 1) AND (((stories.title like '%logs%')))
  Story Load (0.000904)   SELECT * FROM "stories" WHERE ("stories".project_id = 1)

Why the extra load of stories?

Comments and changes to this ticket

  • Tom Locke

    Tom Locke August 27th, 2008 @ 05:23 PM

    • State changed from “new” to “open”
  • Matt Jones

    Matt Jones September 7th, 2008 @ 08:26 AM

    There's something odd going on here with the .try. method.

    For enlightenment, try this in script/console:

    
    (assuming you have a first project, with two stories, "Story 1" and "Story 2")
    (steps numbered before =>)
    
    1=> p = Project.find(:first)
    2=> s = p.stories.apply_scopes(:search => ["Story 1", :title]); nil
    3=> s
    4=> s.try.new_record?
    

    A tail -f on development log tells us the story:

    • Step 1 loads the project (as expected)
    • Step 2 doesn't invoke the DB at all (note the ; nil to keep IRB from dumping s)
    • Step 3 executes the correct query
    • Step 4 does a find(:all) on Story (!?)

    Line 4 above was borrowed from the form tag, which is being called by table-plus. Since the search form doesn't use the results, should the context be changed to something else?

    As to the deeper question (why is .try. doing this), I'm somewhat stumped. I traced through the calls to respond_to? all the way into AssociationCollection#load_target, but how @loaded gets cleared is a mystery.

  • Tom Locke

    Tom Locke September 9th, 2008 @ 01:05 PM

    but how @loaded gets cleared is a mystery

    The association was never loaded in the first place. In the above code, line 4 is the first time the association is loaded.

    It turns out that doing a respond_to? on an ActiveRecord AssociationProxy will tend to cause the proxy to load the collection. So we need to be careful about throwing respond_to? and try around too much.

    Do you happen to remember where the .try in question was?

    I've created #261 to track this

  • Matt Jones

    Matt Jones September 9th, 2008 @ 10:02 PM

    The .try was from rapid_forms.dryml, line 27 (looking for new_record?).

    (Idea 1) I commented out the definitions of both respond_to? and method_missing in named_scope_extensions.rb, but only one level of scoping worked (adding a sort scope ignored the search scope).

    (Idea 2) Changing the respond_to? in named_scope_extensions.rb to

    
    
          def respond_to?(method)
            return true if super || scopes.include?(method)
            with_scope :find => proxy_options do
              proxy_scope.respond_to?(method)
            end
          end
    
    

    changes the second select to match the first. It doesn't look like much in the console (now does the SAME query twice), but since Rails uses the AR query cache, the second one is pulled from cache when running in script/server.

    Of course, now I'm trying to figure out who's loading all the tasks for stories in the result set...

  • Matt Jones

    Matt Jones September 9th, 2008 @ 11:22 PM

    More info:

    The tasks are being loaded from hobo_helper.rb, line 237 (setting up cache in can_view?). The use of the association proxy in an array that gets hashed causes the load.

    Commenting out the cache code still loads the tasks, this time from line 282 in hobo.rb (checking never_show).


    In both cases, the extra loading comes from code that assumes an AssociationProxy object is a single record. Should there be some sort of a check for this? After all, displaying a form tag for an array of Stories doesn't have a sensible interpretation, at least to me.

  • Tom Locke

    Tom Locke September 11th, 2008 @ 03:22 PM

    I'm going to have to try and carve out some time for a proper look at this...

  • Tom Locke

    Tom Locke March 12th, 2009 @ 06:12 PM

    • Milestone changed from Hobo 1.0 - Final to Beyond Hobo 1.0

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 ยป

People watching this ticket