Skip to content

Commit e83e0f7

Browse files
authored
PERF: Improve performance of DiscourseTagging.hidden_tag_names (#36636)
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 a11f86c commit e83e0f7

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
@@ -640,7 +640,21 @@ def self.filter_visible(query, guardian = nil)
640640
end
641641

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

646660
def self.permitted_group_ids_query(guardian = nil)

0 commit comments

Comments
 (0)