This project is archived and is in readonly mode.

#5992 ✓committed
Rolf Timmermans

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

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

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.

Comments and changes to this ticket

  • Rolf Timmermans

    Rolf Timmermans November 17th, 2010 @ 03:00 PM

    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)
    
  • Rolf Timmermans

    Rolf Timmermans November 17th, 2010 @ 03:25 PM

    • Tag changed from arel rails3 performance regression to arel, performance, rails3, regression
  • Aaron Patterson

    Aaron Patterson November 17th, 2010 @ 04:45 PM

    • State changed from “new” to “committed”
    • Importance changed from “” to “Low”

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

  • Magnus Bergmark

    Magnus Bergmark November 19th, 2010 @ 01:31 PM

    How could this be "Low" priority?

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>

Pages