From 62182ab9798375f3b876016ded4b8edb2a310c5c Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sat, 21 Nov 2020 17:29:24 +0530 Subject: [PATCH] Refactor get-lists query for speed --- queries.sql | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/queries.sql b/queries.sql index 96b65b6..7c4e10b 100644 --- a/queries.sql +++ b/queries.sql @@ -294,11 +294,16 @@ UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW() -- lists -- name: get-lists -SELECT COUNT(*) OVER () AS total, lists.*, COUNT(subscriber_lists.subscriber_id) AS subscriber_count - FROM lists LEFT JOIN subscriber_lists - ON (subscriber_lists.list_id = lists.id AND subscriber_lists.status != 'unsubscribed') - WHERE ($1 = 0 OR id = $1) - GROUP BY lists.id ORDER BY %s %s OFFSET $2 LIMIT (CASE WHEN $3 = 0 THEN NULL ELSE $3 END); +WITH ls AS ( + SELECT COUNT(*) OVER () AS total, lists.* FROM lists + WHERE ($1 = 0 OR id = $1) OFFSET $2 LIMIT $3 +), +counts AS ( + SELECT COUNT(*) as subscriber_count, list_id FROM subscriber_lists WHERE status != 'unsubscribed' GROUP BY list_id +) +SELECT ls.*, COALESCE(subscriber_count, 0) AS subscriber_count FROM ls + LEFT JOIN counts ON (counts.list_id = ls.id) ORDER BY %s %s; + -- name: get-lists-by-optin -- Can have a list of IDs or a list of UUIDs.