This project is archived and is in readonly mode.

#3871 ✓resolved
ender672

Chaining scopes can trigger excessive db queries

Reported by ender672 | February 6th, 2010 @ 12:51 AM | in 3.0.2

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.

Comments and changes to this ticket

  • ender672

    ender672 February 6th, 2010 @ 12:52 AM

    • Tag changed from 3.0 scopes to 3.0
  • Jeremy Kemper

    Jeremy Kemper February 6th, 2010 @ 04:24 AM

    • Milestone cleared.
    • State changed from “new” to “open”
  • ender672

    ender672 February 10th, 2010 @ 02:08 AM

    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.

  • Samuel Danielson

    Samuel Danielson February 25th, 2010 @ 05:37 AM

    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">]
    
  • ender672

    ender672 April 5th, 2010 @ 08:13 PM

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

  • Jeremy Kemper

    Jeremy Kemper April 6th, 2010 @ 12:07 AM

    • State changed from “open” to “resolved”
  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “Medium”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Tags

Pages