Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf: improve scores table latency (#1315)

Max Deichmann213d9da88a1c
diff --git a/web/prisma/migrations/20240304222519_scores_add_index/migration.sql b/web/prisma/migrations/20240304222519_scores_add_index/migration.sql
new file mode 100644
index 0000000000..e181bf216b
--- /dev/null
+++ b/web/prisma/migrations/20240304222519_scores_add_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "scores_timestamp_idx" ON "scores"("timestamp");
diff --git a/web/prisma/schema.prisma b/web/prisma/schema.prisma
index 5a58eb2d17..0172fb7dce 100644
--- a/web/prisma/schema.prisma
+++ b/web/prisma/schema.prisma
@@ -301,6 +301,7 @@ model Score {
observation Observation? @relation(fields: [observationId], references: [id], onDelete: SetNull)
@@unique([id, traceId]) // used for upsert
+ @@index(timestamp)
@@index([value])
@@index([traceId], type: Hash)
@@index([observationId], type: Hash)
diff --git a/web/src/components/table/use-cases/scores.tsx b/web/src/components/table/use-cases/scores.tsx
index 9547837f23..6dc47ae5cb 100644
--- a/web/src/components/table/use-cases/scores.tsx
+++ b/web/src/components/table/use-cases/scores.tsx
@@ -62,7 +62,7 @@ export default function ScoresTable({
filter: filterState,
orderBy: orderByState,
});
- const totalCount = scores.data?.slice(1)[0]?.totalCount ?? 0;
+ const totalCount = scores.data?.totalCount ?? 0;
const filterOptions = api.scores.filterOptions.useQuery({
projectId,
@@ -161,7 +161,7 @@ export default function ScoresTable({
: {
isLoading: false,
isError: false,
- data: scores.data.map((t) => convertToTableRow(t)),
+ data: scores.data.scores.map((t) => convertToTableRow(t)),
}
}
pagination={{
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index a42ba3f594..15ab867d0d 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -44,27 +44,44 @@ export const scoresRouter = createTRPCRouter({
);
const scores = await ctx.prisma.$queryRaw<
- Array<Score & { traceName: string; totalCount: number }>
- >(Prisma.sql`
- SELECT
- s.id,
- s.name,
- s.value,
- s.timestamp,
- s.comment,
- s.trace_id as "traceId",
- s.observation_id as "observationId",
- t.name as "traceName",
- (count(*) OVER())::int AS "totalCount"
- FROM scores s
- JOIN traces t ON t.id = s.trace_id
- WHERE t.project_id = ${input.projectId}
- ${filterCondition}
- ${orderByCondition}
- LIMIT ${input.limit}
- OFFSET ${input.page * input.limit}
- `);
- return scores;
+ Array<Score & { traceName: string }>
+ >(
+ generateScoresQuery(
+ Prisma.sql`
+ s.id,
+ s.name,
+ s.value,
+ s.timestamp,
+ s.comment,
+ s.trace_id as "traceId",
+ s.observation_id as "observationId",
+ t.name as "traceName"`,
+ input.projectId,
+ filterCondition,
+ orderByCondition,
+ input.limit,
+ input.page,
+ ),
+ );
+
+ const scoresCount = await ctx.prisma.$queryRaw<
+ Array<{ totalCount: bigint }>
+ >(
+ generateScoresQuery(
+ Prisma.sql` count(*) AS "totalCount"`,
+ input.projectId,
+ filterCondition,
+ orderByCondition,
+ input.limit,
+ input.page,
+ ),
+ );
+
+ return {
+ scores,
+ totalCount:
+ scoresCount.length > 0 ? Number(scoresCount[0]?.totalCount) : 0,
+ };
}),
filterOptions: protectedProjectProcedure
.input(
@@ -284,3 +301,24 @@ export const scoresRouter = createTRPCRouter({
});
}),
});
+
+const generateScoresQuery = (
+ select: Prisma.Sql,
+ projectId: string,
+ filterCondition: Prisma.Sql,
+ orderCondition: Prisma.Sql,
+ limit: number,
+ page: number,
+) => {
+ return Prisma.sql`
+ SELECT
+ ${select}
+ FROM scores s
+ JOIN traces t ON t.id = s.trace_id
+ WHERE t.project_id = ${projectId}
+ ${filterCondition}
+ ${orderCondition}
+ LIMIT ${limit}
+ OFFSET ${page * limit}
+`;
+};