Faster endpoints

This commit is contained in:
Konstantin Wohlwend 2025-08-31 00:39:25 -07:00
parent 7f92f4a3d5
commit 46e8b80d2f
3 changed files with 13 additions and 1 deletions

View File

@ -0,0 +1,6 @@
-- It's very common to query by sessionId, userId, projectId, branchId, and eventStartedAt at the same time.
-- We can use a composite index to speed up the query.
-- Sadly we can't add this to the Prisma schema itself because Prisma does not understand composite indexes of JSONB fields.
-- So we have to add it manually.
-- (This is similar to the older idx_event_userid_projectid_branchid_eventstartedat index, but with sessionId added.)
CREATE INDEX idx_event_sessionid_userid_projectid_branchid_eventstartedat ON "Event" ((data->>'projectId'), (data->>'branchId'), (data->>'userId'), (data->>'sessionId'), "eventStartedAt");

View File

@ -49,6 +49,9 @@ export const sessionsCrudHandlers = createLazyProxy(() => createCrudHandlers(ses
? Prisma.sql`data->>'sessionId' = ANY(${Prisma.sql`ARRAY[${Prisma.join(refreshTokenObjs.map(s => s.id))}]`})`
: Prisma.sql`FALSE`}
AND "systemEventTypeIds" @> '{"$session-activity"}'
AND data->>'userId' = ${query.user_id}
AND data->>'projectId' = ${auth.tenancy.project.id}
AND COALESCE(data->>'branchId', 'main') = ${auth.tenancy.branchId}
GROUP BY data->>'sessionId'
)
SELECT e.data->>'sessionId' as "sessionId",

View File

@ -210,7 +210,10 @@ export const getUsersLastActiveAtMillis = async (projectId: string, branchId: st
const events = await prisma.$queryRaw<Array<{ userId: string, lastActiveAt: Date }>>`
SELECT data->>'userId' as "userId", MAX("eventStartedAt") as "lastActiveAt"
FROM ${sqlQuoteIdent(schema)}."Event"
WHERE data->>'userId' = ANY(${Prisma.sql`ARRAY[${Prisma.join(userIds)}]`}) AND data->>'projectId' = ${projectId} AND COALESCE("data"->>'branchId', 'main') = ${branchId} AND "systemEventTypeIds" @> '{"$user-activity"}'
WHERE data->>'userId' = ANY(${Prisma.sql`ARRAY[${Prisma.join(userIds)}]`})
AND data->>'projectId' = ${projectId}
AND COALESCE("data"->>'branchId', 'main') = ${branchId}
AND "systemEventTypeIds" @> '{"$user-activity"}'
GROUP BY data->>'userId'
`;