Skip to content

The only tool you need to know what is happening and how to fix it.

perf: let per-hash and per-conversation telemetry reads seek instead of sorting (#338)

FrameAutomata1d877b97cfb0
diff --git a/backend/app/migrations/sqlite_telemetry/0021_index_exceptions_hash_time.up.sql b/backend/app/migrations/sqlite_telemetry/0021_index_exceptions_hash_time.up.sql
new file mode 100644
index 00000000..46b70290
--- /dev/null
+++ b/backend/app/migrations/sqlite_telemetry/0021_index_exceptions_hash_time.up.sql
@@ -0,0 +1,2 @@
+DROP INDEX IF EXISTS idx_exceptions_project_hash;
+CREATE INDEX IF NOT EXISTS idx_exceptions_project_hash_recorded ON exception_stack_traces(project_id, exception_hash, recorded_at);
diff --git a/backend/app/migrations/sqlite_telemetry/0022_index_ai_traces_conversation_time.up.sql b/backend/app/migrations/sqlite_telemetry/0022_index_ai_traces_conversation_time.up.sql
new file mode 100644
index 00000000..0a24fe1c
--- /dev/null
+++ b/backend/app/migrations/sqlite_telemetry/0022_index_ai_traces_conversation_time.up.sql
@@ -0,0 +1,2 @@
+DROP INDEX IF EXISTS idx_ai_traces_project_conversation;
+CREATE INDEX IF NOT EXISTS idx_ai_traces_project_conversation_recorded ON ai_traces(project_id, conversation_id, recorded_at);
diff --git a/backend/app/migrations/telemetry_group_index_test.go b/backend/app/migrations/telemetry_group_index_test.go
index 61ffc74b..343c2acb 100644
--- a/backend/app/migrations/telemetry_group_index_test.go
+++ b/backend/app/migrations/telemetry_group_index_test.go
@@ -9,13 +9,14 @@ import (
_ "modernc.org/sqlite"
)
-// The grouped-list reads for these three tables filter
-// (project_id, <group>, recorded_at). An index that stops at the group column
-// leaves SQLite to seek the (project_id, recorded_at) index and re-filter the
-// group column across the whole window once per group, which is quadratic in
-// the number of groups and invisible to functional tests. Guard the covering
-// indexes so a later migration cannot narrow or drop them unnoticed.
-func TestTelemetryGroupIndexesCoverRecordedAt(t *testing.T) {
+// Every per-group telemetry read pairs a group column with the table's time
+// column -- as a second filter on the grouped lists, as the ORDER BY on the
+// per-group detail reads. An index that stops at the group column forces SQLite
+// to either re-filter the group across the whole window once per group, which is
+// quadratic in the number of groups, or seek the group and sort its entire
+// history. Both are unbounded and both are invisible to functional tests. Guard
+// the covering indexes so a later migration cannot narrow or drop one unnoticed.
+func TestTelemetryGroupIndexesCoverTimeColumn(t *testing.T) {
telemetryDB, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatalf("failed to open in-memory sqlite: %v", err)
@@ -27,10 +28,15 @@ func TestTelemetryGroupIndexesCoverRecordedAt(t *testing.T) {
t.Fatalf("telemetry migrations failed: %v", err)
}
- for _, tc := range []struct{ table, group string }{
- {"endpoints", "endpoint"},
- {"tasks", "task_name"},
- {"ai_traces", "trace_name"},
+ for _, tc := range []struct{ table, group, timeCol string }{
+ {"endpoints", "endpoint", "recorded_at"},
+ {"tasks", "task_name", "recorded_at"},
+ {"ai_traces", "trace_name", "recorded_at"},
+ {"ai_traces", "conversation_id", "recorded_at"},
+ {"exception_stack_traces", "exception_hash", "recorded_at"},
+ {"check_results", "check_id", "recorded_at"},
+ {"log_records", "service_name", "timestamp"},
+ {"metric_points", "name", "recorded_at"},
} {
var covered bool
if err := telemetryDB.QueryRow(`
@@ -38,12 +44,12 @@ func TestTelemetryGroupIndexesCoverRecordedAt(t *testing.T) {
SELECT 1 FROM pragma_index_list(?1) AS il
WHERE (SELECT name FROM pragma_index_info(il.name) WHERE seqno = 0) = 'project_id'
AND (SELECT name FROM pragma_index_info(il.name) WHERE seqno = 1) = ?2
- AND (SELECT name FROM pragma_index_info(il.name) WHERE seqno = 2) = 'recorded_at'
- )`, tc.table, tc.group).Scan(&covered); err != nil {
+ AND (SELECT name FROM pragma_index_info(il.name) WHERE seqno = 2) = ?3
+ )`, tc.table, tc.group, tc.timeCol).Scan(&covered); err != nil {
t.Fatalf("%s: failed to inspect indexes: %v", tc.table, err)
}
if !covered {
- t.Errorf("%s: no index leading with (project_id, %s, recorded_at); per-group queries will scan the whole window once per group", tc.table, tc.group)
+ t.Errorf("%s: no index leading with (project_id, %s, %s)", tc.table, tc.group, tc.timeCol)
}
}
}