Skip to main content
Version: V1.4.0

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:

ModuleFeatureDescription
DDLCreate sparse vector index when creating a tableYou can create a sparse vector index on a SPARSEVECTOR column when you create a table. The maximum number of dimensions supported is 500,000.
DDLCreate sparse vector index after table creationYou can create a sparse vector index on a SPARSEVECTOR column of an existing table. The maximum number of dimensions supported is 500,000.
DMLInsert, update, and deleteThe DML syntax for in-memory sparse vector index is the same as that for conventional vector indexes.
SearchVector searchYou can perform vector search by using SQL functions.
SearchQuery parametersYou can set query-level parameters by using the parameters clause during search.
DBMS_VECTORREFRESH_INDEXPerform incremental index refresh.
DBMS_VECTORREBUILD_INDEXPerform 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_TYPE parameter must be specified as SINDI, 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 SPARSEVECTOR column.
  • The VECTOR keyword 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_product algorithm 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​

ParameterDefault valueValue rangeRequiredDescriptionNotes
distanceinner_productYesThe type of vector distance algorithm.Only the inner_product algorithm is supported for sparse vector indexes.
typesindiYesThe type of index algorithm.Indicates that an in-memory sparse vector index is created.
libvsagvsagNoThe type of vector index library.Only the VSAG vector library is supported.
prunefalsetrue/falseNoWhether 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.
refinefalsetrue/falseNoWhether 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_build0[0, 0.9]NoThe 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_search0[0, 0.9]NoThe 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_k4.0[1.0, 1000.0]NoThe 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;

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_name is the SPARSEVECTOR column specified when the sparse vector index was created.
  • query_vector is the query vector, which can be a string in sparse vector format, such as '{1:2.4, 3:1.5}'.
  • n is the number of result rows to return.
  • PARAMETERS is an optional query-level parameter used to set drop_ratio_search and refine_k.

Search syntax requirements​

  • You must specify the APPROXIMATE or APPROX keyword for the search to use the vector index instead of a full table scan.
  • You must include the ORDER BY and LIMIT clauses.
  • The ORDER BY clause only supports a single vector condition.
  • The value of LIMIT + OFFSET must be in the range (0, 16384].
  • If you do not specify the LIMIT clause, an error will be returned.

Rules for using distance functions​

  • If you specify the APPROXIMATE or APPROX keyword, 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 APPROXIMATE or APPROX keyword, 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 APPROXIMATE or APPROX keyword, 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 APPROXIMATE or APPROX keyword, 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 WHERE clause 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_search in 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 of 0. 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 smallest query_length * drop_ratio_search values. At least one value will always be retained.
  • refine_k: a value in the range [1.0, 1000.0], with a default value of 4.0. This parameter specifies the proportion of results to participate in reranking. The search will retrieve limit_k * refine_k results and rerank them using the original vectors. This parameter is only effective when refine=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​