mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-13 21:01:21 +08:00
Faster endpoints
This commit is contained in:
parent
7f92f4a3d5
commit
46e8b80d2f
@ -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");
|
||||
@ -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",
|
||||
|
||||
@ -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'
|
||||
`;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user