From 8952d70fd224963739a1af2668d580fc9d5b99b7 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 10 Sep 2026 16:45:22 +0200 Subject: [PATCH] :zap: Optimize get-profiles-for-file-comments query (#11622) Rewrite sql:file-comment-users to join comment with comment_thread and union the requesting profile id, then join the resulting small id set against profile. The previous "id IN (subquery) OR id = ?" forced a sequential scan over the whole profile table with a hashed subplan filter, taking ~1.9s on large instances. The semi-join lets the planner use profile_pkey, dropping the query to sub-millisecond time. UNION (not UNION ALL) keeps the previous dedup semantics when the requesting profile is also a commenter. AI-assisted-by: deepseek-flash --- backend/src/app/rpc/commands/comments.clj | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/src/app/rpc/commands/comments.clj b/backend/src/app/rpc/commands/comments.clj index b9c6002eb4..6e5c8f76dc 100644 --- a/backend/src/app/rpc/commands/comments.clj +++ b/backend/src/app/rpc/commands/comments.clj @@ -390,18 +390,26 @@ (def ^:private sql:file-comment-users "WITH available_profiles AS ( - SELECT DISTINCT owner_id AS id - FROM comment - WHERE thread_id IN (SELECT id FROM comment_thread WHERE file_id=?) + SELECT DISTINCT c.owner_id AS id + FROM comment c + JOIN comment_thread ct + ON ct.id = c.thread_id + WHERE ct.file_id = ?::uuid + ), + profile_ids AS ( + SELECT id FROM available_profiles + UNION + SELECT ?::uuid ) SELECT p.id, p.email, p.fullname AS name, - p.fullname AS fullname, + p.fullname, p.photo_id, p.is_active - FROM profile AS p - WHERE p.id IN (SELECT id FROM available_profiles) OR p.id=?") + FROM profile p + JOIN profile_ids AS x + ON x.id = p.id;") (defn get-file-comments-users [conn file-id profile-id]