From ab8c2d7fef5e42812fee83b939903fe5b72dae75 Mon Sep 17 00:00:00 2001 From: Stephen Hand Date: Wed, 11 Sep 2024 17:15:51 +0100 Subject: [PATCH] Optimise profile query --- hrm-domain/hrm-core/profile/sql/profile-get-sql.ts | 6 +----- hrm-domain/lambdas/search-index-consumer/index.ts | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/hrm-domain/hrm-core/profile/sql/profile-get-sql.ts b/hrm-domain/hrm-core/profile/sql/profile-get-sql.ts index 6b2ce85bc..0c292d5de 100644 --- a/hrm-domain/hrm-core/profile/sql/profile-get-sql.ts +++ b/hrm-domain/hrm-core/profile/sql/profile-get-sql.ts @@ -60,10 +60,6 @@ export const getProfilesSqlBase = (selectTargetProfilesQuery: string) => ` LEFT JOIN "ProfileSections" pps ON pps."profileId" = profile.id AND pps."accountSid" = profile."accountSid" GROUP BY pps."profileId" ), - - HasRelatedContacts AS ( - SELECT COUNT(*) > 0 as "hasContacts", "profileId" FROM "Contacts" GROUP BY "profileId" - ) SELECT tp.*, @@ -77,7 +73,7 @@ export const getProfilesSqlBase = (selectTargetProfilesQuery: string) => ` LEFT JOIN RelatedProfileFlags rpf ON profiles.id = rpf."profileId" LEFT JOIN RelatedProfileSections rps ON profiles.id = rps."profileId" -- Remove this hack once we have limited contact view permissions - LEFT JOIN HasRelatedContacts hrc ON profiles.id = hrc."profileId" + LEFT JOIN LATERAL (SELECT COUNT(*) > 0 as "hasContacts", "profileId", "accountSid" FROM "Contacts" c WHERE profiles.id = c."profileId" AND profiles."accountSid" = c."accountSid" GROUP BY "profileId", "accountSid") hrc ON true `; export const getProfileByIdSql = getProfilesSqlBase(` diff --git a/hrm-domain/lambdas/search-index-consumer/index.ts b/hrm-domain/lambdas/search-index-consumer/index.ts index 54e70b160..4152b2960 100644 --- a/hrm-domain/lambdas/search-index-consumer/index.ts +++ b/hrm-domain/lambdas/search-index-consumer/index.ts @@ -73,7 +73,7 @@ export const handler = async (event: SQSEvent): Promise => { case 'contact': { const { contact } = message; console.info( - `[generalised-search-contacts]: Indexing Request Acknowledged By ES. Account SID: ${accountSid}, Case ID: ${ + `[generalised-search-contacts]: Indexing Request Acknowledged By ES. Account SID: ${accountSid}, Contact ID: ${ contact.id }, Updated / Created At: ${ contact.updatedAt ?? contact.createdAt