Skip to content

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

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.sql
index 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.sql
index 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.sql
index 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.sql
index 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.sql
index 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_wide
trace_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 AS
SELECT
argMax(t.`name`, o.event_ts) as trace_name,
argMax(t.timestamp, o.event_ts) as trace_timestamp,
@@ -110,9 +115,11 @@ SELECT
argMax(o.event_ts, o.event_ts) as event_ts
FROM traces t
INNER 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 AS
SELECT
argMax(t.timestamp, o.event_ts) as trace_timestamp,
argMax(t.name, o.event_ts) as trace_name,
@@ -162,6 +169,8 @@ SELECT
argMax(o.event_ts, o.event_ts) as event_ts
FROM observations o
LEFT 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.sql
index 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.sql
index 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_wide
trace_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 AS
SELECT
argMax(t.`name`, t.event_ts) as trace_name,
argMax(t.timestamp, t.event_ts) as trace_timestamp,
@@ -109,9 +114,12 @@ SELECT
argMax(o.event_ts, t.event_ts) as event_ts
FROM traces t
LEFT 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 AS
SELECT
argMax(t.timestamp, o.event_ts) as trace_timestamp,
argMax(t.name, o.event_ts) as trace_name,
@@ -161,4 +169,7 @@ SELECT
FROM observations o
INNER JOIN traces t ON t.id = o.trace_id
WHERE 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;
+