This project is archived and is in readonly mode.

#4396 ✓stale
Luke Melia

Repeated SHOW FIELDS FROM... queries with STI

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

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.

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.

Comments and changes to this ticket

  • foca

    foca April 14th, 2010 @ 06:33 PM

    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!"}

  • Luke Melia

    Luke Melia April 14th, 2010 @ 07:14 PM

    @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.

  • José Valim

    José Valim April 14th, 2010 @ 10:12 PM

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

  • Michael Koziarski

    Michael Koziarski April 14th, 2010 @ 10:19 PM

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

  • Luke Melia

    Luke Melia April 14th, 2010 @ 11:17 PM

    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 :

    "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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:43 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:43 PM

    • State changed from “open” to “stale”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>