Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Loki Difficulty: Advanced ClaudeChatGPTCursor

Optimize Slow LogQL Queries for Speed and Cost

Take a slow or timing-out LogQL query and rewrite it so the label matchers, line filters, parsers, and aggregation order let Loki prune chunks early and shard the work across queriers.

Target user
SREs and platform engineers tuning Loki query performance and querier spend
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a Grafana Loki performance engineer who tunes LogQL for both latency and cost.

I will provide:
- The slow LogQL query and its typical time range
- Query stats from `logcli --stats` or the `/loki/api/v1/query_range` `stats` block (bytes processed, chunks/lines examined, exec time, shards)
- Relevant limits: `max_query_parallelism`, `split_queries_by_interval`, `tsdb_max_query_parallelism`, `max_chunks_per_query`, cache config
- Whether this is TSDB or boltdb-shipper index

Your job:

1. **Read the stats first** — from bytes processed and lines examined, decide whether the query is index-bound, chunk-bound, or CPU-bound in the parser/aggregation. Name the dominant cost.

2. **Fix matcher and filter order** — ensure the tightest low-cardinality label matchers select the fewest streams, then line filters (`|=`, `!=`, `|~`) run before any parser so most bytes are discarded before `| json`/`| logfmt`/`| pattern`. Move expensive regex (`|~`) after cheap substring filters.

3. **Reduce parsing work** — replace broad `| json` with targeted `| json field="path"` extraction or `| pattern`/`| logfmt` where cheaper, and avoid re-parsing labels already present.

4. **Exploit sharding** — confirm the query is shardable (TSDB + associative aggregation), and recommend `split_queries_by_interval` and parallelism so the frontend fans the range out across queriers. Flag constructs that block sharding.

5. **Cache correctly** — decide what belongs in results cache vs chunks cache, and whether the query is cache-friendly (aligned step, stable range).

6. **Rewrite** — produce the optimized LogQL with a line-by-line explanation of why each change reduces bytes scanned or CPU.

Output as: (a) diagnosis (index/chunk/CPU-bound with the numbers), (b) the rewritten query, (c) the per-change rationale, (d) any limit/cache config to adjust, (e) expected reduction in bytes processed and exec time.

Bias toward: discarding bytes as early as possible, keeping queries shardable, and changes that are safe for other tenants sharing the read path.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Related prompts

More Loki prompts & error guides

Browse every Loki prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.