Skip to content

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

perf: use final for traces and observations api if no skip indexes are filtered (#5223)

Steffen Schmitze18ac03b4486
diff --git a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
index 04dfdd7a58..ea944158c6 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
@@ -383,6 +383,10 @@ export class FilterList {
return this.filters.find(predicate);
}
+ some(predicate: (filter: Filter) => boolean) {
+ return this.filters.some(predicate);
+ }
+
length() {
return this.filters.length;
}
diff --git a/web/src/features/public-api/server/observations.ts b/web/src/features/public-api/server/observations.ts
index 623deea265..96ae48bc31 100644
--- a/web/src/features/public-api/server/observations.ts
+++ b/web/src/features/public-api/server/observations.ts
@@ -25,6 +25,14 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {
const appliedFilter = chFilter.apply();
const traceFilter = chFilter.find((f) => f.clickhouseTable === "traces");
+ // This _must_ be updated if we add a new skip index column to the observations table.
+ // Otherwise, we will ignore it in most cases due to `FINAL`.
+ const shouldUseSkipIndexes = chFilter.some(
+ (f) =>
+ f.clickhouseTable === "observations" &&
+ ["trace_id"].some((skipIndexCol) => f.field.includes(skipIndexCol)),
+ );
+
const query = `
SELECT
id,
@@ -56,13 +64,12 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {
created_at,
updated_at,
event_ts
- FROM observations o
+ FROM observations o ${shouldUseSkipIndexes ? "" : "FINAL"}
${traceFilter ? `LEFT JOIN traces t ON o.trace_id = t.id AND t.project_id = o.project_id` : ""}
WHERE o.project_id = {projectId: String}
${traceFilter ? `AND t.project_id = {projectId: String}` : ""}
AND ${appliedFilter.query}
- ORDER BY event_ts desc
- LIMIT 1 by id, project_id
+ ${shouldUseSkipIndexes ? "ORDER BY start_time desc, event_ts desc LIMIT 1 by id, project_id" : "ORDER BY start_time DESC"}
${props.limit !== undefined && props.page !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
`;
diff --git a/web/src/features/public-api/server/traces.ts b/web/src/features/public-api/server/traces.ts
index 06052b6179..86924a6c6b 100644
--- a/web/src/features/public-api/server/traces.ts
+++ b/web/src/features/public-api/server/traces.ts
@@ -44,13 +44,25 @@ export const generateTracesForPublicApi = async (
(f.operator === ">=" || f.operator === ">"),
) as DateTimeFilter | undefined;
+ // This _must_ be updated if we add a new skip index column to the traces table.
+ // Otherwise, we will ignore it in most cases due to `FINAL`.
+ const shouldUseSkipIndexes = filter.some(
+ (f) =>
+ f.clickhouseTable === "traces" &&
+ ["user_id", "session_id", "metadata"].some((skipIndexCol) =>
+ f.field.includes(skipIndexCol),
+ ),
+ );
+
// If user provides an order we prefer it or fallback to timestamp as the default.
- // In both cases we append a t.event_ts desc order to pick the latest event in case of duplicates.
+ // In both cases we append a t.event_ts desc order to pick the latest event in case of duplicates
+ // if we want to use a skip index.
// This may still return stale information if the orderBy key was updated between traces or if a filter
// applies only to a stale value.
const chOrderBy =
(orderByToClickhouseSql(orderBy || [], orderByColumns) ||
- "ORDER BY t.timestamp desc") + ", t.event_ts desc";
+ "ORDER BY t.timestamp desc") +
+ (shouldUseSkipIndexes ? ", t.event_ts desc" : "");
const query = `
WITH observation_stats AS (
@@ -97,13 +109,13 @@ export const generateTracesForPublicApi = async (
o.observation_ids as observations,
COALESCE(o.latency_milliseconds / 1000, 0) as latency,
COALESCE(o.total_cost, 0) as totalCost
- FROM traces t
+ FROM traces t ${shouldUseSkipIndexes ? "" : "FINAL"}
LEFT JOIN observation_stats o ON t.id = o.trace_id AND t.project_id = o.project_id
LEFT JOIN score_stats s ON t.id = s.trace_id AND t.project_id = s.project_id
WHERE t.project_id = {projectId: String}
${filter.length() > 0 ? `AND ${appliedFilter.query}` : ""}
${chOrderBy}
- LIMIT 1 by t.id, t.project_id
+ ${shouldUseSkipIndexes ? "LIMIT 1 by t.id, t.project_id" : ""}
${props.limit !== undefined && props.page !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
`;