aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-11-05 20:05:47 -0400
committerGitHub <noreply@github.com>2016-11-05 20:05:47 -0400
commit2b2c096125edc3becc970bea331d8f26cd6a3ed5 (patch)
tree6d7e4da07b6583d2851a94977dd64353d36ef393
parent81e16dad6c151611d0c1e5fd7927378d58e6344a (diff)
parent62bb8b9dfdf4101b4cefedb36a97531e750d623c (diff)
downloadrails-2b2c096125edc3becc970bea331d8f26cd6a3ed5.tar.gz
rails-2b2c096125edc3becc970bea331d8f26cd6a3ed5.tar.bz2
rails-2b2c096125edc3becc970bea331d8f26cd6a3ed5.zip
Merge pull request #26972 from kamipo/avoid_unscope_order_when_limit_value_present
Avoid `unscope(:order)` when `limit_value` is presented for `count`
-rw-r--r--activerecord/CHANGELOG.md13
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb12
2 files changed, 17 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 400f4942c4..d2adc530a4 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
-* Fix an Active Record DateTime field NoMethodError caused by incomplete
- datetime. [Bug #24195](https://github.com/rails/rails/issues/24195)
+* Avoid `unscope(:order)` when `limit_value` is presented for `count`.
+
+ If `limit_value` is presented, records fetching order is very important
+ for performance. Should not unscope the order in the case.
+
+ *Ryuta Kamizono*
+
+* Fix an Active Record DateTime field NoMethodError caused by incomplete
+ datetime.
+
+ Fixes #24195.
*Sen Zhang*
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index e4676f79a5..827688a663 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -223,17 +223,17 @@ module ActiveRecord
end
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
- # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
- relation = unscope(:order)
-
column_alias = column_name
- if operation == "count" && (relation.limit_value || relation.offset_value)
+ if operation == "count" && (limit_value || offset_value)
# Shortcut when limit is zero.
- return 0 if relation.limit_value == 0
+ return 0 if limit_value == 0
- query_builder = build_count_subquery(relation, column_name, distinct)
+ query_builder = build_count_subquery(spawn, column_name, distinct)
else
+ # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
+ relation = unscope(:order)
+
column = aggregate_column(column_name)
select_value = operation_over_aggregate_column(column, operation, distinct)