Skip to content

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

perf: traces table performance improvements (#1256)

Max Deichmann462e8a7b8cc1
diff --git a/web/package.json b/web/package.json
index 21e571b6dc..45cece5a82 100644
--- a/web/package.json
+++ b/web/package.json
@@ -18,6 +18,7 @@
"test:e2e": "dotenv -e ../.env -- playwright test",
"db:migrate": "DISABLE_ERD=false dotenv -e ../.env -- npx prisma migrate dev && npm run db:worker",
"db:reset": "dotenv -e ../.env npx -- prisma migrate reset",
+ "db:deploy": "dotenv -e ../.env npx -- prisma migrate deploy",
"db:seed": "dotenv -e ../.env -- npx prisma db seed",
"db:seed:examples": "dotenv -e ../.env -- npx prisma db seed -- --environment examples",
"release": "dotenv -e ../.env -- release-it",
diff --git a/web/prisma/migrations/20240226165118_add_observations_index/migration.sql b/web/prisma/migrations/20240226165118_add_observations_index/migration.sql
new file mode 100644
index 0000000000..f5e281a719
--- /dev/null
+++ b/web/prisma/migrations/20240226165118_add_observations_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "observations_project_id_start_time_type_idx" ON "observations"("project_id", "start_time", "type");
diff --git a/web/prisma/migrations/20240226182815_add_model_index/migration.sql b/web/prisma/migrations/20240226182815_add_model_index/migration.sql
new file mode 100644
index 0000000000..a11dbd2f31
--- /dev/null
+++ b/web/prisma/migrations/20240226182815_add_model_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "models_project_id_model_name_start_date_unit_idx" ON "models"("project_id", "model_name", "start_date", "unit");
diff --git a/web/prisma/migrations/20240226183642_add_observations_index/migration.sql b/web/prisma/migrations/20240226183642_add_observations_index/migration.sql
new file mode 100644
index 0000000000..c83df4a70f
--- /dev/null
+++ b/web/prisma/migrations/20240226183642_add_observations_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "observations_project_id_internal_model_start_time_unit_idx" ON "observations"("project_id", "internal_model", "start_time", "unit");
diff --git a/web/prisma/migrations/20240226202040_add_observations_trace_id_project_id_start_time_idx/migration.sql b/web/prisma/migrations/20240226202040_add_observations_trace_id_project_id_start_time_idx/migration.sql
new file mode 100644
index 0000000000..de41651a5b
--- /dev/null
+++ b/web/prisma/migrations/20240226202040_add_observations_trace_id_project_id_start_time_idx/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "observations_trace_id_project_id_start_time_idx" ON "observations"("trace_id", "project_id", "start_time");
diff --git a/web/prisma/migrations/20240226202041_add_observations_trace_id_project_id_type_start_time_idx/migration.sql b/web/prisma/migrations/20240226202041_add_observations_trace_id_project_id_type_start_time_idx/migration.sql
new file mode 100644
index 0000000000..ae64d9574e
--- /dev/null
+++ b/web/prisma/migrations/20240226202041_add_observations_trace_id_project_id_type_start_time_idx/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "observations_trace_id_project_id_type_start_time_idx" ON "observations"("trace_id", "project_id", "type", "start_time");
\ No newline at end of file
diff --git a/web/prisma/migrations/20240226203642_rewrite_observations_view/migration.sql b/web/prisma/migrations/20240226203642_rewrite_observations_view/migration.sql
new file mode 100644
index 0000000000..0745a999d1
--- /dev/null
+++ b/web/prisma/migrations/20240226203642_rewrite_observations_view/migration.sql
@@ -0,0 +1,50 @@
+CREATE OR REPLACE VIEW "observations_view" AS
+SELECT
+ o.*,
+ m.id AS "model_id",
+ m.start_date AS "model_start_date",
+ m.input_price,
+ m.output_price,
+ m.total_price,
+ m.tokenizer_config AS "tokenizer_config",
+ CASE
+ WHEN o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ o.prompt_tokens::decimal * m.input_price
+ ELSE
+ o.input_cost
+ END AS "calculated_input_cost",
+ CASE
+ WHEN o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ o.completion_tokens::decimal * m.output_price
+ ELSE
+ o.output_cost
+ END AS "calculated_output_cost",
+ CASE
+ WHEN o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ CASE
+ WHEN m.total_price IS NOT NULL AND o.total_tokens IS NOT NULL THEN
+ m.total_price * o.total_tokens
+ ELSE
+ o.prompt_tokens::decimal * m.input_price +
+ o.completion_tokens::decimal * m.output_price
+ END
+ ELSE
+ o.total_cost
+ END AS "calculated_total_cost",
+ CASE WHEN o.end_time IS NULL THEN NULL ELSE (EXTRACT(EPOCH FROM o."end_time") - EXTRACT(EPOCH FROM o."start_time"))::double precision END AS "latency"
+FROM
+ observations o
+LEFT JOIN LATERAL (
+ SELECT
+ models.*
+ FROM
+ models
+ WHERE (models.project_id = o.project_id OR models.project_id IS NULL)
+ AND models.model_name = o.internal_model
+ AND (models.start_date < o.start_time OR models.start_date IS NULL)
+ AND o.unit::TEXT = models.unit
+ ORDER BY
+ models.project_id ASC, -- in postgres, NULLs are sorted last when ordering ASC
+ models.start_date DESC NULLS LAST -- now, NULLs are sorted last when ordering DESC as well
+ LIMIT 1
+) m ON TRUE
diff --git a/web/prisma/schema.prisma b/web/prisma/schema.prisma
index 3d766883e7..229fd92cc1 100644
--- a/web/prisma/schema.prisma
+++ b/web/prisma/schema.prisma
@@ -261,6 +261,9 @@ model Observation {
prompt Prompt? @relation(fields: [promptId], onDelete: SetNull, references: [id])
@@unique([id, projectId])
+ @@index([projectId, internalModel, startTime, unit])
+ @@index([traceId, projectId, type, startTime])
+ @@index([traceId, projectId, startTime])
@@index([traceId])
@@index([type])
@@index(startTime)
@@ -268,6 +271,7 @@ model Observation {
@@index(projectId)
@@index(parentObservationId)
@@index(model)
+ @@index([projectId, startTime, type])
@@map("observations")
}
@@ -461,6 +465,7 @@ model Model {
tokenizerConfig Json? @map("tokenizer_config")
@@unique([projectId, modelName, startDate, unit])
+ @@index([projectId, modelName, startDate, unit])
@@index([projectId, modelName])
@@map("models")
}
@@ -495,7 +500,7 @@ view ObservationView {
promptId String? @map("prompt_id")
- // Model
+ // model fields
modelId String? @map("model_id")
inputPrice Decimal? @map("input_price")
outputPrice Decimal? @map("output_price")
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 2ba3f9f987..ba9d749a3f 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -463,9 +463,12 @@ function createTracesQuery(
orderByCondition: Sql,
) {
return Prisma.sql`
- WITH usage AS (
+ SELECT
+ ${select}
+ FROM
+ "traces" AS t
+ LEFT JOIN LATERAL (
SELECT
- trace_id,
sum(prompt_tokens) AS "promptTokens",
sum(completion_tokens) AS "completionTokens",
sum(total_tokens) AS "totalTokens",
@@ -473,59 +476,47 @@ function createTracesQuery(
FROM
"observations_view"
WHERE
- "trace_id" IS NOT NULL
+ "trace_id" = t.id
AND "type" = 'GENERATION'
AND "project_id" = ${projectId}
- ${observationTimeseriesFilter}
+ ${observationTimeseriesFilter}
GROUP BY
trace_id
- ),
- trace_latency AS (
+ ) AS u ON TRUE
+ LEFT JOIN LATERAL (
SELECT
- trace_id,
EXTRACT(EPOCH FROM COALESCE(MAX("end_time"), MAX("start_time"))) - EXTRACT(EPOCH FROM MIN("start_time"))::double precision AS "latency"
FROM
"observations"
WHERE
- "trace_id" IS NOT NULL
+ "trace_id" = t.id
AND "project_id" = ${projectId}
- ${observationTimeseriesFilter}
+ ${observationTimeseriesFilter}
GROUP BY
trace_id
- ),
- -- used for filtering
- scores_avg AS (
+ ) AS tl ON TRUE
+ LEFT JOIN LATERAL (
SELECT
- trace_id,
jsonb_object_agg(name::text, avg_value::double precision) AS scores_avg
FROM (
SELECT
- trace_id,
name,
avg(value) avg_value
FROM
scores
+ WHERE
+ trace_id = t.id
GROUP BY
- 1,
- 2
+ name
ORDER BY
- 1) tmp
- GROUP BY
- 1
- )
- SELECT
- ${select}
- FROM
- "traces" AS t
- LEFT JOIN usage AS u ON u.trace_id = t.id
- -- used for filtering
- LEFT JOIN scores_avg AS s_avg ON s_avg.trace_id = t.id
- LEFT JOIN trace_latency AS tl ON tl.trace_id = t.id
- WHERE
+ name
+ ) AS tmp
+ ) AS s_avg ON TRUE
+ WHERE
t."project_id" = ${projectId}
${searchCondition}
${filterCondition}
- ${orderByCondition}
+ ${orderByCondition}
LIMIT ${limit}
OFFSET ${page * limit}
`;