Lesson 3 of 5 Vector Search

Exact (KNN) and Approximate (ANN) Vector Search

This lesson explains exact and approximate vector search, when to choose each, and how filters and query shapes affect them.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between KNN and ANN search.
  • Explain iterative filtering with current vector indexes.
  • Identify the query rules for TOP WITH APPROXIMATE.

KNN: exact search

Exact k-nearest neighbour (KNN) search computes the distance from the query vector to every candidate and returns the k closest. Its results are always correct, but its cost grows linearly with the number of candidate rows. It is the right choice when the candidate set is small, such as a few tens of thousands of rows or fewer, or when ordinary predicates (a tenant, a category, a date range supported by a B-tree index) narrow the rows first. KNN needs no vector index and also serves as the ground truth when measuring approximate search.

True or false

Exact search

Exam skills 3.2.5

Decide whether each statement about exact KNN search is true.

Log in to try this activity Interactive activities are free with an account.

ANN: approximate search

Approximate nearest neighbour (ANN) search uses a DiskANN vector index, a graph of vectors that the search traverses from entry points towards the query, visiting only a small fraction of the data. Latency stays in milliseconds for millions of vectors, but recall is below 100%: occasionally a true neighbour is missed. The index needs at least 100 non-NULL vectors to be created and costs build time and storage.

CREATE VECTOR INDEX VIX_ProductEmbedding
    ON Catalog.ProductEmbedding (Embedding)
    WITH (METRIC = 'cosine', TYPE = 'DiskANN');

The two approaches are compared in the following table.

KNN (exact)
MechanismDistance to every candidate
Accuracy100% recall
Scales toSmall or pre-filtered sets
RequirementsNone
Choose when Small tables, selective filters, ground truth
ANN (approximate)
MechanismDiskANN index traversal
AccuracyHigh but below 100% recall
Scales toMillions of vectors
RequirementsVector index with a matching metric, 100+ vectors
Choose when Large tables with latency targets

The following diagram contrasts exact and approximate search.

Exact compared with approximate vector search. Exact k-nearest-neighbour search computes the distance to every candidate row, so recall is 100% but cost grows with the table, which suits small or well-filtered sets. Approximate search traverses a DiskANN index, a graph of vectors, from entry points towards the query, visiting a small fraction of the data; latency stays in milliseconds for millions of vectors, but recall is slightly below 100%. The index needs at least 100 vectors and a matching metric.
Approximate search trades a little recall for speed at scale.
Sort into groups

KNN or ANN?

Exam skills 3.2.5

Choose exact or approximate search for each workload.

Log in to try this activity Interactive activities are free with an account.

Filters and query shapes

With the current vector index version, WHERE predicates are applied during the search (iterative filtering), so a query asking for the top 10 products in one category returns 10 rows when they exist. Indexes created with earlier versions applied filters only after retrieving TOP_N rows, so they could return too few results and required oversized TOP_N values; the TOP_N parameter is deprecated. The optimizer may choose kNN instead of the index when that is cheaper, and the FORCE_ANN_ONLY table hint forces the index. Queries with WITH APPROXIMATE must order only by distance, ascending; aggregates, window functions, DISTINCT, UNION and multi-column sorting go in an outer query around a subquery, and CROSS APPLY needs the TOP WITH APPROXIMATE inside the applied subquery.

SELECT TOP (10) WITH APPROXIMATE
       p.ProductID, p.Name, r.distance
FROM Catalog.Product AS p
JOIN VECTOR_SEARCH(TABLE = Catalog.ProductEmbedding AS e, COLUMN = Embedding,
                   SIMILAR_TO = @q, METRIC = 'cosine') AS r
  ON r.ProductID = p.ProductID
WHERE p.CategoryID = 3 AND p.Discontinued = 0      -- applied during the search
ORDER BY r.distance;
Spot the bug

An approximate query that will not run

Exam skills 3.2.7

Find the line that makes this approximate search invalid.

Log in to try this activity Interactive activities are free with an account.
Worked example

Tenant-scoped search

A SaaS application stores 20 million document chunks for 4,000 tenants; the largest tenant has 50,000 chunks. Every search is limited to one tenant, and the TenantID column has a B-tree index. Results must be exact. Should the developer use KNN or ANN?

Use exact KNN with VECTOR_DISTANCE after filtering on TenantID: the index on TenantID narrows each search to at most 50,000 rows, which exact search handles quickly, and results are exact as required. ANN over the whole table would return approximate results, which the requirement rules out, although with iterative filtering it would honour the tenant filter.

Check yourself

Which ORDER BY does a TOP (N) WITH APPROXIMATE query require?

Show answer

ORDER BY the distance column only, ascending.

Exam Tips

Remember for the exam

  • Small or pre-filtered candidate sets, or exact results required: KNN.
  • Millions of vectors with latency targets: ANN with a DiskANN vector index.
  • Current vector indexes filter during the search; TOP_N is deprecated.
  • WITH APPROXIMATE requires ORDER BY distance ascending only; wrap other operations in an outer query.