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.

Quoting in ActiveRecord::Base is inefficient

#3642

During performance testing I noticed queries with many bind variables that are fast on mysql are very slow through rails, I believe this is due to ActiveRecord::Base.connection being called at least once for each bind variable. The following patch is just a demo to show how I got the number of calls reduced by more than a factor of 10, which was about a 50% speed increase for my particular problem.

The column_aliases fix, although we didn't profile it, should help speed up ActiveRecord queries with many joined in columns.

Is the connection something that really needs to be looked up so often, and not just memoized for the entire request? Surely the connection isn't changing between bind variables. :)

Reported by Comron Sattari · December 31st, 2009 @ 04:27 PM

State: committed
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. Comron Sattari
    Comron Sattari
    • Tag changed from active to activerecord, active_record, performance

    December 31st, 2009 @ 04:29 PM

  2. Comron Sattari
  3. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open
    • Assigned user set to Jeremy Kemper

    Nice patch! However, it's made against your app's vendor/rails, not Rails itself. Check out the contributor guide for a quick intro to making and submitting changes to Rails: https://rails.lighthouseapp.com/projects/8994/sending-patches

    Additional performance work like this is greatly appreciated. Thanks!

    January 1st, 2010 @ 02:18 AM

  4. Marius Nuennerich
    Marius Nuennerich

    Attached is an updated patch against git master. The last part of Comron's patch didn't apply anymore. I actually did another round of optimization in a second commit, maybe you don't want to commit that. Both tested against PostgreSQL, MySQL and Sqlite.

    May 2nd, 2010 @ 05:47 PM

  5. Marius Nuennerich
    Marius Nuennerich
    • Tag changed from activerecord, active_record, performance to activerecord, active_record, bugmash-review, performance

    May 16th, 2010 @ 10:53 PM

  6. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, active_record, bugmash-review, performance to activerecord, active_record, bugmash, performance

    May 16th, 2010 @ 10:53 PM

  7. Jeremy Kemper
    Jeremy Kemper

    Second patch doesn't apply cleanly. It would be nice to keep Comron as an author on the first commit, by the way: git commit --amend --author "..."

    May 16th, 2010 @ 11:27 PM

  8. Marius Nuennerich
    Marius Nuennerich

    Ok, I have redone the first part of the patch with amend.

    May 16th, 2010 @ 11:56 PM

  9. pleax
    pleax

    Verified cache_conn.diff from Marius applies cleanly to master and passes tests on mysql and sqlite3.

    May 17th, 2010 @ 12:20 AM

  10. Jeremy Kemper
    Jeremy Kemper

    Thanks Marius! Could you rebase your second patch also?

    May 17th, 2010 @ 01:41 AM

  11. Repository
    Repository
    • State changed from open to committed

    May 17th, 2010 @ 01:43 AM