diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-12-14 09:48:34 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-12-14 09:51:56 -0700 |
commit | 6d5b1fdf55611de2a1071c37544933bb588ae88e (patch) | |
tree | 30ce4485d5a5a91edcada087b7d8c53a6c48b896 | |
parent | 757a566657d2b55d1587e09c384b1b413c2a8699 (diff) | |
download | rails-6d5b1fdf55611de2a1071c37544933bb588ae88e.tar.gz rails-6d5b1fdf55611de2a1071c37544933bb588ae88e.tar.bz2 rails-6d5b1fdf55611de2a1071c37544933bb588ae88e.zip |
Perform a more efficient query in `Relation#any?`
This was changed in 421c81b, as `exists?` blows up if you are eager
loading a polymorphic association, as it'll try to construct a join to
that table. The previous change decided to execute a `count` instead,
which wouldn't join.
Of course, the only time we actually need to perform a join on the eager
loaded values (which would perform a left outer join) is if they're
being referenced in the where clause. This doesn't affect inner joins.
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 9 |
2 files changed, 8 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 2cf19c76c5..6728443251 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -271,12 +271,7 @@ module ActiveRecord def empty? return @records.empty? if loaded? - if limit_value == 0 - true - else - c = count(:all) - c.respond_to?(:zero?) ? c.zero? : c.empty? - end + limit_value == 0 || !exists? end # Returns true if there are no records. diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 435cef901b..b83032e2a0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -280,8 +280,8 @@ module ActiveRecord return false if !conditions - relation = apply_join_dependency(self, construct_join_dependency) - return false if ActiveRecord::NullRelation === relation + relation = apply_join_dependency(self, construct_join_dependency_for_exists) + return false if ActiveRecord::NullRelation === relation || limit_value == 0 relation = relation.except(:select, :order).select(ONE_AS_ONE).limit(1) @@ -359,6 +359,11 @@ module ActiveRecord ActiveRecord::Associations::JoinDependency.new(@klass, including, joins) end + def construct_join_dependency_for_exists + including = (eager_load_values + includes_values) & references_values.map(&:to_sym) + ActiveRecord::Associations::JoinDependency.new(@klass, including, []) + end + def construct_relation_for_association_calculations from = arel.froms.first if Arel::Table === from |