aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/finder_methods.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-01-11 02:19:58 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-01-11 03:29:50 +0900
commitb4f64cb1595f2abd3d7e47805273f4f029718829 (patch)
tree3e58dbd5911596ce663a0e2f30ce3ae6c0b01fca /activerecord/lib/active_record/relation/finder_methods.rb
parent96f59306101a6ee252df2a9636ef2569d26924f7 (diff)
downloadrails-b4f64cb1595f2abd3d7e47805273f4f029718829.tar.gz
rails-b4f64cb1595f2abd3d7e47805273f4f029718829.tar.bz2
rails-b4f64cb1595f2abd3d7e47805273f4f029718829.zip
Make `relation.exists?` more performant when using eager loading
`relation.exists?` just wants to know if there is a result or not, does not need the exact records matched. Therefore, an intermediate SELECT query for eager loading is not necessary.
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb7
1 files changed, 4 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 50d0f14b98..12ac89f5ad 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -313,7 +313,7 @@ module ActiveRecord
return false if !conditions || limit_value == 0
if eager_loading?
- relation = apply_join_dependency(construct_join_dependency(eager_loading: false))
+ relation = apply_join_dependency(eager_loading: false)
return relation.exists?(conditions)
end
@@ -396,10 +396,11 @@ module ActiveRecord
)
end
- def apply_join_dependency(join_dependency = construct_join_dependency)
+ def apply_join_dependency(join_dependency = nil, eager_loading: true)
+ join_dependency ||= construct_join_dependency(eager_loading: eager_loading)
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
- if using_limitable_reflections?(join_dependency.reflections)
+ if !eager_loading || using_limitable_reflections?(join_dependency.reflections)
relation
else
if has_limit_or_offset?