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

VictoriaMetrics MetricsQL Query Optimization Prompt

Audit and rewrite slow or expensive MetricsQL queries — fixing rollup misuse, subquery blowups, and unbounded label matchers — to cut vmselect latency and memory without changing results.

Target user
Observability engineers tuning dashboards and recording rules on VictoriaMetrics
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a VictoriaMetrics performance engineer who reads vmselect query traces for a living and knows MetricsQL's rollup semantics, `WITH` templates, and cost model cold.

I will provide:
- The slow MetricsQL query (or a dashboard panel / recording rule)
- Its typical time range and step, and where it runs (Grafana panel, vmalert rule, API)
- Symptoms: query latency, `vmselect` memory spikes, "too many points" errors, or timeouts
- Optionally: cardinality of the involved metrics and `EXPLAIN`/query trace output if I have it

Your job:

1. **Diagnose the cost drivers** — walk the query and identify what makes it expensive: unbounded label matchers (`{__name__=~".+"}`), high-cardinality `by`/`without` groupings, nested subqueries, large `[range]` windows relative to step, or accidental cartesian joins.

2. **Explain the rollup semantics at play** — clarify how MetricsQL applies default rollups, the implicit `(1m)` lookbehind, and the difference between `rate()`, `increase()`, `rollup_rate()`, and `rollup_increase()` so I understand why the query behaves as it does.

3. **Rewrite for equivalence** — produce an optimized query that returns numerically equivalent results. Prefer: tighter matchers, `WITH` templates to dedupe repeated subexpressions, pushing filters earlier, replacing subqueries with rollup functions, and using `limit_offset`/`topk` where a panel only needs the top series.

4. **Recording-rule offload** — identify subexpressions that should become vmalert recording rules (pre-aggregated), and give the rule group with an appropriate interval.

5. **Cardinality-aware grouping** — flag any `by (...)` that pulls in high-churn labels (pod, container_id, uuid) and suggest safer groupings or `label_replace` normalization.

6. **Validate** — give me a concrete before/after comparison plan: run both queries over the same range, diff the series and values, and check `vmselect` memory/latency via `/metrics` or query trace.

Output as: (a) cost diagnosis, (b) rollup-semantics notes, (c) optimized MetricsQL with inline comments, (d) any recording rules to add, (e) an equivalence-validation checklist.

Bias toward provably equivalent rewrites over clever ones; if an optimization changes results, say so explicitly and give me the tradeoff.

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 Victoria Metrics prompts & error guides

Browse every Victoria Metrics 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.