21 Artificial Intelligence in the Oracle Database
This chapter describes the artificial intelligence features provided in the Oracle Database.
- Overview of Oracle AI Vector Search
Oracle AI Vector Search is designed for Artificial Intelligence (AI) workloads and allows you to query data based on semantics, rather than keywords. - Understand Hierarchical Navigable Small World Indexes
Use these examples to understand how to create HNSW indexes for vector approximate similarity searches. - Understand Inverted File Flat Vector Indexes
The Inverted File Flat vector index is a technique designed to enhance search efficiency by narrowing the search area through the use of neighbor partitions or clusters. - Vector Distance Metrics
Measuring distances in a vector space is at the heart of identifying the most relevant results for a given query vector. That process is very different from the well-known keyword filtering in the relational database world. - Artificial Intelligence in the Oracle Database
This section describes the methods for performing similarity searches.
Overview of Oracle AI Vector Search
Oracle AI Vector Search is designed for Artificial Intelligence (AI) workloads and allows you to query data based on semantics, rather than keywords.
VECTOR Data Type
The VECTOR
data type is introduced with the release of
Oracle Database 23ai, providing the foundation to store vector embeddings alongside
business data in the database. Using embedding models, you can transform
unstructured data into vector embeddings that can then be used for semantic queries
on business data. In order to use the VECTOR
data type and its
related features, the COMPATIBLE
initialization parameter must be
set to 23.4.0 or higher. For more information about the parameter and how to change
it, see Oracle Database Upgrade
Guide.
See the following basic example of using the VECTOR
data type in a
table definition:
CREATE TABLE docs (doc_id INT, doc_text CLOB, doc_vector VECTOR);
For more information about the VECTOR
data type and how
to use vectors in tables, see Create Tables Using the VECTOR Data
Type.
VECTOR
data type, you
can use it as an input to the machine learning algorithms such as classification,
anomaly, regression, clustering and feature extraction. More details on using
VECTOR
data type in machine learning could be found in Vector Data Type Support.
Note:
Support forVECTOR
data type machine learning is available in all
versions starting 23.7, but not in earlier versions.
Vector Embeddings
If you've ever used applications such as voice assistants, chatbots, language translators, recommendation systems, anomaly detection, or video search and recognition, you've implicitly used vector embeddings features.
Oracle AI Vector Search stores vector embeddings, which are mathematical vector representations of data points. These vector embeddings describe the semantic meaning behind content such as words, documents, audio tracks, or images. As an example, while doing text based searches, vector search is often considered better than keyword search as vector search is based on the meaning and context behind the words and not the actual words themselves. This vector representation translates semantic similarity of objects, as perceived by humans, into proximity in a mathematical vector space. This vector space usually has multihundreds, if not thousands, of dimensions. Put differently, vector embeddings are a way of representing almost any kind of data, such as text, images, videos, users, or music as points in a multidimensional space where the locations of those points in space, and proximity to others, are semantically meaningful.
This simplified diagram illustrates a vector space where words are encoded as 2-dimensional vectors.
Similarity Search
Searching semantic similarity in a data set is now equivalent to searching nearest neighbors in a vector space instead of using traditional keyword searches using query predicates. As illustrated in the following diagram, the distance between dog and wolf in this vector space is shorter than the distance between dog and kitten. In this space, a dog is more similar to a wolf than it is to a kitten. See Perform Exact Similarity Search for more information.
Vector data tends to be unevenly distributed and clustered into groups that are semantically related. Doing a similarity search based on a given query vector is equivalent to retrieving the K-nearest vectors to your query vector in your vector space. Basically, you need to find an ordered list of vectors by ranking them, where the first row in the list is the closest or most similar vector to the query vector, the second row in the list is the second closest vector to the query vector, and so on. When doing a similarity search, the relative order of distances is what really matters rather than the actual distance.
Using the preceding vector space, here is an illustration of a semantic search where your query vector is the one corresponding to the word Puppy and you want to identify the four closest words:
Similarity searches tend to get data from one or more clusters depending on the value of the query vector and the fetch size.
Approximate searches using vector indexes can limit the searches to specific clusters, whereas exact searches visit vectors across all clusters. See Use Vector Indexes for more information.
Vector Embedding Models
One way of creating such vector embeddings could be to use someone's domain expertise to quantify a predefined set of features or dimensions such as shape, texture, color, sentiment, and many others, depending on the object type with which you're dealing. However, the efficiency of this method depends on the use case and is not always cost effective.
Instead, vector embeddings are created via neural networks. Most modern vector embeddings use a transformer model, as illustrated by the following diagram, but convolutional neural networks can also be used.
Depending on the type of your data, you can use different pretrained, open-source models to create vector embeddings. For example:
- For textual data, sentence transformers transform words, sentences, or paragraphs into vector embeddings.
- For visual data, you can use Residual Network (ResNet) to generate vector embeddings.
- For audio data, you can use the visual spectrogram representation of the audio data to fall back into the visual data case.
Each model also determines the number of dimensions for your vectors. For example:
- Cohere's embedding model embed-english-v3.0 has 1024 dimensions.
- OpenAI's embedding model text-embedding-3-large has 3072 dimensions.
- Hugging Face's embedding model all-MiniLM-L6-v2 has 384 dimensions
Of course, you can always create your own model that is trained with your own data set.
Import Embedding Models into Oracle Database
Although you can generate vector embeddings outside the Oracle Database using pretrained open-source embeddings models or your own embeddings models, you also have the option to import those models directly into the Oracle Database if they are compatible with the Open Neural Network Exchange (ONNX) standard. Oracle Database implements an ONNX runtime directly within the database. This allows you to generate vector embeddings directly within the Oracle Database using SQL. See Generate Vector Embeddings for more information.
Parent topic: Artificial Intelligence in the Oracle Database
Understand Hierarchical Navigable Small World Indexes
Use these examples to understand how to create HNSW indexes for vector approximate similarity searches.
With Navigable Small World (NSW), the idea is to build a proximity graph where each vector in the graph connects to several others based on three characteristics:
- The distance between vectors
- The maximum number of closest vector candidates considered at each step
of the search during insertion
(EFCONSTRUCTION)
- Within the maximum number of connections (
NEIGHBORS
) permitted per vector
If the combination of the above two thresholds is too high, then you may end up with a densely connected graph, which can slow down the search process. On the other hand, if the combination of those thresholds is too low, then the graph may become too sparse and/or disconnected, which makes it challenging to find a path between certain vectors during the search.
Navigable Small World (NSW) graph traversal for vector search begins with a predefined entry point in the graph, accessing a cluster of closely related vectors. The search algorithm employs two key lists: Candidates, a dynamically updated list of vectors that we encounter while traversing the graph, and Results, which contains the vectors closest to the query vector found thus far. As the search progresses, the algorithm navigates through the graph, continually refining the Candidates by exploring and evaluating vectors that might be closer than those in the Results. The process concludes once there are no vectors in the Candidates closer than the farthest in the Results, indicating a local minimum has been reached and the closest vectors to the query vector have been identified.
This is illustrated in the following graphic:
The described method demonstrates robust performance up to a certain scale of vector insertion into the graph. Beyond this threshold, the Hierarchical Navigable Small World (HNSW) approach enhances the NSW model by introducing a multi-layered hierarchy, akin to the structure observed in Probabilistic Skip Lists. This hierarchical architecture is implemented by distributing the graph's connections across several layers, organizing them in a manner where each subsequent layer contains a subset of the links from the layer below. This stratification ensures that the top layers capture long-distance links, effectively serving as express pathways across the graph, while the lower layers focus on shorter links, facilitating fine-grained, local navigation. As a result, searches begin at the higher layers to quickly approximate the region of the target vector, progressively moving to lower layers for a more precise search, significantly improving search efficiency and accuracy by leveraging shorter links (smaller distances) between vectors as one moves from the top layer to the bottom.
To better understand how this works for HNSW, let's look at how this hierarchy is used for the Probability Skip List structure:
Figure 21-3 Probability Skip List Structure
The Probability Skip List structure uses multiple layers of linked lists where the above layers are skipping more numbers than the lower ones. In this example, you are trying to search for number 17. You start with the top layer and jump to the next element until you either find 17, reach the end of the list, or you find a number that is greater than 17. When you reach the end of a list or you find a number greater than 17, then you start in the previous layer from the latest number less than 17.
HNSW uses the same principle with NSW layers where you find greater distances between vectors in the higher layers. This is illustrated by the following diagrams in a 2D space:
At the top layer are the longest edges and at the bottom layer are the shortest ones.
Figure 21-4 Hierarchical Navigable Small World Graphs
Starting from the top layer, the search in one layer starts at the entry vector. Then for each node, if there is a neighbor that is closer to the query vector than the current node, it jumps to that neighbor. The algorithm keeps doing this until it finds a local minimum for the query vector. When a local minimum is found in one layer, the search goes to the next layer by using the same vector in that new layer and the search continues in that layer. This process repeats itself until the local minimum of the bottom layer is found, which contains all the vectors. At this point, the search is transformed into an approximate similarity search using the NSW algorithm around that latest local minimum found to extract the top k
most similar vectors to your query vector. While the upper layers can have a maximum of connections for each vector set by the NEIGHBORS
parameter, layer 0
can have twice as much. This process is illustrated in the following graphic:
Figure 21-5 Hierarchical Navigable Small World Graphs Search
Layers are implemented using in-memory graphs (not Oracle Inmemory graph). Each layer uses a separate in-memory graph. As already seen, when creating an HNSW index, you can fine tune the maximum number of connections per vector in the upper layers using the NEIGHBORS
parameter as well as the maximum number of closest vector candidates considered at each step of the search during insertion using the EFCONSTRUCTION
parameter, where EF stands for Enter Factor.
As explained earlier, when using Oracle AI Vector Search to run an approximate search query using HNSW indexes, you have the possibility to specify a target accuracy at which the approximate search should be performed.
In the case of an HNSW approximate search, you can specify a target accuracy percentage value to influence the number of candidates considered to probe the search. This is automatically calculated by the algorithm. A value of 100 will tend to impose a similar result as an exact search, although the system may still use the index and will not perform an exact search. The optimizer may choose to still use an index as it may be faster to do so given the predicates in the query. Instead of specifying a target accuracy percentage value, you can specify the EFSEARCH
parameter to impose a certain maximum number of candidates to be considered while probing the index. The higher that number, the higher the accuracy.
Note:
- If you do not specify any target accuracy in your approximate search query, then you will inherit the one set when the index was created. You will see that at index creation, you can specify a target accuracy either using a percentage value or parameters values depending on the index type you are creating.
- It is possible to specify a different target accuracy at index search compared to the one set at index creation. For HNSW indexes, you may look at more neighbors using the
EFSEARCH
parameter (higher than theEFCONSTRUCTION
value specified at index creation) to get more accurate results. The target accuracy that you give during index creation decides the index creation parameters and also acts as the default accuracy values for vector index searches.
Parent topic: Artificial Intelligence in the Oracle Database
Understand Inverted File Flat Vector Indexes
The Inverted File Flat vector index is a technique designed to enhance search efficiency by narrowing the search area through the use of neighbor partitions or clusters.
The following diagrams depict how partitions or clusters are created in an approximate search done using a 2D space representation. But this can be generalized to much higher dimensional spaces.
Figure 21-6 Inverted File Flat Index Using 2D
Crosses represent the vector data points in this space.
New data points, shown as small plain circles, are added to identify k
partition centroids, where the number of centroids (k
) is determined by the size of the dataset (n
). Typically k
is set to the square root of n
, though it can be adjusted by specifying the NEIGHBOR PARTITIONS
parameter during index creation.
Each centroid represents the average vector (center of gravity) of the corresponding partition.
The centroids are calculated by a training pass over the vectors whose goal is to minimize the total distance of each vector from the closest centroid.
The centroids ends up partitioning the vector space into k
partitions. This division is conceptually illustrated as expanding circles from the centroids that stop growing as they meet, forming distinct partitions.
Except for those on the periphery, each vector falls within a specific partition associated with a centroid.
For a query vector vq
, the search algorithm identifies the nearest i
centroids, where i
defaults to the square root of k
but can be adjusted for a specific query by setting the NEIGHBOR PARTITION PROBES
parameter. This adjustment allows for a trade-off between search speed and accuracy.
Higher numbers for this parameter will result in higher accuracy. In this example, i
is set to 2 and the two identified partitions are partitions number 1 and 3.
Once the i
partitions are determined, they are fully scanned to identify, in this example, the top 5 nearest vectors. This number 5 can be different from k
and you specify this number in your query. The five nearest vectors to vq
found in partitions number 1 and 3 are highlighted in the following diagram.
This method constitutes an approximate search as it limits the search to a subset of partitions, thereby accelerating the process but potentially missing closer vectors in unexamined partitions. This example illustrates that an approximate search might not yield the exact nearest vectors to vq
, demonstrating the inherent trade-off between search efficiency and accuracy.
However, the five exact nearest vectors from vq
are not the ones found by the approximate search. You can see that one of the vectors in partition number 4 is closer to vq
than one of the retrieved vectors in partition number 3.
You can now see why using vector index searches is not always an exact search and is called an approximate search instead. In this example, the approximate search accuracy is only 80% as it has retrieved only 4 out of 5 of the exact search vectors' result.
When using Oracle AI Vector Search to run an approximate search query using vector indexes, you have the possibility to specify a target accuracy at which the approximate search should be performed.
In the case of an IVF approximate search, you can specify a target accuracy percentage value to influence the number of partitions used to probe the search. This is automatically calculated by the algorithm. A value of 100 will tend to impose an exact search, although the system may still use the index and will not perform an exact search. The optimizer may choose to still use an index as it may be faster to do so given the predicates in the query. Instead of specifying a target accuracy percentage value, you can specify the NEIGHBOR PARTITION PROBES
parameter to impose a certain maximum number of partitions to be probed by the search. The higher that number, the higher the accuracy.
Note:
- If you do not specify any target accuracy in your approximate search query, then you will inherit the one set when the index was created. You will see that at index creation time, you can specify a target accuracy either using a percentage value or parameters values depending on the type of index you are creating.
- It is possible to specify a different target accuracy at index search, compared to the one set at index creation. For IVF indexes, you may probe more centroid partitions using the
NEIGHBOR PARTITION PROBES
parameter to get more accurate results. The target accuracy that you provide during index creation decides the index creation parameters and also acts as the default accuracy value for vector index searches.
Parent topic: Artificial Intelligence in the Oracle Database
Vector Distance Metrics
Measuring distances in a vector space is at the heart of identifying the most relevant results for a given query vector. That process is very different from the well-known keyword filtering in the relational database world.
When working with vectors, there are several ways you can calculate distances to determine how similar, or dissimilar, two vectors are. Each distance metric is computed using different mathematical formulas. The time it takes to calculate the distance between two vectors depends on many factors, including the distance metric used as well as the format of the vectors themselves, such as the number of vector dimensions and the vector dimension formats. Generally it's best to match the distance metric you use to the one that was used to train the vector embedding model that generated the vectors.
Parent topic: Artificial Intelligence in the Oracle Database
Artificial Intelligence in the Oracle Database
This section describes the methods for performing similarity searches.
- Perform Exact Similarity Search
A similarity search looks for the relative order of vectors compared to a query vector. Naturally, the comparison is done using a particular distance metric but what is important is the result set of your top closest vectors, not the distance between them. - Understand Approximate Similarity Search Using Vector Indexes
For faster search speeds with large vector spaces, you can use approximate similarity search using vector indexes. - Perform Multi-Vector Similarity Search
Another major use-case of vector search is multi-vector search. Multi-vector search is typically associated with a multi-document search, where documents are split into chunks that are individually embedded into vectors.
Parent topic: Artificial Intelligence in the Oracle Database
Perform Exact Similarity Search
A similarity search looks for the relative order of vectors compared to a query vector. Naturally, the comparison is done using a particular distance metric but what is important is the result set of your top closest vectors, not the distance between them.
As an example, and given a certain query vector, you can calculate its distance to all other vectors in your data set. This type of search, also called flat search, or exact search, produces the most accurate results with perfect search quality. However, this comes at the cost of significant search times. This is illustrated by the following diagrams:
With an exact search, you compare the query vector vq
against every other vector in your space by calculating its distance to each vector. After calculating all of these distances, the search returns the nearest k
of those as the nearest matches. This is called a k-nearest neighbors (kNN) search.
For example, the Euclidean similarity search involves retrieving the top-k nearest
vectors in your space relative to the Euclidean distance metric and a query vector.
Here's an example that retrieves the top 10 vectors from the vector_tab
table that are the nearest to query_vector
using the following exact
similarity search query:
SELECT docID
FROM vector_tab
ORDER BY VECTOR_DISTANCE( embedding, :query_vector, EUCLIDEAN )
FETCH EXACT FIRST 10 ROWS ONLY;
In this example, docID
and embedding
are columns defined in the vector_tab
table and embedding
has the VECTOR
data type.
In the case of Euclidean distances, comparing squared distances is equivalent to comparing distances. So, when ordering is more important than the distance values themselves, the Euclidean Squared distance is very useful as it is faster to calculate than the Euclidean distance (avoiding the square-root calculation). Consequently, it is simpler and faster to rewrite the query like this:
SELECT docID
FROM vector_tab
ORDER BY VECTOR_DISTANCE( embedding, :query_vector, EUCLIDEAN_SQUARED)
FETCH FIRST 10 ROWS ONLY;
Note:
TheEXACT
keyword is optional. If omitted while connected to an ADB-S
instance, an approximate search using a vector index is attempted if one exists. For
more information, see Perform Approximate Similarity Search Using Vector
Indexes.
Note:
Ensure that you use the distance function that was used to train your embedding model.See Also:
Oracle Database SQL
Language Reference for the full syntax of the
ROW_LIMITING_CLAUSE
Parent topic: Artificial Intelligence in the Oracle Database
Understand Approximate Similarity Search Using Vector Indexes
For faster search speeds with large vector spaces, you can use approximate similarity search using vector indexes.
Using vector indexes for a similarity search is called an approximate search. Approximate searches use vector indexes, which trade off accuracy for performance.
Approximate search for large vector spaces
When search quality is your high priority and search speed is less important, Exact Similarity search is a good option. Search speed can be irrelevant for smaller vector spaces, or when you perform searches with high performance servers. However, ML algorithms often perform similarity searches on vector spaces with billions of embeddings. For example, the Deep1B data-set contains 1B images generated by a Convolutional Neural Network (CNN). Computing vector distances with every vector in the corpus to find Top-K matches at 100 percent accuracy is very slow.
Fortunately, there are many types of approximate searches that you can perform using vector indexes. Vector indexes can be less accurate, but they can consume less resources, and can be more efficient. Unlike traditional database indexes, vector indexes are constructed and perform searches using heuristic-based algorithms.
Because 100 percent accuracy cannot be guaranteed by heuristics, vector index searches use target accuracy. Internally, the algorithms used for both index creation and index search are doing their best to be as accurate as possible. However, you have the possibility to influence those algorithms by specifying a target accuracy. When creating the index or searching it, you can specify non-default target accuracy values either by specifying a percentage value, or by specifying internal parameters values, depending on the index type you are using.
Target accuracy example
To better understand what is meant by target accuracy look at the following diagrams. The first diagram illustrate a vector space where each vector is represented by a small cross. The one in red represents your query vector.
Running a top-5 exact similarity search in that context would return the five vectors shown on the second diagram:
Depending on how your vector index was constructed, running a top-5 approximate similarity search in that context could return the five vectors shown on the third diagram. This is because the index creation process is using heuristics. So searching through the vector index may lead to different results compared to an exact search:
As you can see, the retrieved vectors are different and, in this case, they differ by one vector. This means that, compared to the exact search, the similarity search retrieved 4 out of 5 vectors correctly. The similarity search has 80% accuracy compared to the exact similarity search. This is illustrated on the fourth diagram:
Due to the nature of vector indexes being approximate search structures, it's possible that fewer than K rows are returned in a top-K similarity search.
For information on how to set up your vector indexes, see Create Vector Indexes.
Parent topic: Artificial Intelligence in the Oracle Database
Perform Multi-Vector Similarity Search
Another major use-case of vector search is multi-vector search. Multi-vector search is typically associated with a multi-document search, where documents are split into chunks that are individually embedded into vectors.
A multi-vector search consists of retrieving top-K vector matches using grouping criteria known as partitions based on the documents' characteristics. This ability to score documents based on the similarity of their chunks to a query vector being searched is facilitated in SQL using the partitioned row limiting clause.
With multi-vector search, it is easier to write SQL statements to answer the following type of question:
- If they exist, what are the four best matching sentences found in the three best matching paragraphs of the two best matching books?
For example, imagine if each book in your database is organized into paragraphs containing sentences which have vector embedding representations, then you can answer the previous question using a single SQL statement such as:
SELECT bookId, paragraphId, sentence
FROM books
ORDER BY vector_distance(sentence_embedding, :sentence_query_vector)
FETCH FIRST 2 PARTITIONS BY bookId, 3 PARTITIONS BY paragraphId, 4 ROWS ONLY;
You can also use an approximate similarity search instead of an exact similarity search as shown in the following example:
SELECT bookId, paragraphId, sentence
FROM books
ORDER BY vector_distance(sentence_embedding, :sentence_query_vector)
FETCH APPROXIMATE FIRST 2 PARTITIONS BY bookId, 3 PARTITIONS BY paragraphId, 4 ROWS ONLY
WITH TARGET ACCURACY 90;
Note:
All the rows returned are ordered byVECTOR_DISTANCE()
and not grouped by the partition clause.
Semantically, the previous SQL statement is interpreted as:
- Sort all records in the books table in descending order of the vector distance between the sentences and the query vector.
- For each record in this order, check its
bookId
andparagraphId
. This record is produced if the following three conditions are met:- Its
bookId
is one of the first two distinctbookId
in the sorted order. - Its
paragraphId
is one of the first three distinctparagraphId
in the sorted order within the samebookId
. - Its record is one of the first four records within the same
bookId
andparagraphId
combination.
- Its
- Otherwise, this record is filtered out.
Multi-vector similarity search is not just for documents and can be used to answer the following questions too:
- Return the top K closest matching photos but ensure that they are photos of different people.
- Find the top K songs with two or more audio segments that best match this sound snippet.
Note:
- This partition row-limiting clause extension is a generic extension of the SQL language. It does not have to apply just to vector searches.
- Multi-vector search with the partitioning row-limit clause does not use vector indexes.
See Also:
Oracle Database SQL
Language Reference for the full
syntax of the ROW_LIMITING_CLAUSE
Parent topic: Artificial Intelligence in the Oracle Database