Skip to content

A platform for community discussion. Free, open, simple.

PERF: do not hydrate all candidates in big relation (#34553)

Sam37bf41b8deda
diff --git a/plugins/discourse-ai/lib/embeddings/semantic_search.rb b/plugins/discourse-ai/lib/embeddings/semantic_search.rb
index 0808049f6e9..3f6f5ba8f07 100644
--- a/plugins/discourse-ai/lib/embeddings/semantic_search.rb
+++ b/plugins/discourse-ai/lib/embeddings/semantic_search.rb
@@ -107,7 +107,8 @@ module DiscourseAi
end
def similar_topic_ids_to(query, candidates:)
- return [] if candidates.blank?
+ # NOTE: candidates may be a very large relation, be deliberate that only first is selected
+ return [] if candidates.limit(1).empty?
over_selection_limit = ::Topic::SIMILAR_TOPIC_LIMIT * OVER_SELECTION_FACTOR
asymmetric = true