Domain 3 β€” Module 5 of 7 71%
26 of 28 overall
Domain 3: Implement AI Capabilities in Database Solutions Free ⏱ ~13 min read

Vector Search: Distance, ANN, and ENN

Implement vector search with VECTOR_DISTANCE and VECTOR_SEARCH β€” understand approximate vs exact nearest neighbours and evaluate search performance.

Finding the closest vectors

Simple explanation

Imagine finding the nearest coffee shop.

Exact nearest neighbour (ENN) checks every single coffee shop in the city and guarantees the absolute closest one. Accurate, but slow if there are 100,000 shops. Approximate nearest neighbour (ANN) uses a smart shortcut β€” it checks only nearby neighbourhoods and finds a coffee shop that is very likely the closest (99%+ accuracy) but in a fraction of the time.

For most applications, ANN is the right choice β€” it is dramatically faster and almost always finds the correct answer.

ANN vs ENN

ANN vs ENN β€” ANN is correct for most production scenarios
FeatureANN (Approximate)ENN (Exact)
Accuracy~95-99% recall (may miss a few results)100% β€” guaranteed correct top-K
SpeedMilliseconds (uses vector index)Seconds to minutes (full scan)
Requires index?Yes β€” DiskANN vector indexNo β€” works without index
Scales toBillions of vectorsThousands to low millions
Use whenProduction search, real-time queriesSmall datasets, validation, accuracy-critical analysis

VECTOR_SEARCH function

-- ANN search: find top 10 similar products (uses vector index)
SELECT TOP (10) WITH APPROXIMATE
    ProductId, Name,
    VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS distance
FROM Products
ORDER BY VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector);

-- ENN search: exact nearest neighbours (brute force, no WITH APPROXIMATE)
SELECT TOP 10 ProductId, Name,
    VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS Distance
FROM Products
ORDER BY Distance ASC;

Combining vector search with filters

-- Search within a specific category (pre-filter)
SELECT TOP (10) WITH APPROXIMATE
    ProductId, Name,
    VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS distance
FROM Products
WHERE CategoryId = 5
ORDER BY VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector);

-- Post-filter: search first, then filter
SELECT * FROM (
    SELECT TOP (50) WITH APPROXIMATE
        ProductId, Name, Price,
        VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS distance
    FROM Products
    ORDER BY VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector)
) filtered
WHERE Price < 100
ORDER BY distance ASC;
Exam tip: Pre-filter vs post-filter

Pre-filtering applies WHERE clauses BEFORE vector search β€” the ANN index searches only within the filtered subset. More accurate but may not use the vector index efficiently if the subset is small.

Post-filtering runs vector search on the full dataset and filters results afterward. Simpler and uses the index fully, but you may need to request more results (top 50) to get enough after filtering (top 10).

The exam may present a scenario where vector search returns irrelevant results from the wrong category β€” the fix is adding a pre-filter or post-filter.

Evaluating search performance

Key metrics

MetricWhat It MeasuresTarget
Recall@KProportion of true top-K results found by ANNgreater than 95%
Latency (p50/p95)Query response timeless than 100ms for production
Queries per secondThroughput under loadDepends on workload
Index build timeTime to create/rebuild vector indexHours for millions of vectors

Testing recall

-- Compare ANN results with ENN (ground truth) for a sample
-- 1. Get exact top 10 (ENN)
SELECT TOP 10 ProductId, VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS Distance
INTO #ENN_Results FROM Products ORDER BY Distance ASC;

-- 2. Get approximate top 10 (ANN)
SELECT TOP (10) WITH APPROXIMATE
    ProductId,
    VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector) AS distance
INTO #ANN_Results
FROM Products
ORDER BY VECTOR_DISTANCE('cosine', DescriptionEmbedding, @queryVector);

-- 3. Calculate recall
SELECT CAST(COUNT(*) AS FLOAT) / 10 AS Recall
FROM #ANN_Results a
WHERE EXISTS (SELECT 1 FROM #ENN_Results e WHERE e.ProductId = a.ProductId);
Scenario: Leo tests search quality at SearchWave

Leo runs a recall test on SearchWave’s product search. For 100 random queries, he compares ANN results (top 20) with ENN ground truth (top 20). Average recall is 97% β€” meaning ANN finds 97 out of 100 of the exact nearest neighbours. The 3 missed results are very close in distance (marginal differences). For an e-commerce search, this is excellent β€” users would not notice the difference.

He also measures p95 latency: ANN = 23ms, ENN = 4,200ms. ANN is 180x faster. The decision is clear: ANN for production.

Question

When should you use ENN instead of ANN?

Click or press Enter to reveal answer

Answer

Use ENN (exact nearest neighbour) for: small datasets (under 10K vectors), validation/testing of ANN accuracy, and scenarios where 100% recall is required (e.g., legal discovery). For everything else, ANN is the right choice β€” it is dramatically faster with negligible accuracy loss.

Click to flip back

Question

What is recall@K in vector search?

Click or press Enter to reveal answer

Answer

The proportion of true top-K nearest neighbours that the ANN algorithm actually returns. Recall@10 = 0.95 means ANN found 9.5 out of 10 exact nearest neighbours on average. Higher recall = more accurate but potentially slower.

Click to flip back

Question

What is the difference between ANN and ENN vector search in SQL Server?

Click or press Enter to reveal answer

Answer

ANN uses SELECT TOP (K) WITH APPROXIMATE ... ORDER BY VECTOR_DISTANCE() β€” it leverages the DiskANN index for fast approximate results (~95-99% recall). ENN omits WITH APPROXIMATE, performing a brute-force scan for guaranteed 100% accuracy but at much higher cost. Use ANN for production, ENN for small datasets or validation.

Click to flip back

Knowledge Check

Priya at Vault Bank has 10 million embeddings and needs sub-50ms search latency. She runs a recall test and gets 98% recall with ANN. The security team asks: 'Can you guarantee you always find the most similar record?' What should Priya explain?

Next up: Hybrid Search and Reciprocal Rank Fusion β€” combine keyword and vector search for the best of both worlds.