Skip to content

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

perf: add event table index and column type optimizations (#10280)

Steffen Schmitz713e176aab16
diff --git a/packages/shared/clickhouse/scripts/dev-tables.sh b/packages/shared/clickhouse/scripts/dev-tables.sh
index 30e72c25d9..498a1b969c 100644
--- a/packages/shared/clickhouse/scripts/dev-tables.sh
+++ b/packages/shared/clickhouse/scripts/dev-tables.sh
@@ -132,6 +132,7 @@ CREATE TABLE IF NOT EXISTS events
parent_span_id String,
start_time DateTime64(6),
+ start_time_unix UInt32 MATERIALIZED toUnixTimestamp(start_time),
end_time Nullable(DateTime64(6)),
-- Core properties
@@ -160,26 +161,35 @@ CREATE TABLE IF NOT EXISTS events
-- Model
model_id String,
provided_model_name String,
- model_parameters JSON,
+ model_parameters String,
+ model_parameters_json JSON MATERIALIZED model_parameters::JSON,
-- Usage
- provided_usage_details JSON(max_dynamic_paths=64, max_dynamic_types=8),
- usage_details JSON(
+ provided_usage_details Map(LowCardinality(String), UInt64),
+ provided_usage_details_json JSON(max_dynamic_paths=64, max_dynamic_types=8) MATERIALIZED provided_usage_details::JSON,
+ usage_details Map(LowCardinality(String), UInt64),
+ usage_details_json JSON(
max_dynamic_paths=64,
max_dynamic_types=8,
input UInt64,
output UInt64,
total UInt64,
- ),
- provided_cost_details JSON(max_dynamic_paths=64, max_dynamic_types=8),
- cost_details JSON(
+ ) MATERIALIZED usage_details::JSON,
+ provided_cost_details Map(LowCardinality(String), Decimal(18,12)),
+ provided_cost_details_json JSON(max_dynamic_paths=64, max_dynamic_types=8) MATERIALIZED provided_cost_details::JSON,
+ cost_details Map(LowCardinality(String), Decimal(18,12)),
+ cost_details_json JSON(
max_dynamic_paths=64,
max_dynamic_types=8,
input Decimal(18,12),
output Decimal(18,12),
total Decimal(18,12),
- ),
- total_cost Decimal(18, 12) ALIAS cost_details.total,
+ ) MATERIALIZED cost_details::JSON,
+
+ calculated_input_cost Decimal(18, 12) MATERIALIZED arraySum(mapValues(mapFilter(x -> positionCaseInsensitive(x.1, 'input') > 0, cost_details))),
+ calculated_output_cost Decimal(18, 12) MATERIALIZED arraySum(mapValues(mapFilter(x -> positionCaseInsensitive(x.1, 'output') > 0, cost_details))),
+ calculated_total_cost Decimal(18, 12) MATERIALIZED arraySum(mapValues(mapFilter(x -> positionCaseInsensitive(x.1, 'input') > 0 OR positionCaseInsensitive(x.1, 'output') > 0, cost_details))),
+ total_cost Decimal(18, 12) ALIAS cost_details_json.total,
-- I/O
input String CODEC(ZSTD(3)),
@@ -255,7 +265,7 @@ CREATE TABLE IF NOT EXISTS events
ENGINE = ReplacingMergeTree(event_ts, is_deleted)
-- ENGINE = (Replicated)ReplacingMergeTree(event_ts, is_deleted)
PARTITION BY toYYYYMM(start_time)
- ORDER BY (project_id, toUnixTimestamp(start_time), xxHash32(trace_id), span_id)
+ ORDER BY (project_id, start_time_unix, xxHash32(trace_id), span_id)
SAMPLE BY xxHash32(trace_id)
SETTINGS
index_granularity = 8192,
diff --git a/worker/src/backgroundMigrations/backfillEventsHistoric.ts b/worker/src/backgroundMigrations/backfillEventsHistoric.ts
index 90182ebb84..834bfe8bc4 100644
--- a/worker/src/backgroundMigrations/backfillEventsHistoric.ts
+++ b/worker/src/backgroundMigrations/backfillEventsHistoric.ts
@@ -320,7 +320,7 @@ export default class BackfillEventsHistoric implements IBackgroundMigration {
o.prompt_version,
o.internal_model_id AS model_id,
o.provided_model_name,
- o.model_parameters,
+ coalesce(o.model_parameters, '{}'),
o.provided_usage_details,
o.usage_details,
o.provided_cost_details,
diff --git a/worker/src/features/eventPropagation/handleEventPropagationJob.ts b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
index 3c6fa0843d..d1bbc2b569 100644
--- a/worker/src/features/eventPropagation/handleEventPropagationJob.ts
+++ b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
@@ -320,7 +320,7 @@ export const handleEventPropagationJob = async (
obs.prompt_version,
obs.internal_model_id AS model_id,
obs.provided_model_name,
- obs.model_parameters,
+ coalesce(obs.model_parameters, '{}'),
obs.provided_usage_details,
obs.usage_details,
obs.provided_cost_details,
diff --git a/worker/src/features/eventPropagation/handleExperimentBackfill.ts b/worker/src/features/eventPropagation/handleExperimentBackfill.ts
index f79e7d121b..9ab85702d5 100644
--- a/worker/src/features/eventPropagation/handleExperimentBackfill.ts
+++ b/worker/src/features/eventPropagation/handleExperimentBackfill.ts
@@ -196,7 +196,7 @@ export async function getRelevantObservations(
o.prompt_version AS prompt_version,
coalesce(o.internal_model_id, '') AS model_id,
coalesce(o.provided_model_name, '') AS provided_model_name,
- coalesce(o.model_parameters, '') AS model_parameters,
+ coalesce(o.model_parameters, '{}') AS model_parameters,
o.provided_usage_details AS provided_usage_details,
o.usage_details AS usage_details,
o.provided_cost_details AS provided_cost_details,