feat: optimize traces list queries to avoid OOM#51
Merged
Conversation
The `traces` table sort key is `(OrgId, ServiceName, SpanName, toDateTime(Timestamp))`. `ServiceName` and `SpanName` sit between `OrgId` and the timestamp, so `ORDER BY Timestamp DESC` is not a sort-key prefix and ClickHouse cannot read-in-order. The single-stage list query scanned the whole window and materialized the heavy `SpanAttributes` / `ResourceAttributes` Map columns for every matching span *before* `LIMIT` discarded all but N, OOMing on busy orgs. Apply the same two-stage rewrite to `tracesListQuery` and `tracesRootListQuery` that was just landed on `logsListQuery`: 1. Extract a shared `baseWhere` so stage 1 and stage 2 filter identically. 2. Stage 1 selects only `Timestamp` and finds the (limit+offset)-th newest matching timestamp. The `+ offset` is the only delta from `logsListQuery` — traces support `OFFSET`, so the cutoff must cover every row the outer `OFFSET` will skip past. 3. Stage 2 gates the heavy projection on `Timestamp >= cutoff`, so the Map columns and `SpanAttributes['http.method']` etc. lookups materialize only for rows at/after the cutoff. Tests in `traces.test.ts` mirror the new `logsListQuery` tests: cutoff subquery shape (Timestamp-only, ORDER BY DESC, LIMIT=limit+offset), rootOnly predicate present inside the cutoff for the root variant, and filters applied identically across both stages. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This run croaked 😵 The workflow encountered an error before any progress could be reported. Please check the link below for details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What & Why
tracesListQueryandtracesRootListQueryhad the same ClickHouse OOM anti-pattern that was just fixed inlogsListQuery(fc4761f75).The
tracessort key is(OrgId, ServiceName, SpanName, toDateTime(Timestamp))(datasources.ts:213). BecauseServiceNameandSpanNamesit betweenOrgIdandTimestamp,ORDER BY Timestamp DESCis not a sort-key prefix — ClickHouse cannot read-in-order, so it scans the whole org+time window and materializes the heavySpanAttributes/ResourceAttributesMap(LowCardinality(String), String)columns for every matching span beforeLIMITdiscards all but N. Busy orgs hit the memory limit and the query OOMs.Fix
Two-stage query, mirroring
logsListQuery:baseWhereso stage 1 and stage 2 filter identically (includingcursorand, for the root variant,rootOnly).SELECT Timestamp AS ts FROM traces WHERE … ORDER BY ts DESC LIMIT (limit + offset), compiled viacompileCH(inner, {}, { skipFormat: true })so the outer compile substitutes params once.logsListQuery:LIMIT (limit + offset), not justLIMIT limit. Traces supportOFFSETfor paging; the cutoff must cover every row the outerOFFSETwill skip past, not just the slice returned.Timestamp >= (SELECT min(ts) FROM (<cutoffSql>)), so the Map columns /SpanAttributes.get('http.method' …)lookups in the root variant materialize only for rows at/after the cutoff.Tests
New file packages/query-engine/src/ch/queries/traces.test.ts — 17 tests mirroring the new
logsListQuerytests:FORMAT JSONcolumns: ["spanAttributes.http.method", …])Timestamp-only SELECT,ORDER BY ts DESC,LIMIT = limit + offset, noSpanAttributes/DurationmaterializationrootOnlypredicate inside the cutoff for the root variant (so the cheap scan matches the same population as the outer query)Verification
bun run testinpackages/query-engine— 15 files, 350 tests, all passbun typecheck— all touched packages type-check cleanly (the unrelated@maple/api@maple-dev/effect-sdk/cloudflareerror reproduces on a cleanmain)Reviewer notes
CH.rawExprbecause the DSL has no first-class subquery helper for(SELECT min(ts) FROM (…)). Same approach aslogsListQuery.tracesListQueryandtracesRootListQuery.tracesTimeseriesQuery/tracesBreakdownQueryaggregate up front so they don't carry heavy per-row projections — no fix needed there.🤖 Generated with Claude Code