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.
Exact search
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.
The following diagram contrasts exact and approximate search.

KNN or ANN?
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;An approximate query that will not run
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.
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.