In-Memory Sparse Vector Index
This topic describes how to create, search, and use an in-memory sparse vector index in seekdb.
Overview
In-memory sparse vector index is an efficient index type provided by seekdb for sparse vector data (vectors with most elements as zero). In-memory sparse vector index requires loading the entire index into memory and supports DML and real-time search.
To improve query performance for sparse vectors, seekdb integrates the SINDI index, which is a sparse vector index based on the VSAG algorithm library. This index outperforms disk-based sparse vector indexes and is suitable for use when sufficient memory resources are available.
Feature support
In-memory sparse vector index supports the following features:
| Module | Feature | Description |
|---|---|---|
| DDL | Create sparse vector index when creating a table | You can create a sparse vector index on a SPARSEVECTOR column when you create a table. The maximum number of dimensions supported is 500,000. |
| DDL | Create sparse vector index after table creation | You can create a sparse vector index on a SPARSEVECTOR column of an existing table. The maximum number of dimensions supported is 500,000. |
| DML | Insert, update, and delete | The DML syntax for in-memory sparse vector index is the same as that for conventional vector indexes. |
| Search | Vector search | You can perform vector search by using SQL functions. |
| Search | Query parameters | You can set query-level parameters by using the parameters clause during search. |
| DBMS_VECTOR | REFRESH_INDEX | Perform incremental index refresh. |
| DBMS_VECTOR | REBUILD_INDEX | Perform full index rebuild. |
Index memory estimation and actual usage query
You can use the DBMS_VECTOR package to estimate the memory required for an index. The usage is the same as that for dense indexes. This section describes the special requirements for sparse vector indexes:
- The
IDX_TYPEparameter must be specified asSINDI, case-insensitive.
Syntax and description
In-memory sparse vector index supports two creation methods: create when creating a table and create after table creation. When you create an in-memory sparse vector index, note the following:
- The maximum number of dimensions supported for a sparse vector index column is 500,000.
- A sparse vector index must be created on a
SPARSEVECTORcolumn. - The
VECTORkeyword must be specified when you create an index. - The index type must be specified as
sindi, indicating that an in-memory sparse vector index is created. - Only the
inner_productalgorithm is supported for distance calculation. - The parameters and descriptions for creating an index after table creation are the same as those for creating an index when creating a table.
Create when creating a table
You can use the CREATE TABLE statement to create a sparse vector index.
Syntax
CREATE TABLE table_name (
column_name1 data_type1,
column_name2 SPARSEVECTOR,
...,
VECTOR INDEX index_name (column_name2) WITH (param1=value1, param2=value2, ...)
);
Parameter description
| Parameter | Default value | Value range | Required | Description | Notes |
|---|---|---|---|---|---|
distance | inner_product | Yes | The type of vector distance algorithm. | Only the inner_product algorithm is supported for sparse vector indexes. | |
type | sindi | Yes | The type of index algorithm. | Indicates that an in-memory sparse vector index is created. | |
lib | vsag | vsag | No | The type of vector index library. | Only the VSAG vector library is supported. |
prune | false | true/false | No | Whether to prune vectors. | If prune is set to true, you must also set the refine and drop_ratio_build parameters. If prune is set to false, you can perform full-precision search. If you set refine to true or drop_ratio_build to a value other than 0, an error will be returned. |
refine | false | true/false | No | Whether to rerank. | If refine is set to true, the original sparse vectors will be retrieved for high-precision distance calculation and reranking. This requires storing an additional copy of the original vector data. This parameter is only applicable when prune is set to true. |
drop_ratio_build | 0 | [0, 0.9] | No | The ratio of sparse vector data to be pruned. | When a new sparse vector is inserted, the query_length * drop_ratio_build smallest values will be trimmed based on their values. If refine is set to true, the original vector data will be retained. Otherwise, only the pruned data will be retained. This parameter is only applicable when prune is set to true. |
drop_ratio_search | 0 | [0, 0.9] | No | The ratio of search sparse vector values to be pruned. | A higher value indicates more pruning, lower accuracy, and higher performance. You can also set this parameter by using the parameters clause during search. Query parameters take precedence. |
refine_k | 4.0 | [1.0, 1000.0] | No | The ratio of results to be reranked. | The search results of limit_k * refine_k items will be retrieved, and the original vectors will be used for reranking. This parameter is only applicable when refine is set to true. You can also set this parameter by using the parameters clause during search. Query parameters take precedence. |
Create after table creation
You can create a sparse vector index on the SPARSEVECTOR column of an existing table.
Syntax
CREATE VECTOR INDEX index_name ON table_name(column_name) WITH (param1=value1, param2=value2, ...);
Parameter description
The parameter descriptions are the same as those for creating an index when creating a table. For more information, see the preceding sections.
Examples of creation, update, and deletion
Create when creating a table
Create a test table sparse_t1 and create a sparse vector index:
CREATE TABLE sparse_t1 (
c1 INT PRIMARY KEY,
c2 SPARSEVECTOR,
VECTOR INDEX sparse_idx1(c2)
WITH (lib=vsag, type=sindi, distance=inner_product)
);
Insert sparse vector data into the test table:
INSERT INTO sparse_t1 VALUES(1, '{1:0.1, 2:0.2, 3:0.3}');
INSERT INTO sparse_t1 VALUES(2, '{3:0.3, 2:0.2, 4:0.4}');
INSERT INTO sparse_t1 VALUES(3, '{3:0.3, 4:0.4, 5:0.5}');
Search the test table:
SELECT * FROM sparse_t1;
The returned result is as follows:
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | {1:0.1,2:0.2,3:0.3} |
| 2 | {2:0.2,3:0.3,4:0.4} |
| 3 | {3:0.3,4:0.4,5:0.5} |
+----+---------------------+
3 rows in set
Create after table creation
Create a test table and then create a sparse vector index:
CREATE TABLE sparse_t2 (
c1 INT PRIMARY KEY,
c2 SPARSEVECTOR
);
CREATE VECTOR INDEX sparse_idx2 ON sparse_t2(c2)
WITH (lib=vsag, type=sindi, distance=inner_product,
prune=true, refine=true, drop_ratio_build=0.1,
drop_ratio_search=0.5, refine_k=2.0);
Insert sparse vector data into the test table:
INSERT INTO sparse_t2 VALUES(1, '{1:0.1, 2:0.2, 3:0.3}');
Search the test table:
SELECT * FROM sparse_t2;
The returned result is as follows:
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | {1:0.1,2:0.2,3:0.3} |
+----+---------------------+
1 row in set
Update
When you update sparse vector data, the index is automatically maintained:
UPDATE sparse_t1 SET c2 = '{1:0.1}' WHERE c1 = 1;
Delete
The delete operation is the same as that for conventional vector indexes. You can directly delete data:
DELETE FROM sparse_t1 WHERE c1 = 1;
Search
The search syntax for sparse vector indexes is similar to that for dense vector indexes. You can use the APPROXIMATE or APPROX keyword to perform approximate nearest neighbor search.
Syntax
SELECT ... FROM table_name
ORDER BY inner_product(column_name, query_vector) [APPROXIMATE|APPROX]
LIMIT n [PARAMETERS(param1=value1, param2=value2)];
where:
column_nameis theSPARSEVECTORcolumn specified when the sparse vector index was created.query_vectoris the query vector, which can be a string in sparse vector format, such as'{1:2.4, 3:1.5}'.nis the number of result rows to return.PARAMETERSis an optional query-level parameter used to setdrop_ratio_searchandrefine_k.
Search syntax requirements
- You must specify the
APPROXIMATEorAPPROXkeyword for the search to use the vector index instead of a full table scan. - You must include the
ORDER BYandLIMITclauses. - The
ORDER BYclause only supports a single vector condition. - The value of
LIMIT + OFFSETmust be in the range(0, 16384]. - If you do not specify the
LIMITclause, an error will be returned.
Rules for using distance functions
- If you specify the
APPROXIMATEorAPPROXkeyword, the search will use the vector index if the distance function called is supported by the current version and matches the vector index algorithm. - If you specify the
APPROXIMATEorAPPROXkeyword, the search will not use the vector index if the distance function does not match the vector index algorithm, but no error will be returned. - If you specify the
APPROXIMATEorAPPROXkeyword, the search will not use the vector index and an error will be returned if the distance function is not supported by the current version. - If you do not specify the
APPROXIMATEorAPPROXkeyword, the search will not use the vector index, but no error will be returned if the distance function called is supported by the current version.
Other considerations
- The
WHEREclause will be used as a filter for the vector index search. - The recall rate is affected by the build parameters and search parameters.
- Index search parameters can be set when the index is created. If you need to adjust them after the index is created, you can modify the table-level attributes using DBMS_VECTOR.REBUILD_INDEX or specify the query-level parameter
ef_searchin your query.
Special requirements for sparse vector indexes
For detailed requirements, see Dense vector indexes. Here are the special requirements for sparse vector indexes:
- The priority of query parameters: query-level parameters set by
PARAMETERS> query parameters set when the index is built > default values. drop_ratio_search: a value in the range[0, 0.9], with a default value of0. This parameter specifies the ratio of sparse vector values to prune. A higher value means more pruning, lower accuracy, and higher performance. The search will prune the smallestquery_length * drop_ratio_searchvalues. At least one value will always be retained.refine_k: a value in the range[1.0, 1000.0], with a default value of4.0. This parameter specifies the proportion of results to participate in reranking. The search will retrievelimit_k * refine_kresults and rerank them using the original vectors. This parameter is only effective whenrefine=true.
Examples
Basic query
CREATE TABLE t1 (
c1 INT PRIMARY KEY,
c2 SPARSEVECTOR,
VECTOR INDEX idx1(c2)
WITH (lib=vsag, type=sindi, distance=inner_product)
);
INSERT INTO t1 VALUES(1, '{1:0.1, 2:0.2, 3:0.3}');
INSERT INTO t1 VALUES(2, '{3:0.3, 2:0.2, 4:0.4}');
INSERT INTO t1 VALUES(3, '{3:0.3, 4:0.4, 5:0.5}');
INSERT INTO t1 VALUES(4, '{5:0.5, 4:0.4, 6:0.6}');
INSERT INTO t1 VALUES(5, '{5:0.5, 6:0.6, 7:0.7}');
SELECT * FROM t1
ORDER BY negative_inner_product(c2, '{3:0.3, 4:0.4}')
APPROXIMATE LIMIT 4;
The result is as follows:
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 2 | {2:0.2,3:0.3,4:0.4} |
| 3 | {3:0.3,4:0.4,5:0.5} |
| 4 | {4:0.4,5:0.5,6:0.6} |
| 1 | {1:0.1,2:0.2,3:0.3} |
+----+---------------------+
Use query parameters
SELECT *, negative_inner_product(c2, '{3:0.3, 4:0.4}')
AS score FROM t1
ORDER BY score APPROXIMATE LIMIT 4
PARAMETERS(drop_ratio_search=0.5);
The result is as follows:
+----+---------------------+---------------------+
| c1 | c2 | score |
+----+---------------------+---------------------+
| 4 | {4:0.4,5:0.5,6:0.6} | -0.1600000113248825 |
| 3 | {3:0.3,4:0.4,5:0.5} | -0.2500000149011612 |
| 2 | {2:0.2,3:0.3,4:0.4} | -0.2500000149011612 |
+----+---------------------+---------------------+
3 rows in set
Index monitoring and maintenance
In-memory sparse vector indexes provide monitoring views and support index maintenance using the DBMS_VECTOR system package, including incremental refreshes and full rebuilds. The usage is the same as for dense indexes.
References
- For more information about the sparse vector data type, see Vector data types.
- For more information about vector distance functions, see Vector functions.
- For more information about monitoring and maintaining dense vector indexes, see Index monitoring and maintenance.
- For more information about memory estimation and actual usage of vector indexes, see Vector index memory management.