From 9dfb5612bc803cb6db1f9ac6e1214d84e82ed434 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Tue, 11 Nov 2025 12:19:49 +1100 Subject: [PATCH] Optimization for better speed to fix issue noted by Topgear --- sql/reports/topgear/hourly.sql | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/sql/reports/topgear/hourly.sql b/sql/reports/topgear/hourly.sql index d8d6dc2..c35471a 100644 --- a/sql/reports/topgear/hourly.sql +++ b/sql/reports/topgear/hourly.sql @@ -1,19 +1,17 @@ -WITH bill AS ( - SELECT - cb."challengeId" AS challenge_id, - MAX(cb."billingAccountId") FILTER (WHERE cb."billingAccountId" IS NOT NULL) AS billing_account_id - FROM challenges."ChallengeBilling" cb - WHERE cb."billingAccountId" = '80000062' - GROUP BY cb."challengeId" -), -base_challenges AS ( +WITH base_challenges AS ( SELECT c.*, - b.billing_account_id + ba.billing_account_id FROM challenges."Challenge" c - JOIN bill b - ON b.challenge_id = c.id + JOIN LATERAL ( + SELECT + MAX(cb."billingAccountId") AS billing_account_id + FROM challenges."ChallengeBilling" cb + WHERE cb."challengeId" = c.id + AND cb."billingAccountId" = '80000062' + ) ba ON TRUE WHERE c."createdAt" >= now() - interval '4 months' + AND ba.billing_account_id IS NOT NULL ), project_details AS ( SELECT