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
Use vector_memory_limit to limit the memory available to the vector module. The default value, 0M, sets the limit to 50% of effective memory. Effective memory is physical memory, capped by any finite cgroup memory limit. To set a fixed limit, specify a nonzero value.
ALTER SYSTEM SET vector_memory_limit = '2G';
vector_memory_limit constrains the vector module. Setting it explicitly does not change memory_budget. For the calculation rules, see Configure memory for seekdb.
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]'
);