Skip to content

Commit 5b19143

Browse files
authored
PERF: Improve performance of DiscourseTagging.hidden_tag_names (stable backport) (#36638)
This backports #36636 to stable. Improve performance of `DiscourseTagging.hidden_tag_names` by using its own query. Previously it added `where.not(id: ` to the query from `DiscourseTagging.visible_tags`. This simplifies the hidden_tag_names query by just directly running the inverse query, to find names of tags which have at least one TagGroupPermission but do not have any for permitted tag groups. This query gets run every time tags are displayed on a page, and the goal here is to improve performance in the case tracked in internal topic /t/168944. On that customer's site, this reduces the query time from ~11ms to ~5ms.
1 parent f012da0 commit 5b19143

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/discourse_tagging.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,21 @@ def self.filter_visible(query, guardian = nil)
644644
end
645645

646646
def self.hidden_tag_names(guardian = nil)
647-
guardian&.is_staff? ? [] : Tag.where.not(id: visible_tags(guardian).select(:id)).pluck(:name)
647+
if guardian&.is_staff?
648+
[]
649+
else
650+
# Hidden tags have at least one TagGroupPermission but must not have any for permitted groups
651+
Tag
652+
.where(id: TagGroupMembership.joins(tag_group: :tag_group_permissions).select(:tag_id))
653+
.where.not(
654+
id:
655+
TagGroupPermission
656+
.joins(tag_group: :tag_group_memberships)
657+
.where(group_id: permitted_group_ids_query(guardian))
658+
.select("tag_group_memberships.tag_id"),
659+
)
660+
.pluck(:name)
661+
end
648662
end
649663

650664
def self.permitted_group_ids_query(guardian = nil)

0 commit comments

Comments
 (0)