From 057ac131365433fabbb2989ec88736b80bbe0947 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Thu, 26 Feb 2026 07:31:53 +1100 Subject: [PATCH 1/2] More optimization --- .../challenges/registrants-history.sql | 123 +++++++++--------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/sql/reports/challenges/registrants-history.sql b/sql/reports/challenges/registrants-history.sql index d53f35b..0550745 100644 --- a/sql/reports/challenges/registrants-history.sql +++ b/sql/reports/challenges/registrants-history.sql @@ -4,20 +4,11 @@ WITH submitter_role AS ( WHERE rr."nameLower" = 'submitter' LIMIT 1 ), -filtered_challenges AS ( +candidate_challenges AS MATERIALIZED ( 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 + c.status + FROM challenges."Challenge" c WHERE -- filter by billing account ( @@ -43,76 +34,82 @@ filtered_challenges AS ( AND ($3::text[] IS NULL OR c.status::text = ANY($3::text[])) -- exclude task challenge types from this report AND COALESCE(c."taskIsTask", false) = false +), +latest_phase AS MATERIALIZED ( + SELECT DISTINCT ON (cp."challengeId") + cp."challengeId", + cp."actualEndDate" + FROM challenges."ChallengePhase" cp + JOIN candidate_challenges cc + ON cc.id = cp."challengeId" + ORDER BY + cp."challengeId", + cp."scheduledEndDate" DESC +), +filtered_challenges AS MATERIALIZED ( + SELECT + cc.id, + cc.status, + lp."actualEndDate" AS "challengeCompletedDate" + FROM candidate_challenges cc + LEFT JOIN latest_phase lp + ON lp."challengeId" = cc.id + WHERE -- 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) + lp."actualEndDate" IS NOT NULL + AND ($4::timestamptz IS NULL OR lp."actualEndDate" >= $4::timestamptz) + AND ($5::timestamptz IS NULL OR lp."actualEndDate" <= $5::timestamptz) ) ) ), -registrants AS ( +registrants AS MATERIALIZED ( -- keep one submitter resource row per challenge/member - SELECT DISTINCT ON (fc.id, res."memberId") - fc.id as "challengeId", - fc.status as "challengeStatus", + SELECT + fc.id AS "challengeId", + fc.status AS "challengeStatus", fc."challengeCompletedDate", res."memberId", - res."memberHandle" as "registrantHandle" + MAX(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 - ORDER BY + GROUP BY fc.id, + fc.status, + fc."challengeCompletedDate", res."memberId" LIMIT 1000 -), -winners AS ( - -- aggregate winners only for already-selected registrants - SELECT - r."challengeId", - r."memberId", - MAX(cw.handle) as "winnerHandle" - FROM registrants r - JOIN challenges."ChallengeWinner" cw - ON cw."challengeId" = r."challengeId" - AND cw."userId"::text = r."memberId" - WHERE cw.placement = 1 - GROUP BY r."challengeId", r."memberId" -), -submissions AS ( - -- aggregate submissions only for already-selected registrants - SELECT - r."challengeId", - r."memberId", - MAX(sub."finalScore") as "finalScore", - BOOL_OR(sub.placement = 1) as "isWinner" - FROM registrants r - JOIN reviews.submission sub - ON sub."challengeId" = r."challengeId" - AND sub."memberId" = r."memberId" - GROUP BY r."challengeId", r."memberId" ) SELECT - registrants."challengeId", - registrants."challengeStatus", + r."challengeId", + r."challengeStatus", win."winnerHandle", - COALESCE(sub."isWinner", false) as "isWinner", + COALESCE(sub."isWinner", false) AS "isWinner", CASE - WHEN registrants."challengeStatus" = 'COMPLETED' - THEN registrants."challengeCompletedDate" + WHEN r."challengeStatus" = 'COMPLETED' + THEN r."challengeCompletedDate" ELSE null - END as "challengeCompletedDate", - registrants."registrantHandle", - ROUND(sub."finalScore", 2) as "registrantFinalScore" -FROM registrants -LEFT JOIN winners win - ON win."challengeId" = registrants."challengeId" - AND win."memberId" = registrants."memberId" -LEFT JOIN submissions sub - ON sub."challengeId" = registrants."challengeId" - AND sub."memberId" = registrants."memberId"; + END AS "challengeCompletedDate", + r."registrantHandle", + sub."registrantFinalScore" +FROM registrants r +LEFT JOIN LATERAL ( + SELECT MAX(cw.handle) AS "winnerHandle" + FROM challenges."ChallengeWinner" cw + WHERE cw."challengeId" = r."challengeId" + AND cw."userId"::text = r."memberId" + AND cw.placement = 1 +) win ON true +LEFT JOIN LATERAL ( + SELECT + BOOL_OR(s.placement = 1) AS "isWinner", + ROUND(MAX(s."finalScore"), 2) AS "registrantFinalScore" + FROM reviews.submission s + WHERE s."challengeId" = r."challengeId" + AND s."memberId" = r."memberId" +) sub ON true; From c3cef7f4d614022e97c11b8727eda90884e2e13b Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Wed, 18 Feb 2026 07:57:02 +1100 Subject: [PATCH 2/2] Further join optimization so that it completes large queries within the time alotted --- .../challenges/registrants-history.sql | 56 ++++++++----------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/sql/reports/challenges/registrants-history.sql b/sql/reports/challenges/registrants-history.sql index 0550745..d1ec062 100644 --- a/sql/reports/challenges/registrants-history.sql +++ b/sql/reports/challenges/registrants-history.sql @@ -4,11 +4,19 @@ WITH submitter_role AS ( WHERE rr."nameLower" = 'submitter' LIMIT 1 ), -candidate_challenges AS MATERIALIZED ( +filtered_challenges AS MATERIALIZED ( SELECT c.id, - c.status + c.status, + lp."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 + ) lp ON true WHERE -- filter by billing account ( @@ -34,29 +42,8 @@ candidate_challenges AS MATERIALIZED ( AND ($3::text[] IS NULL OR c.status::text = ANY($3::text[])) -- exclude task challenge types from this report AND COALESCE(c."taskIsTask", false) = false -), -latest_phase AS MATERIALIZED ( - SELECT DISTINCT ON (cp."challengeId") - cp."challengeId", - cp."actualEndDate" - FROM challenges."ChallengePhase" cp - JOIN candidate_challenges cc - ON cc.id = cp."challengeId" - ORDER BY - cp."challengeId", - cp."scheduledEndDate" DESC -), -filtered_challenges AS MATERIALIZED ( - SELECT - cc.id, - cc.status, - lp."actualEndDate" AS "challengeCompletedDate" - FROM candidate_challenges cc - LEFT JOIN latest_phase lp - ON lp."challengeId" = cc.id - WHERE -- filter by completion date bounds on the latest challenge phase end date - ( + AND ( ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) OR ( lp."actualEndDate" IS NOT NULL @@ -71,18 +58,19 @@ registrants AS MATERIALIZED ( fc.id AS "challengeId", fc.status AS "challengeStatus", fc."challengeCompletedDate", - res."memberId", - MAX(res."memberHandle") AS "registrantHandle" + registrant."memberId", + registrant."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 - GROUP BY - fc.id, - fc.status, - fc."challengeCompletedDate", - res."memberId" + JOIN LATERAL ( + SELECT + res."memberId", + MAX(res."memberHandle") AS "registrantHandle" + FROM resources."Resource" res + WHERE res."challengeId" = fc.id + AND res."roleId" = sr.id + GROUP BY res."memberId" + ) registrant ON true LIMIT 1000 ) SELECT