aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/calculations.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-11-05 23:20:53 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-11-06 02:56:26 +0900
commit62bb8b9dfdf4101b4cefedb36a97531e750d623c (patch)
tree24f1bf3f80c7eea3b088d501d801728aab33b079 /activerecord/lib/active_record/relation/calculations.rb
parent95ea51e97bd758591d13d3bf27682d83a15bd1aa (diff)
downloadrails-62bb8b9dfdf4101b4cefedb36a97531e750d623c.tar.gz
rails-62bb8b9dfdf4101b4cefedb36a97531e750d623c.tar.bz2
rails-62bb8b9dfdf4101b4cefedb36a97531e750d623c.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb12
1 files changed, 6 insertions, 6 deletions
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)