未验证 提交 ac65e560 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39269 from kamipo/improve_performance_loaded_association_first

Improve performance for loaded association's `first`
......@@ -1103,6 +1103,10 @@ def reset_scope # :nodoc:
delegate(*delegate_methods, to: :scope)
private
def check_reorder_deprecation
super unless loaded?
end
def find_nth_with_limit(index, limit)
load_target if find_from_target?
super
......
......@@ -114,14 +114,7 @@ def take!
# Person.first(3) # returns the first three objects fetched by SELECT * FROM people ORDER BY people.id LIMIT 3
#
def first(limit = nil)
if !order_values.empty? && order_values.all?(&:blank?)
blank_value = order_values.first
ActiveSupport::Deprecation.warn(<<~MSG.squish)
`.reorder(#{blank_value.inspect})` with `.first` / `.first!` no longer
takes non-deterministic result in Rails 6.2.
To continue taking non-deterministic result, use `.take` / `.take!` instead.
MSG
end
check_reorder_deprecation
if limit
find_nth_with_limit(0, limit)
......@@ -355,8 +348,15 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
end
private
def offset_index
offset_value || 0
def check_reorder_deprecation
if !order_values.empty? && order_values.all?(&:blank?)
blank_value = order_values.first
ActiveSupport::Deprecation.warn(<<~MSG.squish)
`.reorder(#{blank_value.inspect})` with `.first` / `.first!` no longer
takes non-deterministic result in Rails 6.2.
To continue taking non-deterministic result, use `.take` / `.take!` instead.
MSG
end
end
def construct_relation_for_exists(conditions)
......@@ -518,7 +518,7 @@ def find_take_with_limit(limit)
end
def find_nth(index)
@offsets[offset_index + index] ||= find_nth_with_limit(index, 1).first
@offsets[index] ||= find_nth_with_limit(index, 1).first
end
def find_nth_with_limit(index, limit)
......@@ -532,7 +532,7 @@ def find_nth_with_limit(index, limit)
end
if limit > 0
relation = relation.offset(offset_index + index) unless index.zero?
relation = relation.offset((offset_value || 0) + index) unless index.zero?
relation.limit(limit).to_a
else
[]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册