0% found this document useful (0 votes)
3 views3 pages

Vector Distance Metrics Explained

The document provides an overview of the VECTOR_DISTANCE function, which calculates the distance between two vectors using various metrics such as Euclidean, Cosine, and Manhattan. It details how to use these metrics in both exact and approximate similarity searches, along with shorthand operators for convenience. Additionally, it highlights common distance metrics and their applications, particularly in natural language processing.

Uploaded by

irchmaar8080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Vector Distance Metrics Explained

The document provides an overview of the VECTOR_DISTANCE function, which calculates the distance between two vectors using various metrics such as Euclidean, Cosine, and Manhattan. It details how to use these metrics in both exact and approximate similarity searches, along with shorthand operators for convenience. Additionally, it highlights common distance metrics and their applications, particularly in natural language processing.

Uploaded by

irchmaar8080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Vector Distance Overview

1. Main Function

VECTOR_DISTANCE(vector1, vector2 [, metric])

Calculates distance between two vectors.

Default metric: COSINE.

Optional metrics include Euclidean (L2), Euclidean squared, L1 (Manhattan),


cosine, inner product, and Hamming.

Shorthand functions/operators:

L1_DISTANCE → Manhattan distance

L2_DISTANCE → Euclidean distance

COSINE_DISTANCE → Cosine similarity distance

INNER_PRODUCT → Dot product similarity

Shorthand operators exist for L2, cosine, and negative dot product.

2. Usage in Similarity Search

Exact search: Uses the specified metric if it differs from the index metric.

Approximate search: Uses the index metric if it matches the query metric.

Default: Cosine similarity for unspecified metrics.

3. Common Distance Metrics

Metric Description Notes


Straight-line distance;
Euclidean (L2) Sensitive to magnitude & direction
Pythagorean theorem
Euclidean Avoids square root; faster for Comparing squared distances is
Squared ordering equivalent to ordering by distance
Cosine Measures similarity by angle;
Widely used in NLP
Similarity higher = more similar
Dot Product
Magnitude × cosine of angle Larger values = more similar
Similarity
Useful for uniform grids; faster
Manhattan (L1) “City block” distance
than Euclidean
Hamming Counts differing bits in Useful for network error detection
Similarity binary vectors or binary data

4. Example Usage

Euclidean Distance

SELECT VECTOR_DISTANCE(VECTOR(0,0), VECTOR(10,0), 'EUCLIDEAN') AS


DISTANCE

FROM dual;

Computes straight-line distance between vectors (0,0) and (10,0).

Using Shorthand Operators

-- Euclidean shorthand (L2)

SELECT VECTOR(1,2) <-> VECTOR(0,1) AS DISTANCE FROM dual;

-- Cosine shorthand

SELECT VECTOR(1,2) <=> VECTOR(0,1) AS COS_DISTANCE FROM dual;

-- Negative dot product shorthand

SELECT VECTOR(1,2) <#> VECTOR(0,1) AS DOT_DISTANCE FROM dual;

Operators <->, <=>, <#> provide concise syntax for distance calculations.
5. Key Notes

Vector constructor can be used to create vectors for these calculations without
storing them in tables.

Cosine similarity/distance is the default and widely used metric, especially for
NLP.

Squared Euclidean distance can improve performance when only relative


ordering matters.

You might also like