Distance Metrics
Agentic AI for Oracle AI Database Professional · Lesson 4 of 20 · 21:39
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Agentic AI for Oracle AI Database Professional exam (1Z0-1159-26). In this lesson: VECTOR_DISTANCE and its defaults, cosine and dot product, Euclidean and Manhattan, Hamming and Jaccard, and why the query and the index must use the same metric. Follows Ambergate Appliances, a fictional appliance maker. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Three metrics, three lists
Ambergate's search team ran the same question with three metrics, and got three slightly different top ten lists. Which one is right depends on the embedding model, not on taste. This lesson covers the vector distance function, the metrics of Oracle AI Vector Search, and how to choose one.
The distance function
VECTOR_DISTANCE takes two vectors and a metric, and returns a distance: the smaller it is, the more similar the vectors are. The metric is optional, and the default is cosine, or Hamming for binary vectors. When the query names no metric and the column has a vector index, the metric of that index is used. Shorthand functions exist too, like COSINE_DISTANCE, L2_DISTANCE, and INNER_PRODUCT, and so do operators for the Euclidean, cosine, and negative dot product distances.
Cosine
Cosine distance looks only at the angle between two vectors, not at their length. It equals one minus the cosine of that angle, so two vectors that point the same way have a distance of zero. That makes it the usual choice for text embeddings, where the direction carries the meaning. The length of each vector plays no part in the result.
Dot product
The dot product combines the angle with the lengths of both vectors. Oracle returns it as a negative number, so that, like every distance, a smaller value means a closer match. When all the vectors are normalized to a length of one, the dot product and cosine rank the results in exactly the same order.
Euclidean and Manhattan
Euclidean distance, also called L2, is the straight line between two points, so it depends on both direction and length. Euclidean squared skips the square root, so it keeps the same order and is faster to compute when you only rank results. Manhattan distance, also called L1, adds up the absolute differences, dimension by dimension, like walking city blocks. Choose these metrics when the embedding model was trained with them.
Hamming and Jaccard
Binary vectors have their own metrics. Hamming distance counts the dimensions whose bits differ, and it is the default for binary vectors. Jaccard distance compares the bits that are set to one, as one minus their overlap divided by their union, and it works only with binary vectors. Both run on bits, which is what makes binary vectors so fast.
Choose the metric
The rule is simple: use the metric that the embedding model was trained with, which its documentation usually states. Use that same metric when you create the vector index and when you query. If a query names a metric that conflicts with the index, the index is not used, and the database runs an exact search with the named metric. And never compare distances computed with different metrics, since each one has its own scale.
Recap
Let's recap. VECTOR_DISTANCE returns a distance, where smaller means more similar, with cosine by default, Hamming for binary vectors, or the metric of the column's index. Cosine measures the angle only, and the dot product, returned as a negative number, ranks normalized vectors exactly like cosine. Euclidean is the straight line, Euclidean squared ranks the same way faster, and Manhattan adds up the differences. Hamming and Jaccard work on bits, for binary vectors. And the model sets the metric, for the index and for the queries, since a conflicting metric means an exact search.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Ambergate’s text model outputs vectors that are all normalized to a length of one. Which two metrics are guaranteed to rank the results in the same order?
- Euclidean and Manhattan
- Hamming and Jaccard
- Manhattan and cosine
- Cosine and dot product
Show the answer
Cosine and dot product
For normalized vectors, the dot product and cosine give the same ranking, since all the lengths are equal. “Euclidean and Manhattan” pairs Euclidean with Manhattan, which measure the gap in different ways and can rank results differently. “Hamming and Jaccard” applies to binary vectors, not to these vectors of floats. And “Manhattan and cosine” mixes Manhattan with cosine, which can also disagree.
Ambergate stores image fingerprints as BINARY vectors, and a developer calls VECTOR_DISTANCE on them without naming a metric. Which metric is used?
- Cosine, the general default
- Hamming
- Jaccard
- Euclidean squared
Show the answer
Hamming
When no metric is given, binary vectors use Hamming. “Cosine, the general default” is the default for every other format, not for binary vectors. “Jaccard” also works on binary vectors, but only when you name it. And “Euclidean squared” must be named as well, and it is meant for vectors of numbers rather than bits.
The vector index on Ambergate’s chunks was created with the DOT metric. A new query names COSINE in its call to VECTOR_DISTANCE, and it runs far slower than the others. Why?
- Conflicting metrics force an exact search
- Cosine is much slower to compute than dot
- The index is rebuilt for each new query
- Cosine works only on BINARY vectors
Show the answer
Conflicting metrics force an exact search
When the query names a metric that conflicts with the index, the index is not used, and the database runs an exact search with the named metric. “Cosine is much slower to compute than dot” is not the cause, since the gap comes from comparing every vector, not from one formula. “The index is rebuilt for each new query” is wrong, because an index serves many queries without any rebuild. And “Cosine works only on BINARY vectors” confuses cosine with Hamming, the default for binary vectors.