Nathan Broadbent
2013-Mar-17 10:44 UTC
Use = instead of BETWEEN when a date range''s begin and end are the same
Hi all,
Was wondering if it would be worth adding a predicate optimization for when
a date range''s `begin` and `end` are equal?
Post.where(:published_at => Date.new(2012,4,25)..Date.new(2012,4,25))
#=> Post Load (3.0ms) SELECT `posts`.* FROM `posts` WHERE
(`posts`.`published_at` BETWEEN ''2012-04-25'' AND
''2012-04-25'')
is a tiny bit slower than
Post.where(:published_at => Date.new(2012,4,25))
#=> Post Load (2.7ms) SELECT `posts`.* FROM `posts` WHERE
`posts`.`published_at` = ''2012-04-25''
We could add something like this, somewhere in *
active_record/relation/query_methods.rb*:
if val.is_a?(Range)
return val.begin == val.end ? val.begin : val
end
Please let me know if it''s worthwhile, and I''ll submit a pull
request.
Best,
Nathan
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Godfrey Chan
2013-Mar-18 06:27 UTC
Re: Use = instead of BETWEEN when a date range''s begin and end are the same
Since this is a perf PR, I think it''d be nice to see some benchmarks to be absolutely sure. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Thiago Massa
2013-Mar-20 12:00 UTC
Re: Use = instead of BETWEEN when a date range''s begin and end are the same
I think this isn''t worth the if clause performance degradation from all queries. This scenario doesn''t happen quite often. Em domingo, 17 de março de 2013 07h44min27s UTC-3, Nathan Broadbent escreveu:> > Hi all, > > Was wondering if it would be worth adding a predicate optimization for > when a date range''s `begin` and `end` are equal? > > > Post.where(:published_at => Date.new(2012,4,25)..Date.new(2012,4,25)) > > #=> Post Load (3.0ms) SELECT `posts`.* FROM `posts` WHERE > (`posts`.`published_at` BETWEEN ''2012-04-25'' AND ''2012-04-25'') > > > > is a tiny bit slower than > > Post.where(:published_at => Date.new(2012,4,25)) > > #=> Post Load (2.7ms) SELECT `posts`.* FROM `posts` WHERE > `posts`.`published_at` = ''2012-04-25'' > > > > We could add something like this, somewhere in * > active_record/relation/query_methods.rb*: > > if val.is_a?(Range) > return val.begin == val.end ? val.begin : val > end > > > > Please let me know if it''s worthwhile, and I''ll submit a pull request. > > > Best, > Nathan >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Borna Novak
2013-Mar-27 09:25 UTC
Re: Use = instead of BETWEEN when a date range''s begin and end are the same
Answer to this is "database specific and depending if there is an index on the column or not and how that index is organized", some database-storage engine-index type combinations will opt a full table table scan instead of index scan/fetch when "between" happens as opposed to a "=" query which would imply severe performance degradations in specific cases. Perhaps this can be addressed at adapter level or as a configuration option? On Monday, March 18, 2013 7:27:09 AM UTC+1, Godfrey Chan wrote:> > Since this is a perf PR, I think it''d be nice to see some benchmarks to be > absolutely sure. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.