In-Memory Sparse Vector Index
This topic describes how to create, search, and use the in-memory sparse vector index in seekdb.
Overview
The in-memory sparse vector index is an efficient index type provided by seekdb for sparse vector data (vectors with most elements being zero). This index requires the complete index to be loaded into memory and supports DML operations and real-time search.
To enhance query performance for sparse vectors, seekdb integrates the SINDI index, which is 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
The in-memory sparse vector index supports the following features:
| Module | Feature | Description |
|---|---|---|
| DDL | Create a 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 a 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 data | The DML operations for the in-memory sparse vector index are consistent with those for a regular vector index. |
| Search | Vector search | You can perform vector search using SQL functions. |
| Search | Query parameters | You can set query-level parameters in the parameters clause during search. |
| DBMS_VECTOR | REFRESH_INDEX | Perform incremental index refresh. |
| DBMS_VECTOR | REBUILD_INDEX | Perform full index rebuild. |
Memory estimation and actual usage
You can use the DBMS_VECTOR package to estimate the memory required for an index. The usage is the same as for a dense index. The following describes the special requirements for sparse vector indexes:
- The
IDX_TYPEparameter must be specified asSINDI. The parameter is case-insensitive.
Syntax and description
The in-memory sparse vector index supports creating an index when creating a table and creating an index after table creation. When creating an 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 creating an index. - The index type must be specified as
sindi, indicating the creation of an in-memory sparse vector index. - The
inner_productalgorithm is the only supported distance algorithm. - 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 an index 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. | The inner_product algorithm is the only supported distance algorithm for sparse vector indexes. | |
type | sindi | Yes | The type of index algorithm. | Indicates the creation of an in-memory sparse vector index. | |
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 when prune is set to false, an error will be returned. |
refine | false | true/false | No | Whether to perform re-ranking. | If refine is set to true, the original sparse vectors will be retrieved for high-precision distance calculation and re-ranking. This requires storing an additional copy of the original vector data. This parameter is only valid when prune is set to true. |
drop_ratio_build | 0 | [0, 0.9] | No | The pruning ratio for sparse vector data. | When a new sparse vector is inserted, the query_length * drop_ratio_build smallest values will be pruned 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 valid when prune is set to true. |
drop_ratio_search | 0 | [0, 0.9] | No | The pruning ratio for search sparse vector values. | A larger value indicates more pruning, lower accuracy, and higher performance. You can also set this parameter in the parameters clause during search. Query parameters have higher priority. |
refine_k | 4.0 | [1.0, 1000.0] | No | The ratio of results to be re-ranked. | The search results will be limited to limit_k * refine_k items, and the original vectors will be retrieved for re-ranking. This parameter is only valid when refine is set to true. You can also set this parameter in the parameters clause during search. Query parameters have higher priority. |
Create an index 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 description is the same as when creating an index when creating a table. For more information, see the preceding section.
Examples of creation, update, and deletion
Create an index 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 an index 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 deletion operation is the same as for a regular vector index. 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, and uses the APPROXIMATE/APPROX keyword for 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_name: theSPARSEVECTORcolumn specified when the sparse vector index was created.query_vector: the query vector, which can be a string in sparse vector format, such as'{1:2.4, 3:1.5}'.n: the number of result rows to return.PARAMETERS: optional query-level parameters for settingdrop_ratio_searchandrefine_k.
Search syntax requirements
- The
APPROXIMATE/APPROXkeyword must be specified for the search to use the vector index instead of a full table scan. - The
ORDER BYandLIMITclauses must be included. ORDER BYonly supports a single vector condition.- The value of
LIMIT + OFFSETmust be in the range(0, 16384]. - If the
LIMITclause is not specified, an error will be returned.
Distance function usage rules
- If
APPROXIMATE/APPROXis specified, the current version's supported distance function must be called, and it must match the vector index algorithm for the search to use the vector index. - If
APPROXIMATE/APPROXis specified, and the distance function does not match the vector index algorithm, the search will not use the vector index, but no error will be returned. - If
APPROXIMATE/APPROXis specified, and the distance function is not supported by the current version, the search will not use the vector index, and an error will be returned. - If
APPROXIMATE/APPROXis not specified, and the current version's supported distance function is called, the search will not use the vector index, but no error will be returned.
Other considerations
- The
WHEREcondition serves as a filter for the vector index search. - Recall rate is affected by build parameters and search parameters.
- Index search parameters can be set when creating the index. If adjustments are needed after the index is created, you can modify the table-level attributes using DBMS_VECTOR.REBUILD_INDEX or adjust the query-level parameter
ef_searchduring the query.
Special requirements for sparse vector indexes
For detailed requirements, see Dense vector indexes. Here are the special requirements for sparse vector indexes:
- Query parameter priority:
PARAMETERS-set query-level parameters > build-index-set query parameters > default values. drop_ratio_search: range[0, 0.9], default value0. The ratio of sparse vector values to prune during the query. A higher value means more pruning, lower accuracy, and higher performance. The smallestquery_length * drop_ratio_searchvalues are pruned based on their magnitude. At least one value will always be retained.refine_k: range[1.0, 1000.0], default value4.0. The proportion of results to participate in re-ranking. Querylimit_k * refine_kresults and obtain the original vectors for re-ranking. 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 detailed information about the sparse vector data type, see Vector data types.
- For detailed information about vector distance functions, see Vector functions.
- For information about monitoring and maintaining dense vector indexes, see Index monitoring and maintenance.
- For information about memory estimation and actual usage of vector indexes, see Vector index memory management.