This topic describes how to use semantic indexes in OceanBase Database.
Note
This feature is experimental and is not recommended for production environments.
Overview
Semantic indexes use the built-in embedding capabilities of OceanBase Database to simplify vector index operations. They abstract away vector concepts: you can write the original data to be stored, such as text or image URLs, and OceanBase Database automatically converts the data into vectors and builds indexes. During a search, you provide the original query content, and OceanBase Database automatically embeds the query and searches the vector index.
Building upon text semantic indexing, OceanBase also supports storing image URLs in VARCHAR columns and creating image semantic indexes. The system automatically calls multimodal embedding models to complete vectorization, supporting scenarios such as image search by image and image search by text (cross-modal retrieval).
Considering the performance overhead of embedding models, semantic indexing provides both synchronous and asynchronous embedding methods for users to choose from:
- Synchronous mode: Embedding and indexing are performed immediately after data is written, ensuring data is visible in real time.
- Asynchronous mode: Background tasks perform data embedding and indexing in batches, which can significantly improve write performance, but there will be a delay before data becomes visible. You can flexibly set the trigger cycle of background tasks based on your requirements for data visibility timeliness.
Tables with semantic indexes also support brute-force search. A brute-force search performs a full-table scan to return the exact top n nearest rows.
Feature support
Note
This feature currently only supports HNSW/HNSW_BQ/HNSW_SQ indexes.
In the current version, updates, deletions, and searches on semantic indexes use the same syntax and memory management as HNSW series indexes. Semantic indexes support index monitoring and maintenance. In asynchronous mode, an incremental refresh also triggers data embedding.
Supported features include:
Module |
Feature |
Introduction |
|---|---|---|
| DDL | Create a semantic index during table creation | You can create a semantic index for text or image URLs on a VARCHAR column when you create a table. |
| DDL | Create a semantic index after table creation | You can create a semantic index on an existing VARCHAR column. |
| DDL | Batch File embedding | When creating a semantic index after table creation, you can specify ai_service_tier=BATCH to submit requests in batches through files. The system processes all requests asynchronously. |
| DDL | Single-column multiple indexes | You can create multiple semantic indexes on the same column, which can use different models, distance algorithms, or index parameter combinations.
NoticeThis feature is for testing only and cannot be used in production. |
| DDL | Create an image URL semantic index | Set content_type=image to create a semantic index on a VARCHAR column and automatically vectorize image URLs.
NoticeThis feature is supported starting with V4.6.0 BP1. |
| Search | semantic_distance function |
Pass raw text or an image URL to perform vector search, including text-to-image and image-to-image searches.
NoticeThis feature is supported starting from V4.6.0 BP1. |
| Search | semantic_vector_distance function |
Pass a vector to perform a search in either of the following ways:
|
| DBMS_VECTOR | REBUILD_INDEX |
Uses the same method as a regular vector index to perform a full index rebuild. |
Some considerations are as follows:
- In synchronous mode, write performance may be affected by embedding performance; in asynchronous mode, data visibility will be delayed.
- For scenarios involving repeated searches, it is recommended to use the AI Function Service to pre-obtain query vectors, avoiding the need for embedding with each search.
- You cannot create multiple semantic indexes on the same column.
Prerequisites
Notice
Semantic indexes do not support registering models by using the new REGISTER_PROVIDER API. You must use the legacy CREATE_AI_MODEL and CREATE_AI_MODEL_ENDPOINT APIs to register embedding models.
Register a text embedding model
Before using semantic indexing, you must register an embedding model and endpoint. Here is an example of registration:
CALL DBMS_AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://api.siliconflow.cn/v1/embeddings",
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "siliconflow"
}');
Note
Replace access_key with your actual API Key. The BAAI/bge-m3 model has a vector dimension of 1024, so dim=1024 must be used when creating a semantic index.
Register a multimodal embedding model (image)
Image semantic indexes (content_type=image) must use multimodal (VL) embedding models. Register the model and endpoint by using the legacy APIs, and specify the registered model_key in the model parameter of the index WITH clause.
CALL DBMS_AI_SERVICE.DROP_AI_MODEL ('ob_vl_embed');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_vl_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_vl_embed', '{
"type": "dense_embedding",
"model_name": "qwen2.5-vl-embedding"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_vl_embed_endpoint', '{
"ai_model_name": "ob_vl_embed",
"url": "https://dashscope.aliyuncs.com/api/v1/services/embeddings/multimodal-embedding/multimodal-embedding",
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-dashscope"
}');
Note
Replace access_key with your actual API key. When you create an index, dim must match the actual output dimension of the selected model. When you register the endpoint, provider must be set to aliyun-dashscope. For more registration examples, see the legacy API instructions in Register an AI model.
Considerations for image semantic indexes
- Model requirements:
- Currently, only the multimodal embedding model provided by Alibaba Cloud DashScope is supported.
- Using a non-multimodal (text-only) embedding model for
content_type=imagewill result in an error.
- Unsupported features:
- Doc mode (slicing) is not supported.
- Other multimodal types such as video and audio are not supported.
- The same semantic index cannot handle both text and image URLs; you must create separate indexes for
content_type=textandcontent_type=image.
- Column type limitations:
- Image semantic indexes can only be created on
VARCHARcolumns. The following types are not supported:TEXT/LONGTEXT/TINYTEXT/MEDIUMTEXTCHAR/BINARY/VARBINARY/BLOB
- Image semantic indexes can only be created on
- URL constraints:
- Only the
http://andhttps://protocols are supported. - An empty string is not allowed for
content_type=image. - Supported image formats depend on the AI model used (typically JPEG, PNG, WebP, GIF, etc.). Storing images as binary data (e.g.,
BLOB/VARBINARY) is currently not supported.
- Only the
- Query limitations:
- Cross-modal retrieval (searching for images by text) requires the multimodal embedding model to support both text and image inputs and produce outputs in the same vector space.
- Other considerations:
- The
content_typecannot be changed after creation. To modify it, you must delete the index and recreate it. - If you need to create both text and image semantic indexes on the same column, enable the
_enable_multiple_semantic_indexes_on_columnconfiguration and use theVECTOR_INDEXhint to specify the index.
- The
- Private OSS images:
- Object storage buckets are private by default. Directly writing an object URL may cause a vectorization error due to authentication failure. For images on OSS, you must first generate a pre-signed URL and then write it to the database or pass it as a query parameter.
- You can generate a pre-signed URL through the OSS console or the Alibaba Cloud SDK.
- A pre-signed URL has an expiration time. When
sync_mode=manualorasync, vectorization is performed asynchronously in the background. It is recommended to set the expiration time to more than 1 hour. - After vectorization is complete, OceanBase Database stores only the vector values and does not persist the URL. An expired URL does not affect the existing vector index.
Manually enable semantic indexing
Enable semantic indexes by using the tenant-level configuration item _enable_semantic_index. It is disabled by default:
ALTER SYSTEM SET _enable_semantic_index = true;
Index syntax and description
Create
Semantic indexes can be created during table creation or after table creation. Consider the following points when creating an index:
- You must specify a
VARCHARcolumn for creating an index. - You can create multiple semantic indexes on the same column.
- The
model,sync_mode, andai_service_tierparameters are not supported for regular vector indexes. ai_service_tier=BATCHis supported only when you create an index after table creation. It is not supported during table creation.
You can use the CREATE TABLE statement to create a semantic index and initiate background tasks synchronously or asynchronously through index parameters. In synchronous mode, VARCHAR data is automatically converted into vector data during insertion. In asynchronous mode, data conversion is completed periodically or manually.
The syntax is as follows:
CREATE TABLE table_name (
column_name1 data_type1,
column_name2 VARCHAR, -- Column for text or image URLs
...,
VECTOR INDEX index_name (column_name2) WITH (param1=value1, param2=value2, ...)
);
The description is as follows:
- We recommend that you use the
VARCHAR(2048)orVARCHAR(4096)type to achieve optimal search performance.
You can create a semantic index on an existing VARCHAR column of a table. When creating an index after table creation, a background task is initiated synchronously or asynchronously based on the provided index parameters. In synchronous mode, all existing VARCHAR data is vector-embedded. In asynchronous mode, embedding is completed periodically or manually.
The syntax is as follows:
CREATE VECTOR INDEX index_name
ON table_name(varchar_column_name)
WITH (param1=value1, param2=value2, ...);
When you create an index after table creation, you can specify ai_service_tier=BATCH in the WITH clause to submit requests in batches through files. The system processes all requests asynchronously.
The param parameters are described as follows:
Parameter |
Default Value |
Value range |
Required |
Description |
Remarks |
|---|---|---|---|---|---|
distance |
l2/inner_product/cosine |
Yes | Specifies the vector distance algorithm. | l2 represents Euclidean distance, inner_product represents inner product distance, and cosine represents cosine distance. |
|
type |
hnsw/hnsw_bq/hnsw_sq |
Yes | Specifies the index algorithm. | ||
lib |
vsag |
vsag |
No | Specifies the vector index library. | Currently, only the VSAG vector index library is supported. |
model |
Registered model_key |
Yes | Specifies the embedding model. | You must register the model by using the legacy CREATE_AI_MODEL API and specify its model_key in the WITH clause. The new provider/model format is not supported.
NoteThis parameter is not supported for regular vector indexes. |
|
dim |
A positive integer no greater than 4,096 | Yes | Specifies the dimension of the embedded vector. | The value must match a dimension supported by the model. If the model supports multiple dimensions, you can select one based on your requirements: use 768 or 1,024 for higher accuracy, or 256 or 512 for better performance. | |
sync_mode |
async |
immediate/manual/async |
No | Specifies the data and index synchronization mode. | immediate indicates synchronous mode, manual indicates manual mode, and async indicates asynchronous mode. Image semantic indexes support only async and manual, not immediate.
NoteThis parameter is not supported for regular vector indexes. |
sync_interval |
10s |
A time interval, such as 10s, 1h, or 1d |
No | Specifies how often an asynchronous background task is triggered. | The numeric value must be positive. Supported units include seconds (s), hours (h), and days (d). |
content_type |
text |
text/image |
No | Specifies the content type of the semantic index. | text indicates a text semantic index, and image indicates an image URL semantic index. This parameter cannot be changed after the index is created.
NoticeThis parameter is supported starting from V4.6.0 BP1. |
ai_service_tier |
STANDARD |
STANDARD/BATCH |
No | Specifies the AI service tier used to create the index. | STANDARD sends synchronous HTTP requests row by row. BATCH uses batch files for asynchronous processing and completes embedding through the AI service provider's Batch API. |
allow_null_on_failure |
FALSE |
TRUE/FALSE |
No | Specifies the fault-tolerance behavior when batch embedding fails. | This parameter takes effect only when ai_service_tier=BATCH. If set to TRUE, the vector for a failed row is set to NULL, and index creation continues. If set to FALSE, a failure on any row aborts the DDL operation. |
Other vector index parameters, such as m, ef_construction, and ef_search, are used in the same way as those of regular HNSW/HNSW_BQ indexes. For more information, see HNSW series indexes in References.
Search
Semantic indexes support two search methods:
- Search by original content (text or image URL)
- Search by vector
- You can specify the
APPROXIMATE/APPROXclause to perform a nearest neighbor search using the vector index. - You can also omit the
APPROXIMATE/APPROXclause and use a full-table scan for a brute-force search.
- You can specify the
The usage instructions and considerations for the hints are as follows:
- When multiple semantic indexes exist on the same column, you must explicitly specify the desired vector index using the
VECTOR_INDEXhint; otherwise, the system will report an error. With this hint, you can not only select a specific vector index in the query statement but also flexibly configure pre- or post-filter conditions to improve search accuracy and flexibility. - The
VECTOR_INDEXhint can be used together with theINDEXhint. - For usage instructions on the preceding two points, see VECTOR_INDEX Hint.
Use the semantic_distance expression to pass in raw text or an image URL for a vector search.
The syntax is as follows:
SELECT ... FROM table_name
ORDER BY semantic_distance(column_name, 'query_content' [, 'query_type'])
APPROXIMATE|APPROX
LIMIT n;
Where:
column_name: The column (text or image URL) specified when creating the semantic index.query_content: The raw content to search (text string or image URL).query_type: Optional. Valid values are'TEXT'and'IMAGE'. If this parameter is omitted, the value defaults to'TEXT'. For an image-to-image search on an image semantic index, explicitly specify'IMAGE'. This parameter is supported starting from V4.6.0 BP1.n: The number of result rows to return.
Note
semantic_distance supports only approximate searches with APPROXIMATE/APPROX.
With the
APPROXIMATEclause: Use thesemantic_vector_distanceexpression to pass in vectors for search. When the search statement contains theAPPROXIMATE/APPROXclause, the vector index is used for the search. The syntax is as follows:SELECT ... FROM table_name ORDER BY semantic_vector_distance(column_name, 'query_vector') [APPROXIMATE|APPROX] LIMIT n;Where:
column_name: The text column specified when creating the semantic index.query_vector: The query vector.n: The number of result rows to return.
Without the
APPROXIMATEclause: Use thesemantic_vector_distanceexpression to pass in vectors for retrieval. When theAPPROXIMATE/APPROXclause is omitted, a full-table scan is performed for a brute-force retrieval to obtain the exact results of the n nearest rows. During retrieval execution, the type ofdistanceis obtained from the table schema, and then a full-table scan is performed. The vector distance is calculated for each row to ensure accurate results. The syntax is as follows:SELECT ... FROM table_name ORDER BY semantic_vector_distance(column_name, 'query_vector') LIMIT n;The parameter meanings are consistent with those when the
APPROXIMATEclause is used.
For detailed syntax descriptions of the APPROXIMATE/APPROX clause, see the HNSW series of index documents in the related documentation at the end of this topic.
Examples of create, update, search, and delete operations
The DML operations for semantic indexes (INSERT, UPDATE, and DELETE) are identical to those for regular vector indexes. When you insert or update VARCHAR data, the system embeds the data synchronously or asynchronously based on the sync_mode setting.
Create during table creation
Example of creating a text semantic index
Create the
vector_idxindex when creating the test tableitems:-- Assume that the ob_embed model has been created previously (see the "Prerequisites" section to register the model). CREATE TABLE items ( id BIGINT PRIMARY KEY, doc VARCHAR(100), VECTOR INDEX vector_idx(doc) WITH (distance=l2, lib=vsag, type=hnsw_sq, model=ob_embed, dim=1024, sync_mode=async, sync_interval=10s) );Insert a row of data into the test table
items, and the system automatically performs embedding:INSERT INTO items(id, doc) VALUES(1, 'Rose');Example of creating an image semantic index
-- Assume that the ob_vl_embed model has been created previously (see the "Prerequisites" section to register the model). CREATE TABLE product_images ( id INT PRIMARY KEY, image_url VARCHAR(4096), product_name VARCHAR(256), VECTOR INDEX img_idx(image_url) WITH (distance=l2, type=hnsw, lib=vsag, model=ob_vl_embed, dim=768, sync_mode=manual, content_type=image) ) ORGANIZATION HEAP;
Create after table creation
Example of creating a text semantic index after table creation
After creating the test table
items, use theCREATE VECTOR INDEXstatement to create thevector_idxindex:CREATE TABLE items ( id BIGINT PRIMARY KEY, doc VARCHAR(100) ); -- Assume that the ob_embed model has been created previously (see the "Prerequisites" section to register the model). CREATE VECTOR INDEX vector_idx ON items (doc) WITH (distance=l2, lib=vsag, type=hnsw_sq, model=ob_embed, dim=1024, sync_mode=async, sync_interval=10s);Insert a row of data into the test table
items, and the system automatically performs embedding:INSERT INTO items(id, doc) VALUES(1, 'Rose');Example of creating an image semantic index after table creation
CREATE TABLE product_images ( id INT PRIMARY KEY, image_url VARCHAR(4096), product_name VARCHAR(256) ) ORGANIZATION HEAP; INSERT INTO product_images VALUES (1, 'https://example.com/shoes.jpg', 'Red Shoes'), (2, 'https://example.com/bag.jpg', 'Leather Bag'); CREATE VECTOR INDEX img_idx ON product_images(image_url) WITH (distance=l2, type=hnsw, lib=vsag, model=ob_vl_embed, dim=768, sync_mode=manual, content_type=image);
Update
When updating data of the VARCHAR type, the system re-embeds the vectors:
- Synchronous mode: Re-embedding is performed immediately after the update.
- Asynchronous mode: The background task re-embeds the vectors during the next trigger cycle.
Example:
UPDATE items SET doc = 'Lily' WHERE id = 1;
Delete
The deletion operation is the same as that for a regular vector index; you can directly delete the data.
Example:
DELETE FROM items WHERE id = 1;
Search
-- Assume that the ob_embed model has been created previously.
CREATE TABLE items (
id INT PRIMARY KEY,
doc varchar(100),
VECTOR INDEX vector_idx(doc)
WITH (distance=l2, lib=vsag, type=hnsw_sq, model=ob_embed, dim=1024, sync_mode=immediate)
);
INSERT INTO items(id, doc) VALUES(1, 'Rose');
INSERT INTO items(id, doc) VALUES(2, 'Sunflower');
INSERT INTO items(id, doc) VALUES(3, 'Rose');
INSERT INTO items(id, doc) VALUES(4, 'Sunflower');
INSERT INTO items(id, doc) VALUES(5, 'Rose');
-- Search by original text
SELECT id, doc FROM items
ORDER BY semantic_distance(doc, 'Sunflower')
APPROXIMATE LIMIT 3;
The result is as follows:
+----+-----------+
| id | doc |
+----+-----------+
| 4 | Sunflower |
| 2 | Sunflower |
| 3 | Rose |
+----+-----------+
3 rows in set
Use the semantic_vector_distance expression to pass vectors for search. When a search statement contains the APPROXIMATE/APPROX clause, vector indexing is used for the search.
-- Assume that the ob_embed model has been created previously (see the "Prerequisites" section to register the model).
CREATE TABLE items (
id INT PRIMARY KEY,
doc varchar(100),
VECTOR INDEX vector_idx(doc)
WITH (distance=l2, lib=vsag, type=hnsw_sq, model=ob_embed, dim=1024, sync_mode=immediate)
);
INSERT INTO items(id, doc) VALUES(1, 'Rose');
INSERT INTO items(id, doc) VALUES(2, 'Lily');
INSERT INTO items(id, doc) VALUES(3, 'Sunflower');
INSERT INTO items(id, doc) VALUES(4, 'Rose');
-- Obtain the query vector first
SET @query_vector = AI_EMBED('ob_embed', 'Sunflower');
-- Search by index using vectors
SELECT id, doc FROM items
ORDER BY semantic_vector_distance(doc, @query_vector)
APPROXIMATE LIMIT 3;
The result is as follows:
+----+-----------+
| id | doc |
+----+-----------+
| 3 | Sunflower |
| 1 | Rose |
| 4 | Rose |
+----+-----------+
3 rows in set
-- Perform an exact nearest-neighbor search by using vectors
SELECT id, doc FROM items
ORDER BY semantic_vector_distance(doc, @query_vector)
LIMIT 3;
The result is as follows:
+----+-----------+
| id | doc |
+----+-----------+
| 3 | Sunflower |
| 4 | Rose |
| 1 | Rose |
+----+-----------+
3 rows in set
Example of semantic index search for images:
-- Image Search by Image (IMAGE → IMAGE)
SELECT id, product_name, image_url
FROM product_images
ORDER BY semantic_distance(image_url, 'https://example.com/query.jpg', 'IMAGE')
APPROXIMATE LIMIT 10;
-- Search for images by text (TEXT → IMAGE, cross-modal)
SELECT id, product_name, image_url
FROM product_images
ORDER BY semantic_distance(image_url, 'a photo of red shoes', 'TEXT')
APPROXIMATE LIMIT 10;
-- Filter by WHERE clause
SELECT id, product_name
FROM product_images
WHERE product_name LIKE 'Red%'
ORDER BY semantic_distance(image_url, 'https://example.com/query.jpg', 'IMAGE')
APPROXIMATE LIMIT 10;
