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.

Extreme performance problem with ActiveRecord queries with date ranges in Ruby 1.8

#5992

As also reported by some people in the announcement blog post of Rails 3.0.3, there is a severe performance problem when using ActiveRecord queries with Ruby 1.8 and Rails 3.0.3 (Arel 2.x) like these:

Account.where(:created_at => 2.months.ago..1.month.ago)

This is caused by a problem in ARel where ranges are enumerated in Ruby 1.8 by calling Range#min and Range#max. These methods are simply mixed in by Enumerable in 1.8, and therefore internally call to_a. When Ranges are very wide, which often is the case for dates, compiling such queries takes a very long time.

I have created a patch that can be found in this pull request in the ARel repository. Please let me know where the fix can be improved so this issue can be resolved as soon as possible hopefully.

Reported by Rolf Timmermans · November 17th, 2010 @ 02:36 PM

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

Activity

  1. Rolf Timmermans
    Rolf Timmermans

    And just to illustrate how extreme "extreme" is, I have conducted the following benchmark with a simple model named Account with a default created_at column:

    require "benchmark"
    
    Benchmark.bm do |bm|
      bm.report { Account.where(:created_at => 2.months.ago..1.month.ago) }
    end
    

    Result for Ruby 1.8 with Rails 3.0.3:

          user     system      total        real
    175.780000   1.490000 177.270000 (177.330136)
    

    Result for Ruby 1.8 with Rails 3.0.3 and the patch I linked to above:

          user     system      total        real
      0.010000   0.010000   0.020000 (  0.031937)
    

    November 17th, 2010 @ 03:00 PM

  2. Rolf Timmermans
    Rolf Timmermans
    • Tag changed from arel rails3 performance regression to arel, performance, rails3, regression

    November 17th, 2010 @ 03:25 PM

  3. Aaron Patterson
    Aaron Patterson
    • State changed from new to committed
    • Importance changed from to Low

    Thanks. I've merged your changes, and I'll release a new version.

    November 17th, 2010 @ 04:45 PM

  4. Magnus Bergmark
    Magnus Bergmark

    How could this be "Low" priority?

    November 19th, 2010 @ 01:31 PM