From 95085f44abb459f25a72f3b6be651d02c24039f7 Mon Sep 17 00:00:00 2001 From: Rami Abdou Date: Sat, 14 Dec 2024 10:48:03 -0800 Subject: [PATCH 1/2] speed up query --- packages/core/src/modules/events/events.ts | 40 +++++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/core/src/modules/events/events.ts b/packages/core/src/modules/events/events.ts index c474f3bd..67fb10f4 100644 --- a/packages/core/src/modules/events/events.ts +++ b/packages/core/src/modules/events/events.ts @@ -198,26 +198,48 @@ type ListPastEventsInput = { export async function listPastEvents({ timezone }: ListPastEventsInput) { const records = await db + .with('attendees', (db) => { + return db + .selectFrom('eventAttendees') + .select(['eventId', ({ fn }) => fn.count('email').as('attendees')]) + .groupBy('eventId'); + }) + .with('pictures', (db) => { + return db + .selectFrom('eventAttendees') + .leftJoin('students', 'students.id', 'eventAttendees.studentId') + .select([ + 'eventId', + 'profilePicture', + ({ ref }) => { + const field = sql`row_number() over (partition by ${ref('eventId')} order by ${ref('eventAttendees.createdAt')} asc)`; + + return field.as('rank'); + }, + ]) + .where('students.profilePicture', 'is not', null); + }) .selectFrom('events') - .leftJoin('eventAttendees', 'eventAttendees.eventId', 'events.id') - .leftJoin('students', 'students.id', 'eventAttendees.studentId') + .leftJoin('attendees', 'attendees.eventId', 'events.id') + .leftJoin('pictures', (join) => { + return join + .onRef('pictures.eventId', '=', 'events.id') + .on('pictures.rank', '<=', 3); + }) .select([ 'events.endTime', 'events.id', 'events.name', 'events.recordingLink', 'events.startTime', + ({ fn }) => { + return fn.max('attendees.attendees').as('attendeesCount'); + }, ({ ref }) => { - const field = sql`string_agg(${ref('students.profilePicture')}, ',' order by ${ref('eventAttendees.createdAt')} asc)`; + const field = sql`string_agg(${ref('profilePicture')}, ',')`; return field.as('profilePictures'); }, - ({ fn }) => { - return fn - .count('eventAttendees.email') - .distinct() - .as('attendeesCount'); - }, ]) .where('events.endTime', '<=', new Date()) .where('events.hidden', '=', false) From ed335324cfbb52a73f00abb32026b10a8524e906 Mon Sep 17 00:00:00 2001 From: Rami Abdou Date: Sat, 14 Dec 2024 12:37:46 -0800 Subject: [PATCH 2/2] better --- packages/core/src/modules/events/events.ts | 65 ++++++++++++---------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/packages/core/src/modules/events/events.ts b/packages/core/src/modules/events/events.ts index 67fb10f4..4a44f7dd 100644 --- a/packages/core/src/modules/events/events.ts +++ b/packages/core/src/modules/events/events.ts @@ -198,34 +198,42 @@ type ListPastEventsInput = { export async function listPastEvents({ timezone }: ListPastEventsInput) { const records = await db - .with('attendees', (db) => { - return db - .selectFrom('eventAttendees') - .select(['eventId', ({ fn }) => fn.count('email').as('attendees')]) - .groupBy('eventId'); - }) - .with('pictures', (db) => { - return db - .selectFrom('eventAttendees') - .leftJoin('students', 'students.id', 'eventAttendees.studentId') - .select([ - 'eventId', - 'profilePicture', - ({ ref }) => { - const field = sql`row_number() over (partition by ${ref('eventId')} order by ${ref('eventAttendees.createdAt')} asc)`; - - return field.as('rank'); - }, - ]) - .where('students.profilePicture', 'is not', null); - }) .selectFrom('events') - .leftJoin('attendees', 'attendees.eventId', 'events.id') - .leftJoin('pictures', (join) => { - return join - .onRef('pictures.eventId', '=', 'events.id') - .on('pictures.rank', '<=', 3); - }) + .leftJoin( + (eb) => { + return eb + .selectFrom('eventAttendees') + .select(['eventId', ({ fn }) => fn.count('email').as('attendees')]) + .groupBy('eventId') + .as('attendees'); + }, + (join) => { + return join.onRef('attendees.eventId', '=', 'events.id'); + } + ) + .leftJoin( + (eb) => { + return eb + .selectFrom('eventAttendees') + .leftJoin('students', 'students.id', 'eventAttendees.studentId') + .select([ + 'eventId', + 'profilePicture', + ({ ref }) => { + const field = sql`row_number() over (partition by ${ref('eventId')} order by ${ref('eventAttendees.createdAt')} asc)`; + + return field.as('rank'); + }, + ]) + .where('students.profilePicture', 'is not', null) + .as('pictures'); + }, + (join) => { + return join + .onRef('pictures.eventId', '=', 'events.id') + .on('pictures.rank', '<=', 3); + } + ) .select([ 'events.endTime', 'events.id', @@ -262,8 +270,7 @@ export async function listPastEvents({ timezone }: ListPastEventsInput) { const profilePictures = (_profilePictures || '') .split(',') - .filter(Boolean) - .slice(0, 3); + .filter(Boolean); return { ...record,