aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-11-07 00:27:50 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-11-09 18:47:39 +0900
commit2bf7c30049558c2503f20e9810fdf4727650fe99 (patch)
tree27f7aebb843c001060d6d1b39bd97d443f23ef43 /activerecord
parent1b16e4c348055e9e7909e59e47dbe346b149ba52 (diff)
downloadrails-2bf7c30049558c2503f20e9810fdf4727650fe99.tar.gz
rails-2bf7c30049558c2503f20e9810fdf4727650fe99.tar.bz2
rails-2bf7c30049558c2503f20e9810fdf4727650fe99.zip
Should except `:distinct` rather than `:order` for `exists?`
Records fetching order is very important for performance if `limit` is presented. Should not except the order in the case. And `exists?` replaces select list to `1 AS one` therefore `:distinct` is useless (`DISTINCT 1 AS one`). And PostgreSQL raises the following error if `:distinct` and `:order` are used in the same time. ``` ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list ```
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md3
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
2 files changed, 3 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index dd2a1b4b19..a85fa321cd 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -7,7 +7,8 @@
*Pavel Evstigneev*
-* Avoid `unscope(:order)` when `limit_value` is presented for `count`.
+* Avoid `unscope(:order)` when `limit_value` is presented for `count`
+ and `exists?`.
If `limit_value` is presented, records fetching order is very important
for performance. Should not unscope the order in the case.
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 97a819c5af..55ded4c6d0 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -321,7 +321,7 @@ module ActiveRecord
relation = apply_join_dependency(self, construct_join_dependency(eager_loading: false))
return false if ActiveRecord::NullRelation === relation
- relation = relation.except(:select, :order).select(ONE_AS_ONE).limit(1)
+ relation = relation.except(:select, :distinct).select(ONE_AS_ONE).limit(1)
case conditions
when Array, Hash