ai newsFr., 4. Dez.
EilmeldungAlibaba Releases Qwen4-35B-A3B: Open-Weight MoE Model With 1M Context

Text-to-SQL Benchmarks: How to Measure LLM Accuracy on Real SQL Questions

Text-to-SQL demos are easy to make impressive. You type "show me revenue by region last quarter", the model writes a neat query, and the chart appears. The hard part comes later, when real users ask real questions against a real schema and you need to know: how often is the generated SQL actually r

dev.toFreitag 05:57 · 04.12.4 Min. Lesezeit
Bild: dev.to

Text-to-SQL demos are easy to make impressive. You type "show me revenue by region last quarter", the model writes a neat query, and the chart appears. The hard part comes later, when real users ask real questions against a real schema and you need to know: how often is the generated SQL actually right?

You cannot answer that by trying a few prompts by hand. You need a benchmark. This post explains how to build one for your own database, what to measure, and which mistakes make Text-to-SQL numbers look better than they are.

Public datasets such as Spider and BIRD are useful for comparing models in general. But your production accuracy depends on things those datasets do not contain:

A model that scores well on a leaderboard can still fail most of your users' questions. The only number that matters is accuracy on your questions against your schema.

Start with questions people actually ask. Good sources are support tickets, analyst request queues, saved reports and dashboard filters. Aim for 30 to 150 questions to begin with. A small, honest set beats a large, artificial one.

Make sure the set covers different difficulty levels:

A common mistake is to store one "correct" SQL query per question and compare the model's SQL text with it. Two queries can look completely different and return the same result, or look almost identical and return different results.

Instead, for each question store the expected result: the rows and values a correct query returns on a fixed snapshot of the database. Then judge the model by execution accuracy: run its SQL and compare the result set with the reference.

"Accuracy" as a single number hides the information you need. Record each run in one of these buckets:

Silently wrong is the most dangerous bucket. A failed query is annoying, but the user knows something went wrong. A silently wrong query returns a believable number, and someone may make a decision based on it. Track it separately and try to drive it toward zero, even at the cost of more refusals.

Run the benchmark with the simplest possible setup first: the question plus a raw schema dump, sent to the model. This shows how much each later improvement actually helps.

As a real example, in a 32-question benchmark against a SQL Server sample database, a local 7B coding model (qwen2.5-coder:7b running in Ollama) started at 17 of 32 correct, or 53%. Of the rest, 7 were silently wrong, 8 failed and 2 were unsafe. That baseline looked nothing like the demo.

With a baseline in place, improve the pipeline step by step and re-run the benchmark after every change:

The step-by-step build behind these numbers is documented in AI Database Agent, Part 1: a first Text-to-SQL baseline and Part 3: validated SQL.

A benchmark you run once is a report. A benchmark you run on every change is a safety net. Build a small console app or test project that:

Run it whenever you change the prompt, the model, the schema description or the validation rules. Prompt changes that "obviously improve" things regularly break questions that used to work.

Keep the output of every benchmark run in the same shape so you can compare runs over weeks and months. A useful report has three parts:

The diff list is the part people read. When a prompt change fixes four questions but breaks two others, you see both immediately and can decide whether the trade is worth it. Store the reports in your repository next to the code, so every pull request that touches the AI pipeline shows its effect on accuracy.

There is no universal target, but a reasonable bar for internal analytics tools is:

If you are building the pipeline itself, How to Build Text-to-SQL with ASP.NET Core covers the architecture: schema context, generation, parsing, read-only execution and result formatting.

How many questions do I need? Start with 30. That is enough to spot big differences between approaches. Grow toward 100 to 200 as you see real usage.

Should I use an LLM to judge correctness? Use execution-based comparison first. An LLM judge can help with free-text explanations, but for SQL results, comparing rows is cheaper and more reliable.

Do local models work for Text-to-SQL? Yes, for many internal use cases. With good schema context and validation, a 7B coding model can be useful, and it keeps your data on your own hardware. Measure it on your own benchmark before deciding.

Text-to-SQL quality is not a feeling, it is a number you can measure. Collect real questions, store expected results, classify every outcome, and re-run the benchmark on every change. Most of all, watch the silently wrong bucket, because that is where trust is lost.

More practical guides on Text-to-SQL, RAG and AI agents in.NET are on AI Engineering Foundry.

Quelle: dev.to (dev.to). Original öffnen