Commit 783868e9 authored by Andreas Brandl's avatar Andreas Brandl

Remove N+1 query for Noteable association.

For each event with `Event#target_type` of "Note",
we had a query to load the associated instance `noteable`.

For example, if `Note` was related to an issue, we'd load each `Issue`
with its own query (N+1 problem).

Closes #43150.
parent 3cd95700
...@@ -52,12 +52,12 @@ class Event < ActiveRecord::Base ...@@ -52,12 +52,12 @@ class Event < ActiveRecord::Base
belongs_to :target, -> { belongs_to :target, -> {
# If the association for "target" defines an "author" association we want to # If the association for "target" defines an "author" association we want to
# eager-load this so Banzai & friends don't end up performing N+1 queries to # eager-load this so Banzai & friends don't end up performing N+1 queries to
# get the authors of notes, issues, etc. # get the authors of notes, issues, etc. (likewise for "noteable").
if reflections['events'].active_record.reflect_on_association(:author) incs = %i(author noteable).select do |a|
includes(:author) reflections['events'].active_record.reflect_on_association(a)
else
self
end end
incs.reduce(self) { |obj, a| obj.includes(a) }
}, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations }, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
has_one :push_event_payload has_one :push_event_payload
......
---
title: Remove N+1 query for Noteable association.
merge_request: 17956
author:
type: performance
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment