🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
perf: improve CH primary keys and order by (#3880)
Max Deichmann5b1704c95ed6
diff --git a/packages/shared/clickhouse/migrations/0001_traces.up.sql b/packages/shared/clickhouse/migrations/0001_traces.up.sqlindex 86ea51e7eb..bbbcf2120c 100644--- a/packages/shared/clickhouse/migrations/0001_traces.up.sql+++ b/packages/shared/clickhouse/migrations/0001_traces.up.sql@@ -20,6 +20,10 @@ CREATE TABLE traces (INDEX idx_res_metadata_key mapKeys(metadata) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_res_metadata_value mapValues(metadata) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplacingMergeTree Partition by toYYYYMM(timestamp)+PRIMARY KEY (+ project_id,+ toDate(timestamp)+)ORDER BY (project_id,toDate(timestamp),diff --git a/packages/shared/clickhouse/migrations/0002_observations.up.sql b/packages/shared/clickhouse/migrations/0002_observations.up.sqlindex ae97410fd6..73ae2b702e 100644--- a/packages/shared/clickhouse/migrations/0002_observations.up.sql+++ b/packages/shared/clickhouse/migrations/0002_observations.up.sql@@ -42,6 +42,12 @@ CREATE TABLE observations (INDEX idx_res_metadata_key mapKeys(metadata) TYPE bloom_filter() GRANULARITY 1,INDEX idx_res_metadata_value mapValues(metadata) TYPE bloom_filter() GRANULARITY 1) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)+PRIMARY KEY (+ project_id,+ `type`,+ toDate(start_time)+ )+ORDER BY (project_id,`type`,diff --git a/packages/shared/clickhouse/migrations/0003_scores.up.sql b/packages/shared/clickhouse/migrations/0003_scores.up.sqlindex c4105ce4a9..0a8d3e3722 100644--- a/packages/shared/clickhouse/migrations/0003_scores.up.sql+++ b/packages/shared/clickhouse/migrations/0003_scores.up.sql@@ -18,6 +18,11 @@ CREATE TABLE scores (INDEX idx_id id TYPE bloom_filter(0.001) GRANULARITY 1,INDEX idx_project_trace_observation (project_id, trace_id, observation_id) TYPE bloom_filter(0.001) GRANULARITY 1) ENGINE = ReplacingMergeTree Partition by toYYYYMM(timestamp)+PRIMARY KEY (+ project_id,+ toDate(timestamp),+ name+ )ORDER BY (project_id,toDate(timestamp),diff --git a/packages/shared/clickhouse/migrations/0004_observations_wide.down.sql b/packages/shared/clickhouse/migrations/0004_observations_wide.down.sqlindex 2f2eacfaf7..019b55bb74 100644--- a/packages/shared/clickhouse/migrations/0004_observations_wide.down.sql+++ b/packages/shared/clickhouse/migrations/0004_observations_wide.down.sql@@ -1,3 +1,3 @@-DROP TABLE IF EXISTS mv_observations_to_observations_wide;-DROP TABLE IF EXISTS mv_traces_to_observations_wide;+DROP TABLE IF EXISTS observations_to_observations_wide;+DROP TABLE IF EXISTS traces_to_observations_wide;DROP TABLE IF EXISTS observations_wide;diff --git a/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql b/packages/shared/clickhouse/migrations/0004_observations_wide.up.sqlindex 8cec261890..ae61f4df3f 100644--- a/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql+++ b/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql@@ -53,6 +53,11 @@ CREATE TABLE observations_widetrace_session_id Nullable(String),trace_event_ts DateTime64(3)) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)+PRIMARY KEY (+ project_id,+ `type`,+ toDate(start_time)+ )ORDER BY (project_id,`type`,@@ -60,7 +65,7 @@ ORDER BY (id);-CREATE MATERIALIZED VIEW mv_traces_to_observations_wide TO observations_wide AS+CREATE MATERIALIZED VIEW traces_to_observations_wide TO observations_wide ASSELECTargMax(t.`name`, o.event_ts) as trace_name,argMax(t.timestamp, o.event_ts) as trace_timestamp,@@ -110,9 +115,11 @@ SELECTargMax(o.event_ts, o.event_ts) as event_tsFROM traces tINNER JOIN observations o ON t.id = o.trace_id-GROUP BY o.id, o.project_id;+GROUP BY o.id, o.project_id+ORDER BY event_ts desc+LIMIT 1 by o.id;-CREATE MATERIALIZED VIEW mv_observations_to_observations_wide TO observations_wide AS+CREATE MATERIALIZED VIEW observations_to_observations_wide TO observations_wide ASSELECTargMax(t.timestamp, o.event_ts) as trace_timestamp,argMax(t.name, o.event_ts) as trace_name,@@ -162,6 +169,8 @@ SELECTargMax(o.event_ts, o.event_ts) as event_tsFROM observations oLEFT OUTER JOIN traces t ON t.id = o.trace_id-GROUP BY o.id, o.project_id;+GROUP BY o.id, o.project_id+ORDER BY event_ts desc+LIMIT 1 by o.id;diff --git a/packages/shared/clickhouse/migrations/0005_traces_wide.down.sql b/packages/shared/clickhouse/migrations/0005_traces_wide.down.sqlindex 75183dcc90..a2c2d63faf 100644--- a/packages/shared/clickhouse/migrations/0005_traces_wide.down.sql+++ b/packages/shared/clickhouse/migrations/0005_traces_wide.down.sql@@ -1,3 +1,3 @@-DROP TABLE IF EXISTS mv_observations_to_traces_wide;-DROP TABLE IF EXISTS mv_traces_to_traces_wide;+DROP TABLE IF EXISTS observations_to_traces_wide;+DROP TABLE IF EXISTS traces_to_traces_wide;DROP TABLE IF EXISTS traces_wide;diff --git a/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql b/packages/shared/clickhouse/migrations/0005_traces_wide.up.sqlindex ec9d1572c6..2486a4511d 100644--- a/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql+++ b/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql@@ -53,14 +53,19 @@ CREATE TABLE traces_widetrace_session_id Nullable(String),trace_event_ts DateTime64(3)) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)+PRIMARY KEY (+ project_id,+ `type`,+ toDate(trace_timestamp)+ )ORDER BY (project_id,`type`,- toDate(start_time),+ toDate(trace_timestamp),id);-CREATE MATERIALIZED VIEW mv_traces_to_traces_wide TO traces_wide AS+CREATE MATERIALIZED VIEW traces_to_traces_wide TO traces_wide ASSELECTargMax(t.`name`, t.event_ts) as trace_name,argMax(t.timestamp, t.event_ts) as trace_timestamp,@@ -109,9 +114,12 @@ SELECTargMax(o.event_ts, t.event_ts) as event_tsFROM traces tLEFT JOIN observations o ON t.id = o.trace_id-GROUP BY o.id, o.project_id;+GROUP BY o.id, o.project_id+ORDER BY event_ts desc+LIMIT 1 by o.id;-CREATE MATERIALIZED VIEW mv_observations_to_traces_wide TO traces_wide AS++CREATE MATERIALIZED VIEW observations_to_traces_wide TO traces_wide ASSELECTargMax(t.timestamp, o.event_ts) as trace_timestamp,argMax(t.name, o.event_ts) as trace_name,@@ -161,4 +169,7 @@ SELECTFROM observations oINNER JOIN traces t ON t.id = o.trace_idWHERE t.id IS NOT NULL-GROUP BY o.id, o.project_id;+GROUP BY o.id, o.project_id+ORDER BY event_ts desc+LIMIT 1 by o.id;+