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.