Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize OTel Trace ID queries with skip index #725

Merged
merged 4 commits into from
Feb 29, 2024

Conversation

SpencerTorres
Copy link
Collaborator

@SpencerTorres SpencerTorres commented Feb 22, 2024

Adds the optimization requested in #724

This optimization is only included in the SQL when OTel is enabled since the table is specific to OTel.

Here's the new SQL that is generated for Trace ID queries:

+ WITH
+ '207c6fb96eda8541559a283678587c86' as trace_id,
+ (SELECT min(Start) FROM "default"."otel_traces_trace_id_ts" WHERE TraceId = trace_id) as trace_start,
+ (SELECT max(End) + 1 FROM "default"."otel_traces_trace_id_ts" WHERE TraceId = trace_id) as trace_end
SELECT
"TraceId" as traceID, "SpanId" as spanID, "ParentSpanId" as parentSpanID, "ServiceName" as serviceName,
"SpanName" as operationName, "Timestamp" as startTime, multiply("Duration", 0.000001) as duration,
arrayMap(key -> map('key', key, 'value',"SpanAttributes"[key]), mapKeys("SpanAttributes")) as tags,
arrayMap(key -> map('key', key, 'value',"ResourceAttributes"[key]), mapKeys("ResourceAttributes")) as serviceTags
FROM "default"."otel_traces"
WHERE
- traceID = '207c6fb96eda8541559a283678587c86'
+ traceID = trace_id
+ AND startTime >= trace_start
+ AND startTime <= trace_end 
LIMIT 1000

Here's a rough graph of query duration before and after:
image

asimpson
asimpson previously approved these changes Feb 28, 2024
Copy link
Contributor

@asimpson asimpson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

Just two nits, up to you on how you want to handle them 😎

@SpencerTorres
Copy link
Collaborator Author

Good points on the otel functions, pushed a change to clean up the imports/references 👌

@asimpson asimpson enabled auto-merge (squash) February 29, 2024 15:32
Copy link
Contributor

@asimpson asimpson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏆 nicely done

@asimpson asimpson merged commit d3b5eae into grafana:main Feb 29, 2024
15 of 16 checks passed
@tanner-bruce
Copy link

FYI this query scans the trace_id_ts table twice.

Compare with:

(SELECT (min(start), max(end) + 1) FROM tracing.otel_traces_trace_id_ts WHERE trace_id = ID) as bounds,
...
AND startTime >= bounds.1
AND startTime <= bounds.2 

@SpencerTorres
Copy link
Collaborator Author

FYI this query scans the trace_id_ts table twice.

Very good point, this PR was simply using the linked query on the original issue, but this seems like another optimization we can add. Thanks!

@SpencerTorres SpencerTorres deleted the optimize-trace-id-query branch June 17, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants