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.

Chaining scopes can trigger excessive db queries

#3871

Scope#method_missing was testing whether the initial scoped methods were missing. However this test triggered Scope#== which actually loads scopes from the database. In some situations, this caused unnecessary database access.

I noticed extra db queries on a production app and narrowed it down to this test for initial scoped methods.

Note that there were no unit tests for the initial scoped methods code path and I am unsure where it would be needed. We might want to add a unit test for this scenario or take out that path entirely.

Reported by ender672 · February 6th, 2010 @ 12:51 AM

State: resolved
Milestone: 3.0.2
Assigned to: Pratik Pratik
Importance: Medium

Activity

  1. ender672
    ender672
    • Tag changed from 3.0 scopes to 3.0

    February 6th, 2010 @ 12:52 AM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • State changed from new to open

    February 6th, 2010 @ 04:24 AM

  3. ender672
    ender672

    I was unable to find a reason for the current_scoped_methods_when_defined test in Scope#method_missing. All tests pass without it.

    I am attaching an updated patch with a better unit test and that removes the current_scoped_methods_when_defined test.

    February 10th, 2010 @ 02:08 AM

  4. Samuel Danielson
    Samuel Danielson

    Thanks for the patch. Went from this

    >> Cow.after('a').after('b').after('c').after('d').all
      Cow Load (0.5ms)  SELECT "kine".* FROM "kine" WHERE (name > 'a') AND (name > 'b')
      Cow Load (0.2ms)  SELECT "kine".* FROM "kine" WHERE (name > 'a')
      Cow Load (0.2ms)  SELECT "kine".* FROM "kine" WHERE (name > 'a') AND (name > 'b') AND (name > 'c')
      Cow Load (0.2ms)  SELECT "kine".* FROM "kine" WHERE (name > 'a') AND (name > 'b') AND (name > 'c') AND (name > 'd')
    => [#<Cow id: 1, name: "sam", created_at: "2010-02-25 05:04:04", updated_at: "2010-02-25 05:04:04">]
    

    to this! Happy cows.

    >> Cow.after('a').after('b').after('c').after('d').all
      Cow Load (0.3ms)  SELECT "kine".* FROM "kine" WHERE (name > 'a') AND (name > 'b') AND (name > 'c') AND (name > 'd')
    => [#<Cow id: 1, name: "sam", created_at: "2010-02-25 05:04:04", updated_at: "2010-02-25 05:04:04">]
    

    February 25th, 2010 @ 05:37 AM

  5. ender672
    ender672
    • Tag changed from 3.0 to 3

    A patch similar to this (but sans test) looks to have been committed with 0be31f85639cf2f536c558819ef3ee45ba7d83a3.

    Link here:
    http://github.com/rails/rails/commit/0be31f85639cf2f536c558819ef3ee...

    I don't have permission to close the ticket, but this ticket is no longer needed.

    April 5th, 2010 @ 08:13 PM

  6. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to resolved

    April 6th, 2010 @ 12:07 AM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Medium

    October 15th, 2010 @ 11:01 PM