Using LLMs for query root-cause analysis: what's actually useful

The pattern that works, the pattern that doesn't, and the guardrails that keep an LLM from confidently mis-diagnosing a slow warehouse query.

You have a slow query. The dashboard renders in 12 seconds, users are complaining, and the query planner output is 40 lines of nested joins and merge steps. The obvious question in 2026 is: can an LLM just read this and tell me what is wrong?

The honest answer is “yes, but only if you wire it right”. An LLM that reads a query, a plan, and the warehouse’s system tables can produce a genuinely useful root-cause analysis. An LLM that only reads the query text and guesses is a hallucination waiting to be pushed into a Slack channel.

This post walks through the pattern that works, the pattern that doesn’t, and the guardrails that keep the LLM honest.

The pattern that doesn’t work

Paste a slow query into ChatGPT and ask “why is this slow.” The LLM will tell you something. It will sound plausible. It will suggest adding an index, or rewriting the join, or using a CTE.

The problem: the LLM cannot see your data. It does not know your table sizes, your sort keys, your existing indices, your partition scheme, your query planner output, or your recent system events. It is making a statistically-shaped guess from the SQL text alone. On simple queries it will land close. On the query that is actually causing your incident, it will confidently mis-diagnose.

We have seen this failure mode in every LLM-based query analyzer that only reads the SQL. It is not the LLM’s fault. It is a data problem: the LLM does not have the data needed to actually diagnose.

The pattern that works

The LLM has to see the same things a senior DBA would see. That is, at minimum:

  1. The query itself.
  2. The query plan (or EXPLAIN output) from the actual warehouse.
  3. Table statistics: row count, part count, sort key, partition column, existing projections and materialized views.
  4. Recent execution history: how long did this query pattern take last week? Has it degraded? Under what circumstances?
  5. System-level context: is the cluster under load right now? Are merges backed up? Is a schema migration in flight?

That is a real amount of data to gather. It is also exactly the data that a warehouse’s system tables already have. On ClickHouse, system.query_log, system.parts, system.merges, and system.tables between them cover every one of those points. On Snowflake, ACCOUNT_USAGE.QUERY_HISTORY plus INFORMATION_SCHEMA.TABLES covers it.

The pattern that works, in shape:

  1. Detect the slow query. Latency threshold or user report.
  2. Pull the plan and the stats. Run a fixed set of read-only queries against the system tables.
  3. Assemble a context document. Query text plus plan plus stats plus recent history plus system state.
  4. Send that context to the LLM with a tightly scoped prompt: “This query is slow. Given the plan and the stats below, name the most likely three causes and the fix for each.”
  5. The LLM outputs a structured response - each candidate cause with a confidence score, a citation to the specific line in the plan or the specific stat that supports it, and a proposed fix.
  6. A human reviews and applies. The LLM does not touch the warehouse.

The critical property of that pattern: every claim the LLM makes cites a specific piece of the context. If the LLM says “the sort key is not helping this query”, the response includes the sort key it saw and the line of the plan showing a full scan. The claim is grounded. A grounded claim is checkable.

The guardrails that keep the LLM honest

Three, in order of importance.

1. Every claim must cite the context. No claim without a citation. If the LLM cannot point to a specific system-table row or plan line, the claim is a guess and should be flagged as such. Prompt engineering can enforce this; the LLM outputs a JSON structure where each finding has a cite field that references a specific piece of the context.

2. The LLM cannot modify the warehouse. The tool is read-only. It reads the plan, reads the stats, produces a proposal. A human reads the proposal and executes the fix. This is defence-in-depth against a hallucination that suggests DROP TABLE.

3. Confidence intervals are visible. The LLM outputs a confidence score per finding (high / medium / low). Downstream consumers (dashboards, alerts, humans) treat low-confidence findings as investigatory leads, not conclusions.

What an LLM actually catches well

Three shapes of slowness the LLM-plus-context pattern catches reliably:

Missing sort key alignment. The query filters by a column that is not part of the sort key, the plan shows a full scan, and the LLM says “consider a projection or a re-sort.” This is a shape the LLM is good at because the pattern is visible in the plan.

Cardinality-explosion joins. The query has a JOIN that returns more rows than either input, indicating a many-to-many. The plan shows the row count blowing up mid-way through. The LLM catches this because the row-count numbers are literally in the plan.

Recent regression. The query used to run in 200 ms and now runs in 12 s. The query_log history shows the regression started three days ago. The LLM correlates the regression to a schema change or an insert-rate spike, if the context includes that.

What an LLM does not catch well

  • Business-logic bugs. “This query returns the wrong number” is not a query-performance question, and the LLM has no way to know the right number.
  • Cluster-level issues that are not in the system tables. Network partitions, disk failures, and container OOMs are outside the query context. Only your infrastructure observability catches those.
  • Novel query shapes. If the query is doing something genuinely unusual (a recursive CTE, a lateral view over an array, a window function inside a subquery), the LLM’s suggestion is more likely to be a generic “consider rewriting” than a specific fix.

Know what the tool is good at and stop expecting it to be an oracle. It is a fast pattern-matcher over a large context, and that shape covers about 60 percent of the slowness incidents we see. The other 40 percent are still human work.

The prompt shape

The prompt that keeps the LLM disciplined is short and structured. Roughly:

You are analyzing a slow ClickHouse query.

Query:
{QUERY_TEXT}

Plan (EXPLAIN PIPELINE):
{PLAN_TEXT}

Table stats:
{STATS_JSON}

Recent history of similar queries (last 30 days):
{HISTORY_JSON}

Cluster state right now:
{CLUSTER_STATE_JSON}

Task: Name the three most likely causes of the slowness. For each cause,
output a JSON object with fields: cause, confidence (high|medium|low),
evidence (which specific stat or plan line supports it), and proposed_fix.
Never suggest a cause you cannot cite from the context above. If the
context does not contain enough information, say so explicitly.

That last line - “if the context does not contain enough information, say so explicitly” - is the guardrail that keeps the LLM from hallucinating. LLMs will tell you when they are uncertain if you ask them to.

Where this fits in a BI stack

We built this pattern into Goni as the “Doctor” agent. It runs on the ClickHouse query log, detects slow queries, assembles the context described above, and produces a structured RCA that a data engineer reviews before applying. The mechanics are what this post described; the packaging is a UI on top of a ClickHouse-side observability layer. See goni.paysera.com.

The pattern is portable. If you run Snowflake or BigQuery, the shape is the same - collect plan, collect stats, collect history, hand it to the LLM with grounded-claim prompt engineering. The warehouse-specific work is the “collect” step; the LLM step is warehouse-agnostic.

Why this matters for a data team

An LLM without context is a plausible-sounding guess machine. An LLM with the plan, the stats, and the history is a genuine second pair of eyes on your slow queries. The difference is not the model; the difference is whether the prompt is grounded in real data. If you are experimenting with LLM tooling for RCA, start by asking whether every claim can cite the context. If it cannot, the tool is not ready for production yet.