Store vector data
This topic describes how to store unstructured data, semi-structured data, and structured data in a single table in the database. This approach not only leverages the database's core capabilities but also supports hybrid search effectively.
Storage principles
The database can store data of different modalities, such as text, images, and videos, by converting them into vectors. This enables hybrid search by calculating the distances between vectors. Hybrid search can be simple, based on similarity search of a single vector, or complex, involving a combination of vector and scalar searches.
Since vector search is inherently approximate, it is essential to use various methods to enhance accuracy in practical applications. Accurate search results can provide significant value to your business.
Configure vector index memory
You can configure the memory for vector indexes by setting the ob_vector_memory_limit_percentage parameter:
The default value is 0, which indicates adaptive mode. In this mode, the system automatically adjusts the memory usage ratio for vector index data in the instance without manual intervention:
- If the instance has 8 GB or less of memory, the value is automatically set to
40. - If the instance has more than 8 GB of memory, the value is automatically set to
50.
Create a vector column
The following example shows how to create a table that stores vector data, spatial data, and relational data. The vector column has the VECTOR data type and must specify the dimension when created, with a maximum of 16000 dimensions. The spatial column has the GEOMETRY data type:
CREATE TABLE t (
-- Store relational data (structured data)
id INT PRIMARY KEY,
-- Store spatial data (semi-structured data)
g GEOMETRY,
-- Store vector data (unstructured data)
vec VECTOR(3)
);
Use the INSERT statement to insert vector data
Once a table with a VECTOR column is created, you can directly use the INSERT statement to insert vectors into the table. When inserting data, the vector must match the dimension specified when the table was created, otherwise an error will occur. This design ensures data consistency and query efficiency. Vectors are represented as standard floating-point number arrays, with each dimension value being a valid floating-point number. Here is a simple example:
INSERT INTO t (id, g, vec) VALUES (
-- Insert structured data
1,
-- Insert semi-structured data
ST_GeomFromText('POINT(1 1)'),
-- Insert unstructured data
'[0.1, 0.2, 0.3]'
);