Skip to content

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

perf(clickhouse): scores query in events.all scans all partitions (#13336)

Valery Meleshkin76fdf528f15d
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index 27d0c9bf53..e9815af4ff 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -621,6 +621,12 @@ export type GetScoresForObservationsProps<
> = {
projectId: string;
observationIds: string[];
+ /**
+ * When provided, adds `AND s.timestamp >= minTimestamp - SCORE_TO_TRACE_OBSERVATIONS_INTERVAL`
+ * to the query so ClickHouse can prune monthly partitions and avoid full-table scans.
+ * Pass the minimum startTime of the observations whose scores you are fetching.
+ */
+ minTimestamp?: Date;
limit?: number;
offset?: number;
clickhouseConfigs?: ClickHouseClientConfigOptions;
@@ -638,6 +644,7 @@ export const getScoresForObservations = async <
const {
projectId,
observationIds,
+ minTimestamp,
limit,
offset,
clickhouseConfigs,
@@ -661,6 +668,7 @@ export const getScoresForObservations = async <
WHERE s.project_id = {projectId: String}
AND s.observation_id IN ({observationIds: Array(String)})
AND s.data_type IN ({dataTypes: Array(String)})
+ ${minTimestamp ? `AND s.timestamp >= {minTimestamp: DateTime64(3)} - ${SCORE_TO_TRACE_OBSERVATIONS_INTERVAL}` : ""}
ORDER BY s.event_ts DESC
LIMIT 1 BY s.id, s.project_id
${limit !== undefined && offset !== undefined ? `limit {limit: Int32} offset {offset: Int32}` : ""}
@@ -682,6 +690,9 @@ export const getScoresForObservations = async <
dataTypes: LISTABLE_SCORE_TYPES,
limit: limit,
offset: offset,
+ ...(minTimestamp
+ ? { minTimestamp: convertDateToClickhouseDateTime(minTimestamp) }
+ : {}),
},
tags: {
feature: "tracing",
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index d2cd6b1e5c..a305e1f62e 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -109,10 +109,30 @@ export async function getEventList(params: GetObservationsListParams) {
),
);
+ // Earliest observation startTime on this page — used as a partition-pruning
+ // lower bound for observation-level scores. Safe because
+ // score.timestamp >= observation.start_time - 1 hour
+ // (SCORE_TO_TRACE_OBSERVATIONS_INTERVAL).
+ const minStartTime = observations.reduce(
+ (min, obs) => (obs.startTime < min ? obs.startTime : min),
+ observations[0].startTime,
+ );
+
+ // For trace-level scores the bound must account for the fact that
+ // trace.timestamp can be up to 2 days before observation.start_time
+ // (OBSERVATIONS_TO_TRACE_INTERVAL). The events table does not carry
+ // trace timestamps, so we derive a safe lower bound:
+ // minStartTime - 2 days (earliest possible trace.timestamp)
+ // getScoresForTraces then applies its own 1-hour buffer, giving:
+ // s.timestamp >= (minStartTime - 2 days) - 1 hour
+ const TWO_DAYS_MS = 2 * 24 * 60 * 60 * 1000;
+ const minTraceTimestamp = new Date(minStartTime.getTime() - TWO_DAYS_MS);
+
const [scores, traceScores] = await Promise.all([
getScoresForObservations({
projectId: params.projectId,
observationIds: observations.map((observation) => observation.id),
+ minTimestamp: minStartTime,
excludeMetadata: true,
includeHasMetadata: true,
}),
@@ -120,6 +140,7 @@ export async function getEventList(params: GetObservationsListParams) {
? getScoresForTraces({
projectId: params.projectId,
traceIds,
+ timestamp: minTraceTimestamp,
excludeMetadata: true,
includeHasMetadata: true,
})