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.

This project is archived and is in readonly mode.

Repeated SHOW FIELDS FROM... queries with STI

#4396

ActiveRecord looks up the columns for a table each time it loads a class using that table
With STI, this means repeated calls to 'SHOW FIELDS FROM table_name' for each subclass.

The reason for this is that AR uses @columns (a class-level instance variable) to cache the results of the 'SHOW FIELDS...' query. STI subclasses thus repeat this call as each one loads to populate their own cache.

My first pass at addressing this is to use a class_variable hash mapping table names to columns.

http://gist.github.com/366053

I'm concerned about how this would impact development mode as migrations are run. With a little guidance in this area, I can put together a proper patch.

Reported by Luke Melia · April 14th, 2010 @ 06:17 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. foca
    foca

    The problem with using @@ variables is that they are shared across the entire ancestor chain, thus:

    @@@ruby class Base
    def self.columns

    @@columns ||= {}
    

    end end
    class A < Base; end
    class B < Base; end

    A.columns[:foo] = "I'm a column!"

    B.columns #=> {:foo=>"I'm a column!"}

    
    

    April 14th, 2010 @ 06:33 PM

  2. Luke Melia
    Luke Melia

    @foca, yep, my proposed solution actually takes advantage of that "feature" of class variables. It uses a single hash for the entire inheritance hierarchy, with table names as keys and the column collections as values.

    April 14th, 2010 @ 07:14 PM

  3. José Valim
    José Valim

    Not sure if this is concern, these queries run only once in production and in development they do not represent a bottleneck.

    April 14th, 2010 @ 10:12 PM

  4. Michael Koziarski
    Michael Koziarski

    yeah, I'm with Jose, how many subclasses do you really have?

    April 14th, 2010 @ 10:19 PM

  5. Luke Melia
    Luke Melia

    We have about 300 models in our app. About a third of them are subclasses. I started looking at this today because these queries popped up in some New Relic transaction traces.

    According to http://blog.lukeludwig.com/index.php/2009/01/08/rails-patch-for-cac... :

    "Turns out that MySQL creates a temporary table on disk for 'SHOW FIELDS' queries, so if the disk is busy with something else these queries can take awhile to complete as seen here."

    In addition, the overhead can be impactful in development or during repeated test runs, where you're reloading frequently.

    What do you think? If this doesn't belong in core, I can maintain my patch locally.

    April 14th, 2010 @ 11:17 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to Low

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:43 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:43 PM