Skip to content

Commit 269d02b

Browse files
committed
findById performance improvements
1 parent 0b011d1 commit 269d02b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

backend/src/database/repositories/organizationRepository.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,21 +1152,19 @@ class OrganizationRepository {
11521152
const query = `
11531153
with leaf_segment_ids as (${segmentsSubQuery}),
11541154
member_data as (select a."organizationId",
1155-
count(distinct m.id) filter ( where mo."dateEnd" is null ) as "memberCount",
1155+
count(distinct a."memberId") as "memberCount",
11561156
count(distinct a.id) as "activityCount",
11571157
case
11581158
when array_agg(distinct a.platform) = array [null] then array []::text[]
11591159
else array_agg(distinct a.platform) end as "activeOn",
11601160
max(a.timestamp) as "lastActive",
11611161
min(a.timestamp) filter ( where a.timestamp <> '1970-01-01T00:00:00.000Z' ) as "joinedAt"
11621162
from leaf_segment_ids ls
1163-
left join activities a
1163+
join activities a
11641164
on a."segmentId" = ls.id and a."organizationId" = :id and
11651165
a."deletedAt" is null
1166-
left join members m on a."memberId" = m.id and m."deletedAt" is null
1167-
left join "memberOrganizations" mo
1168-
on m.id = mo."memberId" and mo."organizationId" = :id
1169-
left join "memberIdentities" mi on m.id = mi."memberId"
1166+
join members m on a."memberId" = m.id and m."deletedAt" is null
1167+
join "memberOrganizations" mo on m.id = mo."memberId" and mo."organizationId" = :id and mo."dateEnd" is null
11701168
group by a."organizationId"),
11711169
organization_segments as (select "organizationId", array_agg("segmentId") as "segments"
11721170
from "organizationSegments"

0 commit comments

Comments
 (0)