Skip to main content

RAG Evaluation: Measure Retrieval Quality in SQL

RAG evaluation has two layers: did retrieval return the right chunks, and did the model write a good answer from them. This recipe measures the retrieval layer. It scores BM25, vector and hybrid runs over one SereneDB index against a set of labelled queries with recall@k, precision@k, hit rate, MRR and nDCG, all in SQL, and then checks how many of the exact nearest neighbours the vector index returns.

A RAG pipeline can fail without an error: the retriever returns plausible but wrong chunks, and the model answers fluently from them. Retrieval metrics catch that failure before anyone reads an answer.

Schema and sample data

Nine support-article chunks, each with a body and a three-dimensional emb embedding written by hand so the distances stay readable. In production emb holds the vector your embedding model returns, usually hundreds of dimensions wide. One inverted index holds the text and an hnsw vector index side by side. queries holds four test queries with their embeddings, qrels holds the labels, and runs collects what each retriever returned.

SQL
CREATE TEXT SEARCH DICTIONARY en AS    split_text(case := 'lower') | normalize_tokens('en_US.UTF-8', accent := false)    WITH (frequency, position);
CREATE TABLE chunks (id INTEGER PRIMARY KEY, body VARCHAR, emb FLOAT[3]);
CREATE INDEX chunks_idx ON chunks    USING inverted (id, body en, emb hnsw (metric = 'l2'));
INSERT INTO chunks VALUES(1, 'To reset your password, open Settings and choose Reset password.', [0.0, 1.0, 0.0]),(2, 'Accounts lock after five failed sign-in attempts; an admin can unlock them.', [0.1, 0.9, 0.1]),(3, 'Password rules: at least 12 characters, one number and one symbol.', [0.0, 0.8, 0.0]),(4, 'Refunds for annual plans are prorated by the unused months.', [1.0, 0.0, 0.0]),(5, 'Invoices are emailed on the first day of each billing cycle.', [0.9, 0.1, 0.0]),(6, 'Cancel a subscription at any time; the plan stays active until the period ends.', [0.8, 0.0, 0.1]),(7, 'Reports with many widgets can take a minute to load; narrow the date range.', [0.0, 0.1, 1.0]),(8, 'Cache query results to make dashboards open faster.', [0.0, 0.0, 0.9]),(9, 'Error E1042 means the API key has expired; create a new key.', [0.2, 0.6, 0.2]);
CREATE TABLE queries (qid INTEGER PRIMARY KEY, text VARCHAR, emb FLOAT[3]);
INSERT INTO queries VALUES(1, 'reset password', [0.05, 0.95, 0.0]),(2, 'refund annual plan', [0.95, 0.0, 0.05]),(3, 'slow dashboard', [0.0, 0.05, 0.95]),(4, 'error E1042', [0.5, 0.0, 0.5]);
CREATE TABLE qrels (qid INTEGER, doc_id INTEGER, rel INTEGER);
INSERT INTO qrels VALUES(1, 1, 2), (1, 2, 1),(2, 4, 2), (2, 6, 1),(3, 7, 2), (3, 8, 2),(4, 9, 2);
CREATE TABLE runs (system VARCHAR, qid INTEGER, doc_id INTEGER, rank INTEGER);

Retrieval first, answers second​

Evaluate RAG retrieval on its own before you judge any generated answer. A retrieval score needs no LLM, costs one SQL query and gives the same number every time you run it on the same data. It also sets a ceiling: a chunk that is not in the top k never reaches the prompt, so no prompt or model change recovers an answer that depends on it.

