From 9d6e71f327ba170f662574d29bde9a82f4e92202 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Wed, 25 Feb 2026 17:41:15 +1100 Subject: [PATCH] Optimization for registrants history report --- .../challenges/registrants-history.sql | 117 ++++++++++++------ 1 file changed, 78 insertions(+), 39 deletions(-) diff --git a/sql/reports/challenges/registrants-history.sql b/sql/reports/challenges/registrants-history.sql index 943e6c5..bad5bb2 100644 --- a/sql/reports/challenges/registrants-history.sql +++ b/sql/reports/challenges/registrants-history.sql @@ -1,47 +1,86 @@ +WITH submitter_role AS ( + SELECT rr.id + FROM resources."ResourceRole" rr + WHERE rr."nameLower" = 'submitter' + LIMIT 1 +), +filtered_challenges AS ( + SELECT + c.id, + c.status, + latest_phase."actualEndDate" as "challengeCompletedDate" + FROM + challenges."Challenge" c + LEFT JOIN LATERAL ( + SELECT cp."actualEndDate" + FROM challenges."ChallengePhase" cp + WHERE cp."challengeId" = c.id + ORDER BY cp."scheduledEndDate" DESC + LIMIT 1 + ) latest_phase ON true + WHERE + -- filter by billing account + ( + $1::text[] IS NULL + OR EXISTS ( + SELECT 1 + FROM challenges."ChallengeBilling" cb + WHERE cb."challengeId" = c.id + AND cb."billingAccountId" = ANY($1::text[]) + ) + ) + -- filter by excluding billing account + AND ( + $2::text[] IS NULL + OR NOT EXISTS ( + SELECT 1 + FROM challenges."ChallengeBilling" cb + WHERE cb."challengeId" = c.id + AND cb."billingAccountId" = ANY($2::text[]) + ) + ) + -- filter by challenge status + AND ($3::text[] IS NULL OR c.status::text = ANY($3::text[])) + -- filter by completion date bounds on the latest challenge phase end date + AND ( + ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) + OR ( + latest_phase."actualEndDate" IS NOT NULL + AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz) + AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz) + ) + ) +) SELECT - c.id as "challengeId", - c.status as "challengeStatus", + registrants."challengeId", + registrants."challengeStatus", cw.handle as "winnerHandle", CASE WHEN sub.placement = 1 THEN true ELSE false END as "isWinner", CASE - WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate" + WHEN registrants."challengeStatus" = 'COMPLETED' + THEN registrants."challengeCompletedDate" ELSE null END as "challengeCompletedDate", - res."memberHandle" as "registrantHandle", + registrants."registrantHandle", ROUND(sub."finalScore", 2) as "registrantFinalScore" -FROM - challenges."Challenge" c -LEFT JOIN LATERAL ( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 -) latest_phase ON true -LEFT JOIN - resources."Resource" res ON c.id = res."challengeId" -LEFT JOIN - resources."ResourceRole" rr on res."roleId" = rr.id -LEFT JOIN - challenges."ChallengeWinner" cw ON c.id = cw."challengeId" AND cw."userId"::text=res."memberId" -LEFT JOIN - reviews."submission" sub on sub."memberId"=res."memberId" AND sub."challengeId"=c.id -LEFT JOIN - challenges."ChallengeBilling" cb on cb."challengeId"=c.id -WHERE rr.name = 'Submitter' - -- filter by billing account - AND ($1::text[] IS NULL OR cb."billingAccountId" = ANY($1::text[])) - -- filter by excluding billing account - AND ($2::text[] IS NULL OR cb."billingAccountId" <> ANY($2::text[])) - -- filter by challenge status - AND ($3::text[] IS NULL OR c.status::text= ANY($3::text[])) - -- filter by completion date bounds on the latest challenge phase end date - AND ( - ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) - OR ( - latest_phase."actualEndDate" IS NOT NULL - AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz) - AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz) - ) - ) +FROM ( + SELECT + fc.id as "challengeId", + fc.status as "challengeStatus", + fc."challengeCompletedDate", + res."memberId", + res."memberHandle" as "registrantHandle" + FROM filtered_challenges fc + JOIN submitter_role sr ON true + JOIN resources."Resource" res + ON res."challengeId" = fc.id + AND res."roleId" = sr.id + LIMIT 1000 +) registrants +LEFT JOIN challenges."ChallengeWinner" cw + ON cw."challengeId" = registrants."challengeId" + AND cw."userId"::text = registrants."memberId" +LEFT JOIN reviews."submission" sub + ON sub."challengeId" = registrants."challengeId" + AND sub."memberId" = registrants."memberId" LIMIT 1000;