Skip to content

Commit 563834f

Browse files
author
Uroš Marolt
committed
Use optimized version of identities for memberExists method to speed it up
1 parent 3964714 commit 563834f

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

backend/src/database/repositories/memberRepository.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,22 @@ class MemberRepository {
389389
const currentTenant = SequelizeRepository.getCurrentTenant(options)
390390

391391
const query = `
392-
with identities as (select "memberId",
393-
array_agg(distinct platform) as identities,
394-
jsonb_object_agg(platform, usernames) as username
392+
with identities as (select mi."memberId",
393+
array_agg(distinct mi.platform) as identities,
394+
jsonb_object_agg(mi.platform, mi.usernames) as username
395395
from (select "memberId",
396396
platform,
397-
jsonb_agg(username) over (partition by "memberId", platform) as usernames
398-
from "memberIdentities") ranked
399-
group by "memberId")
397+
array_agg(username) as usernames
398+
from (select "memberId",
399+
platform,
400+
username,
401+
"createdAt",
402+
row_number() over (partition by "memberId", platform order by "createdAt" desc) =
403+
1 as is_latest
404+
from "memberIdentities" where "tenantId" = :tenantId) sub
405+
where is_latest
406+
group by "memberId", platform) mi
407+
group by mi."memberId")
400408
select m."id",
401409
m."displayName",
402410
m."attributes",

0 commit comments

Comments
 (0)