Unverified Commit c52a36e8 authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Fix N+1 query when displaying events

When displaying events we would load the target of those events, then
render the entire data using our Markdown pipeline. This pipeline would
eventually request the author of every target, leading to an additional
query being executed for every target to get the author.

To fix this we now eager load the author of the event's target. In my
local environment this reduces the number of queries to display a
project's Atom feed from 40 to 24 queries.

See https://gitlab.com/gitlab-org/gitlab-ce/issues/36878 for more
information.
parent 9429e8ac
...@@ -72,7 +72,7 @@ class Event < ActiveRecord::Base ...@@ -72,7 +72,7 @@ class Event < ActiveRecord::Base
# We're using preload for "push_event_payload" as otherwise the association # We're using preload for "push_event_payload" as otherwise the association
# is not always available (depending on the query being built). # is not always available (depending on the query being built).
includes(:author, :project, project: :namespace) includes(:author, :project, project: :namespace)
.preload(:target, :push_event_payload) .preload(:push_event_payload, target: :author)
end end
scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) } scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
......
---
title: Fix N+1 query when displaying events
merge_request:
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