| title | VECTOR_DISTANCE (Transact-SQL) | ||
|---|---|---|---|
| description | VECTOR_DISTANCE calculates the distance between two vectors using a specified distance metric. | ||
| author | WilliamDAssafMSFT | ||
| ms.author | wiassaf | ||
| ms.reviewer | damauri, pookam | ||
| ms.date | 09/11/2024 | ||
| ms.service | sql | ||
| ms.subservice | t-sql | ||
| ms.topic | reference | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| dev_langs |
|
||
| monikerRange | = azuresqldb-current || = azuresqldb-mi-current |
[!INCLUDE Azure SQL Database Azure SQL Managed Instance]
Note
This data type is in preview and is subject to change. Make sure to read preview usage terms in the Service Level Agreements (SLA) for Online Services document.
Calculates the distance between two vectors using a specified distance metric.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
VECTOR_DISTANCE ( distance_metric, vector1, vector2 )
A string with the name of the distance metric to use to calculate the distance between the two given vectors. The following distance metrics are supported:
cosine- Cosine distanceeuclidean- Euclidean distancedot- (Negative) Dot product
An expression that evaluates to a vector in binary format.
An expression that evaluates to a vector in binary format.
| Metric | Description | Range | Examples |
|---|---|---|---|
cosine |
Cosine (angular) distance | [0, 2] | 0: identical vectors 2: opposing vectors |
euclidean |
Euclidean distance | [-∞, +∞] | 0: identical vectors |
dot |
Dot product-based indication of distance, obtained by calculating the negative dot product |
[-∞, +∞] | Smaller numbers indicate more similar vectors |
The function returns a scalar float value that represents the distance between the two vectors using the specified distance metric.
An error is returned if distance_metric isn't a valid metric and if the provided binary values don't represent valid vectors.
Review the Azure SQL Database Vector Search Samples GitHub repo to get end-to-end working samples.
Examples assume the existence of a table named wikipedia_articles with a column title_vector of type vector that stores title's embeddings of Wikipedia articles. title_vector is assumed to be an embedding generated with an embedding model like text-embedding-ada-002 or text-embedding-3-small , which returns vectors with 1,536 dimensions.
The following example creates a vector with three dimensions from a string with a JSON array.
DECLARE @v1 VECTOR(2) = '[1,1]';
DECLARE @v2 VECTOR(2) = '[-1,-1]';
SELECT
VECTOR_DISTANCE('euclidean', @v1, @v2) AS euclidean,
VECTOR_DISTANCE('cosine', @v1, @v2) AS cosine,
VECTOR_DISTANCE('dot', @v1, @v2) AS negative_dot_product;The following example returns the top 10 most similar articles to a given article, based on the cosine distance between their title vectors. Details of the database used in the sample can be found here: Download and import the Wikipedia Article with Vector Embeddings
DECLARE @v AS VECTOR(1536);
SELECT @v = title_vector FROM [dbo].[wikipedia_articles] WHERE title = 'Alan Turing';
SELECT TOP(10)
id,
title,
VECTOR_DISTANCE('cosine', @v, title_vector) AS distance
FROM
[dbo].[wikipedia_articles]
ORDER BY
distanceThe following example returns all the similar articles to a given article, based on the cosine distance between their title vectors, selecting only those with a distance less than 0.3.
DECLARE @v AS VECTOR(1536);
SELECT @v = title_vector FROM [dbo].[wikipedia_articles] WHERE title = 'Alan Turing';
SELECT
id,
title,
VECTOR_DISTANCE('cosine', @v, title_vector) AS distance
FROM
[dbo].[wikipedia_articles]
WHERE
VECTOR_DISTANCE('cosine', @v, title_vector) < 0.3
ORDER BY
distance