Once retrieval scores well, measure generation on top of it (see What retrieval metrics don't tell you).

Build a labelled query set​

A RAG evaluation dataset is two tables: the test queries and their relevance labels, often called a golden dataset, ground truth or qrels.

  • queries(qid, text, emb) holds each query's text and the embedding your application computes for it.
  • qrels(qid, doc_id, rel) lists the chunks that answer each query, with a graded label: 2 for a chunk that answers the query, 1 for a chunk that helps, and no row (read as 0) for everything else.

Where labels come from:

  • Take queries from your search or chat logs, so the set reflects what users ask, not what is easy to answer.
  • Label the chunks a person would want in the prompt. Clicks and thumbs-up from logs are a starting point; an LLM can draft labels, and a person reviews them.
  • Pool the labels: judge the top k of every retriever you compare, not only one. Labels drawn from a single retriever favour that retriever.
  • Cover each kind of query your users send (keywords and identifiers, natural-language questions, paraphrases), and report the number of queries next to every metric.

The sample set has four queries, one of each kind that separates retrievers: two keyword questions, a paraphrase that shares no words with its answers (slow dashboard), and an error code (error E1042).

Record one run per retriever​

A run is the ranked list one retriever returns for each query, stored as runs(system, qid, doc_id, rank). Run each retriever through the same statement your application uses, as a prepared statement executed once per query. Break ties on id so the ranks are the same every time.

BM25 run​

bm25_run searches body with the query text and keeps the top 10 by BM25. A bare string after @@ matches chunks that contain any of its words.

SQL
PREPARE bm25_run(INTEGER, VARCHAR) ASINSERT INTO runsSELECT 'bm25', $1, id, ROW_NUMBER() OVER (ORDER BY s DESC, id)FROM (    SELECT id, BM25(chunks_idx.tableoid) AS s    FROM chunks_idx    WHERE body @@ $2    ORDER BY s DESC, id    LIMIT 10) t;
EXECUTE bm25_run(1, 'reset password');
EXECUTE bm25_run(2, 'refund annual plan');
EXECUTE bm25_run(3, 'slow dashboard');
EXECUTE bm25_run(4, 'error E1042');

Vector run​

vector_run takes the query embedding as a FLOAT[3] parameter and returns the 10 nearest chunks by <-> (L2 distance) through the hnsw index. See Vector Search.

SQL
PREPARE vector_run(INTEGER, FLOAT[3]) ASINSERT INTO runsSELECT 'vector', $1, id, ROW_NUMBER() OVER (ORDER BY d, id)FROM (    SELECT id, emb <-> $2 AS d    FROM chunks_idx    ORDER BY d    LIMIT 10) t;
EXECUTE vector_run(1, [0.05, 0.95, 0.0]);
EXECUTE vector_run(2, [0.95, 0.0, 0.05]);
EXECUTE vector_run(3, [0.0, 0.05, 0.95]);
EXECUTE vector_run(4, [0.5, 0.0, 0.5]);

Hybrid run​

The hybrid run fuses the two runs already stored with Reciprocal Rank Fusion: each chunk scores the sum of 1 / (60 + rank) over the runs that returned it. Fusing stored runs scores exactly what the two retrievers returned; the window is their LIMIT 10.

SQL
INSERT INTO runsSELECT 'hybrid', qid, doc_id,       ROW_NUMBER() OVER (PARTITION BY qid ORDER BY SUM(1.0 / (60 + rank)) DESC, doc_id)FROM runsWHERE system IN ('bm25', 'vector')GROUP BY qid, doc_id;

The top 3 of each run:

SQL
SELECT qid, system, string_agg(doc_id::VARCHAR, ', ' ORDER BY rank) AS top_3FROM runsWHERE rank <= 3GROUP BY qid, systemORDER BY qid, system;
 qid | system | top_3
-----+--------+---------
   1 | bm25   | 1, 3
   1 | hybrid | 1, 3, 2
   1 | vector | 1, 2, 3
   2 | bm25   | 4, 6
   2 | hybrid | 4, 6, 5
   2 | vector | 4, 5, 6
   3 | hybrid | 7, 8, 9
   3 | vector | 7, 8, 9
   4 | bm25   | 9
   4 | hybrid | 9, 6, 8
   4 | vector | 6, 8, 5

BM25 returns nothing for slow dashboard: the answers say "load" and "dashboards", not "slow" or "dashboard". The vector run misses error E1042: an embedding carries the meaning of a sentence, not the identity of an error code. The hybrid run returns a relevant chunk first for every query.

Recall@k, MRR and nDCG in one query​

Each metric compares the top k of a run with the labels; here k is 3:

MetricPer queryWhat it tells you
recall@krelevant chunks in the top k ÷ all relevant chunks for the queryDid the prompt get everything it needs?
precision@krelevant chunks in the top k ÷ kHow much of the prompt is noise?
hit rate@k1 if any relevant chunk is in the top k, else 0Did the prompt get anything useful?
MRR (mean reciprocal rank)1 ÷ rank of the first relevant chunk, 0 if none is in the top kHow far down is the first useful chunk?
nDCG@kDCG ÷ ideal DCG, where DCG = Σ (2^rel − 1) ÷ log2(rank + 1)Are the best chunks at the top? Uses the graded labels

Each metric is averaged over the queries. The view eval_at_3 computes them per system and query. It starts from every system and every labelled query and LEFT JOINs the runs, so a query a retriever returned nothing for scores 0 instead of dropping out of the average. The ideal DCG comes from the labels themselves, sorted best first.

SQL
CREATE VIEW eval_at_3 ASWITH ideal AS (    SELECT qid,           COUNT(*) FILTER (WHERE rel > 0) AS n_relevant,           SUM((2 ^ rel - 1) / log2(pos + 1)) FILTER (WHERE pos <= 3) AS idcg    FROM (        SELECT qid, rel, ROW_NUMBER() OVER (PARTITION BY qid ORDER BY rel DESC) AS pos        FROM qrels    ) t    GROUP BY qid),judged AS (    SELECT r.system, r.qid, r.rank, COALESCE(q.rel, 0) AS rel    FROM runs r    LEFT JOIN qrels q ON q.qid = r.qid AND q.doc_id = r.doc_id    WHERE r.rank <= 3)SELECT s.system,       i.qid,       COUNT(*) FILTER (WHERE j.rel > 0)::DOUBLE / i.n_relevant AS recall,       COUNT(*) FILTER (WHERE j.rel > 0) / 3.0 AS precision,       COALESCE(MAX(1.0 / j.rank) FILTER (WHERE j.rel > 0), 0) AS rr,       COALESCE(SUM((2 ^ j.rel - 1) / log2(j.rank + 1)), 0) / i.idcg AS ndcgFROM (SELECT DISTINCT system FROM runs) sCROSS JOIN ideal iLEFT JOIN judged j ON j.system = s.system AND j.qid = i.qidGROUP BY s.system, i.qid, i.n_relevant, i.idcg;

Averaged per system:

SQL
SELECT system,       COUNT(*) AS queries,       AVG(recall)::DECIMAL(4,3) AS recall_at_3,       AVG(precision)::DECIMAL(4,3) AS precision_at_3,       AVG(CASE WHEN recall > 0 THEN 1 ELSE 0 END)::DECIMAL(4,3) AS hit_rate_at_3,       AVG(rr)::DECIMAL(4,3) AS mrr,       AVG(ndcg)::DECIMAL(4,3) AS ndcg_at_3FROM eval_at_3GROUP BY systemORDER BY system;
 system | queries | recall_at_3 | precision_at_3 | hit_rate_at_3 | mrr   | ndcg_at_3
--------+---------+-------------+----------------+---------------+-------+-----------
 bm25   |       4 |       0.625 |          0.333 |         0.750 | 0.750 |     0.707
 hybrid |       4 |       1.000 |          0.583 |         1.000 | 1.000 |     0.991
 vector |       4 |       0.750 |          0.500 |         0.750 | 0.750 |     0.741

Hybrid retrieval reaches recall@3 of 1.000 and nDCG@3 of 0.991 on these four queries; BM25 and vector each miss one query completely. Precision@3 stays below 1 for every system because no query has more than two relevant chunks, so at least one of the three slots holds a non-relevant chunk; 0.583 is the highest precision@3 these labels allow.

Compare BM25, vector and hybrid on your own queries​

Averages hide which queries a change helps and which it hurts. Put the per-query nDCG of each system side by side:

SQL
SELECT q.qid,       q.text,       MAX(e.ndcg) FILTER (WHERE e.system = 'bm25')::DECIMAL(4,3) AS bm25,       MAX(e.ndcg) FILTER (WHERE e.system = 'vector')::DECIMAL(4,3) AS vector,       MAX(e.ndcg) FILTER (WHERE e.system = 'hybrid')::DECIMAL(4,3) AS hybridFROM queries qJOIN eval_at_3 e ON e.qid = q.qidGROUP BY q.qid, q.textORDER BY q.qid;
 qid | text               | bm25  | vector | hybrid
-----+--------------------+-------+--------+--------
   1 | reset password     | 0.826 |  1.000 |  0.964
   2 | refund annual plan | 1.000 |  0.964 |  1.000
   3 | slow dashboard     | 0.000 |  1.000 |  1.000
   4 | error E1042        | 1.000 |  0.000 |  1.000

Each retriever wins where the others lose: BM25 on the error code, the vector run on the paraphrase. Hybrid wins both, and loses a little on reset password, where the BM25 branch lifts "Password rules" (a keyword match that does not answer the query) to rank 2.

Every variant you want to compare is one more run under its own system name: BM25 with other k1 and b values, another scorer from Scoring such as lm_dirichlet or dfi, a different RRF k or window, or a reranker. Prepare it, execute it for every query, and the view scores it with the others:

SQL
PREPARE lm_run(INTEGER, VARCHAR) ASINSERT INTO runsSELECT 'lm_dirichlet', $1, id, ROW_NUMBER() OVER (ORDER BY s DESC, id)FROM (    SELECT id, LM_DIRICHLET(chunks_idx.tableoid) AS s    FROM chunks_idx    WHERE body @@ $2    ORDER BY s DESC, id    LIMIT 10) t;

The relevance metrics above say whether the right chunks come back. A second number says whether the vector index returns the true nearest neighbours: its recall against exact search. Measure it with sdb_ann_force_exact, which answers a nearest-k query by scoring every row, and compare its top k with what the index returns under the settings you run in production.

This example uses 20,000 generated 64-dimension vectors, 20 query vectors, and the hnsw index with its default quantization. It runs in psql: \gexec executes each row of a query's result as a statement, so one line runs the prepared statement once per query vector.

sql
CREATE TABLE vecs (id INTEGER PRIMARY KEY, emb FLOAT[64]);

INSERT INTO vecs
SELECT i, list_transform(range(64), lambda j: (hash(i * 64 + j) % 2000) / 1000.0 - 1.0)::FLOAT[64]
FROM range(20000) t(i);

CREATE INDEX vecs_idx ON vecs USING inverted (id, emb hnsw (metric = 'l2'));

CREATE TABLE ann_queries AS
SELECT q AS qid, list_transform(range(64), lambda j: (hash(1000000 + q * 64 + j) % 2000) / 1000.0 - 1.0)::FLOAT[64] AS emb
FROM range(1, 21) t(q);

CREATE TABLE ann_runs (setting VARCHAR, qid INTEGER, id INTEGER);

PREPARE knn_top10(VARCHAR, INTEGER, FLOAT[64]) AS
INSERT INTO ann_runs
SELECT $1, $2, id
FROM vecs_idx
ORDER BY emb <-> $3
LIMIT 10;

Record the exact top 10 first, then the index's answer under each setting you want to compare:

sql
SET sdb_ann_force_exact = true;
SELECT format('EXECUTE knn_top10(%L, %s, %L)', 'exact', qid, emb) FROM ann_queries \gexec
RESET sdb_ann_force_exact;

SELECT format('EXECUTE knn_top10(%L, %s, %L)', 'default', qid, emb) FROM ann_queries \gexec

SET sdb_hnsw_ef_search = 10;
SELECT format('EXECUTE knn_top10(%L, %s, %L)', 'ef_search = 10', qid, emb) FROM ann_queries \gexec
SET sdb_hnsw_ef_search = 20;
SELECT format('EXECUTE knn_top10(%L, %s, %L)', 'ef_search = 20', qid, emb) FROM ann_queries \gexec
RESET sdb_hnsw_ef_search;

SET sdb_ann_rescore_width = 0;
SELECT format('EXECUTE knn_top10(%L, %s, %L)', 'rescore_width = 0', qid, emb) FROM ann_queries \gexec
RESET sdb_ann_rescore_width;

Recall@10 is the share of the exact top 10 that each setting returned:

sql
SELECT a.setting,
COUNT(DISTINCT a.qid) AS queries,
(COUNT(e.id)::DOUBLE / COUNT(*))::DECIMAL(4,3) AS recall_at_10
FROM ann_runs a
LEFT JOIN ann_runs e ON e.setting = 'exact' AND e.qid = a.qid AND e.id = a.id
WHERE a.setting <> 'exact'
GROUP BY a.setting
ORDER BY recall_at_10, a.setting;

On this data the result reads:

settingqueriesrecall_at_10
ef_search = 10200.965
ef_search = 20200.980
rescore_width = 0200.985
default201.000

Your numbers depend on your vectors, so measure on a sample of your own embeddings and the queries your application sends. The knobs to sweep, all documented under session settings:

  • sdb_hnsw_ef_search: the HNSW search beam;
  • sdb_ivf_search_nprobe: the IVF clusters scanned per query;
  • sdb_ann_rescore_width: how many candidates of a quantized index are re-scored at full precision (see Re-scoring);
  • the index's quant option: build a second index with another quantizer (see Quantization) and run the same comparison.

Index recall and relevance are different numbers. An index with recall@10 of 1.000 returns exactly the nearest vectors, and those can still be the wrong chunks if the embedding model places them close to the query. Track both: index recall against exact search, and recall@k and nDCG against your labels.

What retrieval metrics don't tell you​

Retrieval metrics stop at the prompt. Whether the answer is faithful to the retrieved chunks, relevant to the question and grounded in its sources needs a judge: an LLM scoring each answer, or people. RAG evaluation frameworks provide those judges and their metrics:

  • Ragas: faithfulness, response relevancy, context precision and context recall;
  • DeepEval: faithfulness, answer relevancy and contextual precision and recall;
  • MLflow GenAI evaluation: LLM-as-a-judge scorers for answers and agents;
  • TruLens: the RAG triad of context relevance, groundedness and answer relevance;
  • Arize Phoenix: faithfulness, hallucination and retrieval relevance evaluators.

They score an answer from the question and the retrieved contexts, so export those from the run you chose with COPY, one JSON line per query. The field names below are the ones Ragas uses (user_input, retrieved_contexts); rename the columns to match your framework:

SQL
COPY (    SELECT q.text AS user_input,           list(c.body ORDER BY r.rank) AS retrieved_contexts    FROM runs r    JOIN queries q ON q.qid = r.qid    JOIN chunks c ON c.id = r.doc_id    WHERE r.system = 'hybrid' AND r.rank <= 3    GROUP BY q.qid, q.text    ORDER BY q.qid) TO 'hybrid_contexts.ndjson' (FORMAT json);

Pitfalls​

  • Zero-hit queries. Average over every labelled query. Starting from the runs instead of the labels drops the queries a retriever returned nothing for, and inflates its score.
  • Ties. Break every ranking tie on a stable column such as id, or two runs of the same retriever give different ranks and different metrics.
  • Labels from one retriever. Pool the top k of every system before you label, or the system that produced the candidates wins by construction.
  • Too few queries. Four queries show the method; a decision needs enough queries of each kind your users send for a one-query change not to move the average.
  • Embedding model mismatch. Embed the queries with the same model and version as the chunks. A query embedded by another model lands in another space.
  • Stale runs. After you change a dictionary, an index option or the embedding model, re-run every system before you compare.
  • LLM judges. A judge model has its own biases: it can favour longer answers or answers from its own model family. Check a sample of its labels by hand.
  • Offline is not online. Labelled queries measure what you anticipated. Search logs, clicks and follow-up questions show what users actually ask; feed the queries that fail back into queries and qrels.

See also​