Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

This project is archived and is in readonly mode.

references_eager_loaded_tables? should search tables in :join clauses

#1104

When deciding whether to use the new-include-syntax or the old-include-syntax, activerecord searches :select, :order, and :conditions clauses for tables that are != to the base finder's table name. If there are any matches, it falls back to the old style.

This should be updated to ignore tables found in :join clauses. Without this update, the old include syntax fallback will happen more conservatively than it needs to.

We are experiencing this so often that we've hard coded references_eager_loaded_tables? to always return false so we're always using the new style. Of course, that would break lots of stuff, so this is a good compromise.

Patch to follow.

David Stevenson & Joseph Palermo (Pivotal Labs)

Reported by David Stevenson · September 24th, 2008 @ 05:00 PM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung

    This looks juicy :-) With 2.2RC1 around the corner, would be great to see the patch!

    October 7th, 2008 @ 12:01 AM

  2. David Stevenson
    David Stevenson

    So... this is actually hard. It's not easy to write a REGEX that works to find all the joined in tables from a string. It's not as easy as looking for TABLE_NAME.FIELD_NAME like rails does for everything else.

    Joseph and I have since disabled pre-2.1 style includes across our app. This turned out to be better for us than making rails "guess" better at which style of includes to use.

    More and more, people will be switching away from old includes for performance reasons, so I think the better approach here might be to provide a global configuration on ActiveRecord to disable single-query includes. Naturally, it won't work for every query, but anyone who cares about performance will be optimizing all their queries to work anyway. Without the ability to disable these old-style includes completely, you're always going to be stuck trying to trick rails into believing that your query is safe. No matter how good we make the guessing system, there will always be incorrect guesses, so let's solve this problem at the core.

    October 7th, 2008 @ 02:40 AM

  3. David Stevenson
  4. Rob Sanheim
    Rob Sanheim

    What do you mean by "new include syntax" versus the "old include syntax" ?

    October 17th, 2008 @ 05:04 AM

  5. David Stevenson
    David Stevenson

    Sorry - new include syntax is not a syntax but an implementation. It's the process of preloading associations using a separate query for each level of the include chain.

    The old include "syntax" created a single query to prefetch all the associated data. This was usually slower, but allowed you to use conditions on tables in the include chain.

    October 17th, 2008 @ 06:12 AM

  6. Frederick Cheung
    Frederick Cheung
    • State changed from new to duplicate

    #528 Check joins when determining preloading of includes is all about this (and has patches too)

    December 17th, 2008 @ 04:00 PM