Commit 998aa2a5 authored by Adam Cohen's avatar Adam Cohen Committed by David Kim

Remove N+1 query from Issue::Metrics#record!

parent d78df37c
......@@ -24,6 +24,10 @@ class Issue::Metrics < ApplicationRecord
private
def issue_assigned_to_list_label?
issue.labels.any? { |label| label.lists.present? }
# Avoid another DB lookup when issue.labels are empty by adding a guard clause here
# We can't use issue.labels.empty? because that will cause a `Label Exists?` DB lookup
return false if issue.labels.length == 0 # rubocop:disable Style/ZeroLengthPredicate
issue.labels.includes(:lists).any? { |label| label.lists.present? }
end
end
---
title: Remove N+1 query from Issue::Metrics#record
merge_request: 60589
author:
type: performance
......@@ -80,5 +80,20 @@ RSpec.describe Issue::Metrics do
expect(metrics.first_added_to_board_at).to be_like_time(time)
end
end
describe "#record!" do
it "does not cause an N+1 query" do
label = create(:label)
subject.update!(label_ids: [label.id])
control_count = ActiveRecord::QueryRecorder.new { Issue::Metrics.find_by(issue: subject).record! }.count
additional_labels = create_list(:label, 4)
subject.update!(label_ids: additional_labels.map(&:id))
expect { Issue::Metrics.find_by(issue: subject).record! }.not_to exceed_query_limit(control_count)
end
end
end
end
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