aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-04 11:16:45 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-04-04 11:51:36 +0900
commit7fb2ba5ed85abe210f33eb49e5820cfe8973530d (patch)
tree20cbfe3e405d6d1aba2c0a01b899e6a622d62665 /activerecord/lib/active_record/relation
parent464d625324accb8486aefa0b4a4b46477462dd08 (diff)
downloadrails-7fb2ba5ed85abe210f33eb49e5820cfe8973530d.tar.gz
rails-7fb2ba5ed85abe210f33eb49e5820cfe8973530d.tar.bz2
rails-7fb2ba5ed85abe210f33eb49e5820cfe8973530d.zip
Optimizer hints should be applied on Top level query as much as possible
I've experienced this issue in our app, some hints only works on Top level query (e.g. `MAX_EXECUTION_TIME`).
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb7
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb9
2 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 4f9ddf302e..0384023a17 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -416,16 +416,17 @@ module ActiveRecord
def build_count_subquery(relation, column_name, distinct)
if column_name == :all
+ column_alias = Arel.star
relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
else
column_alias = Arel.sql("count_column")
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
end
- subquery = relation.arel.as(Arel.sql("subquery_for_count"))
- select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
+ subquery_alias = Arel.sql("subquery_for_count")
+ select_value = operation_over_aggregate_column(column_alias, "count", false)
- Arel::SelectManager.new(subquery).project(select_value)
+ relation.build_subquery(subquery_alias, select_value)
end
end
end
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index b0535cfff5..5f728f2263 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -992,6 +992,15 @@ module ActiveRecord
@arel ||= build_arel(aliases)
end
+ protected
+ def build_subquery(subquery_alias, select_value) # :nodoc:
+ subquery = except(:optimizer_hints).arel.as(subquery_alias)
+
+ Arel::SelectManager.new(subquery).project(select_value).tap do |arel|
+ arel.optimizer_hints(*optimizer_hints_values) unless optimizer_hints_values.empty?
+ end
+ end
+
private
# Returns a relation value with a given name
def get_value(name)