Skip to content

Commit 9e3a1f7

Browse files
dutowImTheKai
authored andcommitted
Fix N+1 query issues.
The previous rework was slow because it executed additional queries for each status cell. This change brings it back to only use preloaded information.
1 parent 6d180f4 commit 9e3a1f7

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

app/controllers/topics_controller.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class TopicsController < ApplicationController
55

66
def index
77
@search_query = nil
8-
base_query = apply_filters(Topic.includes(:creator))
8+
base_query = apply_filters(Topic.includes(:creator, creator_person: :default_alias, last_sender_person: :default_alias))
99

1010
apply_cursor_pagination(base_query)
1111
preload_topic_participants
@@ -240,7 +240,7 @@ def user_state_frame
240240
return head :unauthorized unless user_signed_in?
241241
return head :ok if topic_ids.empty?
242242

243-
@topics = Topic.includes(:creator).where(id: topic_ids)
243+
@topics = Topic.includes(:creator, creator_person: :default_alias, last_sender_person: :default_alias).where(id: topic_ids)
244244
preload_topic_states
245245
preload_note_counts
246246
preload_participation_flags
@@ -771,7 +771,7 @@ def filter_team_readers(topic, team_id:)
771771
end
772772

773773
def topics_base_query(search_query: nil)
774-
return apply_filters(Topic.includes(:creator)) if search_query.nil?
774+
return apply_filters(Topic.includes(:creator, creator_person: :default_alias, last_sender_person: :default_alias)) if search_query.nil?
775775

776776
cleaned_query = search_query.to_s.strip
777777
return Topic.none if cleaned_query.blank?
@@ -790,7 +790,6 @@ def build_search_query(query)
790790
union_sql = "(#{title_sql}) UNION (#{message_sql})"
791791

792792
Topic.where("topics.id IN (#{union_sql})")
793-
.includes(:creator)
794793
end
795794

796795
def viewing_since_param
@@ -930,7 +929,7 @@ def hydrate_topics_from_entries(entries)
930929
ids = entries.map { |e| e[:id] }
931930
return [] if ids.empty?
932931

933-
topics_map = Topic.includes(:creator).where(id: ids).index_by(&:id)
932+
topics_map = Topic.includes(:creator, creator_person: :default_alias, last_sender_person: :default_alias).where(id: ids).index_by(&:id)
934933
entries.filter_map do |entry|
935934
topic = topics_map[entry[:id]]
936935
next unless topic

app/views/topics/_status_cell.html.slim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ td.topic-title.status-border class=status_class id=dom_id(topic, "status_cell")
1010
.topic-byline
1111
- if creator
1212
span.topic-byline-item
13-
= image_tag creator.display_gravatar_url(size: 20), class: "topic-byline-avatar", alt: creator.name
13+
= image_tag creator.gravatar_url(size: 20), class: "topic-byline-avatar", alt: creator.name
1414
span.topic-byline-name = creator.name
1515
- if last_sender
1616
span.topic-byline-item.topic-byline-right
17-
= image_tag last_sender.display_gravatar_url(size: 20), class: "topic-byline-avatar", alt: last_sender.name
17+
= image_tag last_sender.gravatar_url(size: 20), class: "topic-byline-avatar", alt: last_sender.name
1818
span.topic-byline-name = last_sender.name
1919
.topic-row-footer
2020
.topic-footer-icons

app/views/topics/_topics_page.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
- role_label = participant.contributor_badge
3434
- badge_text = role_label ? "#{participant.name} (#{role_label})" : participant.name
3535
= link_to person_path(participant.email), class: "participant-avatar-link" do
36-
= image_tag participant.display_gravatar_url(size: 32), class: css_classes.join(" "), alt: participant.name, title: badge_text
36+
= image_tag participant.gravatar_url(size: 32), class: css_classes.join(" "), alt: participant.name, title: badge_text
3737
- if topic.participant_count > 5
3838
span.participants-count +#{topic.participant_count - 5}
3939
.topic-column.replies-col

0 commit comments

Comments
 (0)