From bb33a84058a2e2123e5b846d7d17642ef09478fc Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Wed, 31 Jul 2024 09:37:34 +0530 Subject: [PATCH] Optimising count query : Added option param to skip distinct from count query for members events API Ref https://linear.app/tryghost/issue/SLO-193/optimise-count-query-skip-distinct-from-count-query-for-members-events The member events endpoint have many queries like:- select count(distinct members_subscribe_events.id) as aggregate from `members_subscribe_events` where `members_subscribe_events`.`created_at` < '2024-07-30 11:30:39' In these queries, distinct is not required as id is a primary key. Skipping distinct would improve the performance. This PR will changed the query to:- select count(*) as aggregate from `members_subscribe_events` where `members_subscribe_events`.`created_at` < '2024-07-30 11:30:39' --- .../lib/repositories/EventRepository.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ghost/members-api/lib/repositories/EventRepository.js b/ghost/members-api/lib/repositories/EventRepository.js index eb3574e391..a44541b50c 100644 --- a/ghost/members-api/lib/repositories/EventRepository.js +++ b/ghost/members-api/lib/repositories/EventRepository.js @@ -151,6 +151,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'newsletter'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -193,6 +194,7 @@ module.exports = class EventRepository { 'stripeSubscription.stripePrice.stripeProduct.product' ], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -245,6 +247,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -277,6 +280,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -314,6 +318,7 @@ module.exports = class EventRepository { 'tagAttribution' ], filter: 'subscriptionCreatedEvent.id:null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -368,6 +373,7 @@ module.exports = class EventRepository { 'tagAttribution' ], filter: 'member_id:-null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -416,6 +422,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'post', 'parent'], filter: 'member_id:-null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -449,6 +456,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'link', 'link.post'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -537,6 +545,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'post'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -571,6 +580,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'email'], filter: filterStr, + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -613,6 +623,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'email'], filter: 'delivered_at:-null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -655,6 +666,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'email'], filter: 'opened_at:-null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -697,6 +709,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'email'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -730,6 +743,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member', 'email'], filter: 'failed_at:-null+custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter), @@ -772,6 +786,7 @@ module.exports = class EventRepository { ...options, withRelated: ['member'], filter: 'custom:true', + useBasicCount: true, mongoTransformer: chainTransformers( // First set the filter manually replaceCustomFilterTransformer(filter),