Lesson 2 of 5 Vector Search

Vector Functions for Semantic Search

This lesson explains VECTOR_DISTANCE, VECTOR_NORMALIZE, VECTOR_NORM, VECTORPROPERTY and VECTOR_SEARCH.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between the cosine, euclidean and dot metrics.
  • Explain when to normalize vectors.
  • Choose between VECTOR_DISTANCE and VECTOR_SEARCH.

VECTOR_DISTANCE and the metrics

VECTOR_DISTANCE(metric, v1, v2) returns the exact distance between two vectors of the same dimensions; smaller always means more similar. The three metrics behave as the following table describes.

MetricMeasuresRange
cosineAngle between vectors, ignoring length0 (same direction) to 2 (opposite)
euclideanStraight-line distance, including length0 and up
dotNegative dot productAny value; more negative is more similar

Used with TOP (k) ... ORDER BY, VECTOR_DISTANCE performs an exact k-nearest neighbour search over every candidate row.

DECLARE @q vector(1536) = AI_GENERATE_EMBEDDINGS(N'lightweight tent for two' USE MODEL EmbeddingModel);

SELECT TOP (5) p.ProductID, p.Name,
       VECTOR_DISTANCE('cosine', e.Embedding, @q) AS distance
FROM Catalog.ProductEmbedding AS e
JOIN Catalog.Product AS p ON p.ProductID = e.ProductID
WHERE p.CategoryID = 3
ORDER BY distance;

The following diagram contrasts the three metrics.

The three distance metrics of VECTOR_DISTANCE, where smaller always means more similar. Cosine distance measures the angle between vectors and ignores their length, from 0 for the same direction to 2 for opposite. Euclidean distance is the straight-line distance and includes length. The dot metric is the negative dot product, so more negative is more similar; on unnormalized vectors it favours long vectors, which normalization removes.
Match the metric to the embedding model's recommendation.
Match the pairs

Distance metrics

Exam skills 3.2.4 · 3.2.6

Match each metric with how VECTOR_DISTANCE reports it.

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

VECTOR_NORMALIZE, VECTOR_NORM and VECTORPROPERTY

VECTOR_NORMALIZE(v, 'norm2') returns a vector in the same direction scaled to length 1 (also 'norm1' or 'norminf'). For normalized vectors, cosine distance and dot product produce the same ranking, and dot product is cheaper, so normalizing vectors from models that do not already normalize them lets queries use the dot metric. Azure OpenAI embeddings are already normalized. VECTOR_NORM(v, 'norm2') returns a vector's length, which shows whether vectors are normalized. VECTORPROPERTY(v, 'Dimensions') and VECTORPROPERTY(v, 'BaseType') describe a vector, which helps validate that stored vectors match the expected model and precision.

DECLARE @v vector(3) = '[3, 4, 0]';
SELECT VECTOR_NORM(@v, 'norm2')            AS Length,      -- 5
       VECTOR_NORMALIZE(@v, 'norm2')       AS UnitVector,  -- [0.6, 0.8, 0]
       VECTORPROPERTY(@v, 'Dimensions')    AS Dims,        -- 3
       VECTORPROPERTY(@v, 'BaseType')      AS BaseType;    -- float32
Fill in the value

Norms and properties

Exam skills 3.2.4

Work out what each vector function returns.

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

VECTOR_SEARCH

VECTOR_SEARCH is a table-valued function that searches a table's vector column for the vectors most similar to a query vector. It returns all columns of the table plus a distance column. With SELECT TOP (N) WITH APPROXIMATE ... ORDER BY distance it uses a vector index whose metric matches (approximate search); without WITH APPROXIMATE, TOP (N) ... ORDER BY distance performs exact kNN. The columns of the searched table are referenced through the alias given in the TABLE argument.

SELECT TOP (10) WITH APPROXIMATE
       e.ProductID, r.distance
FROM VECTOR_SEARCH(
        TABLE      = Catalog.ProductEmbedding AS e,
        COLUMN     = Embedding,
        SIMILAR_TO = @q,
        METRIC     = 'cosine') AS r
ORDER BY r.distance;

The choice of function depends on the task, as the following comparison summarizes.

VECTOR_DISTANCE
PurposeExact distance between two vectors
Uses a vector indexNo
Typical useSmall sets, re-ranking, ground truth
Choose when Exact results or small candidate sets
VECTOR_SEARCH
PurposeSearch a table for the nearest vectors
Uses a vector indexYes, with TOP (N) WITH APPROXIMATE and a matching metric
Typical useLarge tables with latency targets
Choose when Index-backed approximate search
Sort into groups

VECTOR_DISTANCE or VECTOR_SEARCH?

Exam skills 3.2.4 · 3.2.7

Choose the vector function for each task.

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

Dot product on unnormalized vectors

A team stores embeddings from an open-source model that does not normalize its output and wants to use the dot metric for speed. Rankings with dot differ from cosine rankings, favouring long documents. What should the developer do?

Normalize the stored vectors and the query vector with VECTOR_NORMALIZE(..., 'norm2'); for unit-length vectors, dot product ranks identically to cosine. Without normalization, the dot product is influenced by vector length, which is why long documents rank higher. Switching to euclidean distance would still be affected by length.

Check yourself

For vectors normalized to length 1, how do cosine and dot product rankings compare?

Show answer

They are identical, so the cheaper dot product can be used.

Exam Tips

Remember for the exam

  • Exact distance: VECTOR_DISTANCE; smaller is more similar for every metric.
  • Normalized vectors: cosine and dot give the same ranking.
  • Validate dimensions and precision: VECTORPROPERTY.
  • Index-backed approximate search: VECTOR_SEARCH with TOP (N) WITH APPROXIMATE.