Course contentsShow
← Cloud and Infrastructure
Lesson 2097 of 2,98944. Observability - TracingFree lesson

Query Performance and Indexing

Optimizing trace queries: index strategies, sampling decisions, and query patterns.

Query Performance and Indexing

What you'll learn: How to optimize trace query performance through smart indexing strategies, sampling decisions, and efficient query patterns in distributed tracing backends.

Why Query Performance Matters

When you're debugging a production incident at 3 AM, waiting 30 seconds for a trace query is unacceptable. Your tracing backend stores millions of spans—finding the relevant ones quickly requires smart indexing, just like a library organizes books by category, author, and title rather than stacking them randomly.

Core Indexing Strategies

Tag-based indexing is fundamental. Your tracing backend indexes span attributes like service.name, http.status_code, and error=true. Think of these as the "search terms" you'll use most often. Index only what you query frequently—over-indexing wastes storage and slows writes.

Time-range partitioning is essential since traces are time-series data. Backends like Jaeger, Tempo, and Zipkin partition data by time windows (hourly, daily). Querying "last 15 minutes" only scans recent partitions, not your entire historical dataset.

Sampling Impact on Queries

Your sampling strategy from earlier lessons directly affects query performance. Head-based sampling (keeping 1% of traces) means smaller datasets and faster queries, but you might miss rare errors. Tail-based sampling keeps interesting traces (errors, slow requests) while discarding routine ones—better for debugging but requires more storage and indexing.

Efficient Query Patterns

Avoid wildcard queries on high-cardinality fields like trace_id or user_id without additional filters. Instead, combine indexed tags: service.name=checkout AND http.status_code=500 performs far better than scanning all traces.

Use time bounds aggressively—always specify the smallest reasonable time window. Querying "last 1 hour" instead of "last 7 days" reduces scan volume by 168x.

Key Takeaway: Optimize trace queries by indexing frequently-queried span attributes, leveraging time-based partitioning, aligning sampling strategies with query needs, and always using specific time ranges with indexed tag filters.