Categories:

Vector similarity functions

VECTOR_L2_DISTANCE

Computes the L2 distance between two vectors.

L2 distance, also known as the Euclidean distance, is a measure of the distance between two vectors in a vector space. The distance is calculated by taking the square root of the sum of the squared differences of vector elements. The distance can be a value of zero or higher. If the distance is zero, the vectors are identical. The larger the distance, the farther apart the vectors are.

See also:

VECTOR_INNER_PRODUCT , VECTOR_COSINE_SIMILARITY , VECTOR_L1_DISTANCE , Vector Embeddings

Syntax

VECTOR_L2_DISTANCE( <vector>, <vector> )
Copy

Arguments

vector

The VECTOR value to calculate the distance from.

vector

The VECTOR value to calculate the distance to.

Returns

Returns the distance between the two input vectors as a FLOAT value.

Usage notes

  • Due to computational optimizations in the vector comparison functions, floating-point errors may be slightly larger than usual (e.g. about 1e-4).

Examples

This example uses the VECTOR_L2_DISTANCE function to determine which vectors in the table are closest to each other between columns a and b:

CREATE TABLE vectors (a VECTOR(FLOAT, 3), b VECTOR(FLOAT, 3));
INSERT INTO vectors SELECT [1.1,2.2,3]::VECTOR(FLOAT,3), [1,1,1]::VECTOR(FLOAT,3);
INSERT INTO vectors SELECT [1,2.2,3]::VECTOR(FLOAT,3), [4,6,8]::VECTOR(FLOAT,3);

-- Compute the pairwise inner product between columns a and b
SELECT VECTOR_L2_DISTANCE(a, b) FROM vectors;
Copy
+------+
| 2.3  |
|------|
| 6.95 |
+------+