Perform incremental vector retrieval and incremental index updates

Updated at:

Limitations

  • Both the Base and Query input tables must be Append Delta Tables. The Base table must also meet the table format requirements for vector indexes.

  • The examples in this topic apply only to standard HGraph indexes. Indexes with a category must also keep the category column compatible with the index definition. IVF and other algorithms are outside the scope of this topic.

  • The vector dimensions of the Base and Query tables must be the same, and the distance type used in queries must be the same as the one defined in the index.

  • Before incremental retrieval is used, an index must be created and the initial full build must be completed. CREATE VECTOR INDEX creates only the index definition.

  • The following procedure demonstrates only the INSERT INTO append scenario. The behavior of updates, deletes, and overwrites must be verified separately in the target public cloud version.

  • The account that runs the statements must have permissions to create tables, write data, run queries, create indexes, and rebuild indexes.

If not.support.append2.table is returned when a table is created, check the project requirements and table configuration for Append Delta Tables. If the error persists after these requirements are met, contact Alibaba Cloud technical support.

Procedure

By default, the following SQL statements are executed in the same session in sequence. Wait for each step to complete before running the next step. If the statements are submitted as separate jobs, add the required SET configurations to each job.

Step 1: Prepare the Base and Query tables

SET odps.sql.type.system.odps2=true;
SET odps.sql.type.vector.enable=true;
SET odps.table.append2.enable=true;

CREATE TABLE vector_inc_demo_base (
  doc_id BIGINT,
  title STRING,
  embedding VECTOR(FLOAT, 3)
)
TBLPROPERTIES (
  'transactional'='true',
  'table.format.version'='2'
);

CREATE TABLE vector_inc_demo_query (
  query_id BIGINT,
  query_embedding VECTOR(FLOAT, 3)
)
TBLPROPERTIES (
  'transactional'='true',
  'table.format.version'='2'
);

INSERT INTO vector_inc_demo_base (doc_id, title, embedding)
SELECT 1L, 'Historical document A', vector(1.0F, 0.0F, 0.0F)
UNION ALL
SELECT 2L, 'Historical document B', vector(0.0F, 1.0F, 0.0F);

INSERT INTO vector_inc_demo_query (query_id, query_embedding)
SELECT 101L, vector(0.0F, 0.0F, 1.0F);

Step 2: Create an index and complete the initial full build

CREATE VECTOR INDEX idx_vector_inc_demo
ON vector_inc_demo_base (embedding)
IDXPROPERTIES (
  'algorithm'='hgraph',
  'distance_type'='cosine',
  'build_params'='{"max_degree":16,"ef_construction":128}'
);

ALTER TABLE vector_inc_demo_base
REBUILD INDEX idx_vector_inc_demo;

DESC INDEX idx_vector_inc_demo ON vector_inc_demo_base;

Confirm that the REBUILD INDEX job succeeds, and check the index status, refresh time, and coverage information. If only the index definition has been created and the initial build has not been completed, incremental retrieval cannot be performed.

Step 3: Append data and perform incremental retrieval

INSERT INTO vector_inc_demo_base (doc_id, title, embedding)
SELECT 3L, 'New document C', vector(0.0F, 0.0F, 1.0F);

SET odps.sql.vector.index.enable.incremental.search=true;

SELECT
  _query.query_id,
  _base.doc_id,
  _base.title,
  distance
FROM VECTOR_SEARCH(
  TABLE vector_inc_demo_base, embedding,
  TABLE vector_inc_demo_query, query_embedding,
  3, COSINE
);

"New document C" uses the same vector as the Query table, so the distance should be close to 0. If the index has been updated by another maintenance task, the query still returns this record, but the job logs may not indicate incremental retrieval. Determine the execution path based on the logs.

Step 4: Update the persisted index

SET odps.sql.vector.index.enable.incremental.rebuild=true;

ALTER TABLE vector_inc_demo_base
REBUILD INDEX idx_vector_inc_demo AUTO;

DESC INDEX idx_vector_inc_demo ON vector_inc_demo_base;

The system chooses between incremental and full rebuild for the current rebuild only when the incremental rebuild switch is enabled and AUTO is specified. AUTO does not create a periodic job and does not guarantee that the incremental method is used.

To explicitly perform a full rebuild, omit AUTO:

ALTER TABLE vector_inc_demo_base
REBUILD INDEX idx_vector_inc_demo;

Step 5: Verify the query results

If the Base and Query data remain unchanged, brute-force search results can be used as a reference:

SELECT
  _query.query_id,
  _base.doc_id,
  distance
FROM VECTOR_SEARCH(
  TABLE vector_inc_demo_base, embedding,
  TABLE vector_inc_demo_query, query_embedding,
  3, COSINE,
  '{"use_brute_force":true}'
);

This example verifies whether both existing rows and newly added rows participate in retrieval. In production environments, sample the results and compare the query latency rather than frequently running full brute-force searches on large tables.

Check the index status and execution path

Run the following statements to view the index definition and status:

SHOW INDEXES ON vector_inc_demo_base;

DESC INDEX idx_vector_inc_demo ON vector_inc_demo_base;

Information

Description

SHOW INDEXES returns the index definition

Indicates that the index is registered. This cannot be used to determine whether the physical index has been built.

status=ACTIVE

Usually indicates that the index watermark has caught up with the table data watermark.

status=STALE

Usually indicates that the index does not yet cover the latest data. Queries can still run after incremental retrieval is enabled.

last_refresh_time

Indicates the most recent build time. This cannot be used alone to determine the build method.

coverage_percentage

Provides a reference for index coverage. Do not treat it as an exact miss rate or recall rate.

The compilation warnings of a query job may include the following messages:

Message

Description

incremental vector-index search will be used

This query reuses the existing index and supplements the data that the index does not cover.

temporary runtime build will be used

This query uses a temporary runtime index.

The rebuild job may output the following messages:

Message

Description

Vector index rebuild will use incremental build.

This rebuild uses an incremental build.

Vector index rebuild will use full build.

This rebuild uses a full build.

To verify the actual build mode, open LogView from the job details, go to Summary, and check VECTOR_INDEX_REBUILD:

  • build_mode=incremental_build: incremental build.

  • build_mode=full_build: full build.

Incremental queries do not publish new index metadata. Therefore, the index may still show as STALE.

Common reasons for AUTO to choose a full rebuild

Scenario

System behavior

The incremental rebuild switch is not enabled, or AUTO is not specified in the statement

Performs a full rebuild

This is the initial build, or no reusable full baseline exists

Performs a full rebuild

The index status, transaction information, or partition conditions are not suitable for incremental reuse

May perform a full rebuild

The index definition is incompatible with the existing index

An error may be returned, and a full rebuild is required as prompted

The system chooses the build method for the current rebuild job. If the selected partitions contain an index that is not suitable for incremental reuse, the entire job may switch to a full rebuild. After the algorithm, dimensions, distance type, or build parameters are changed, check the index compatibility again.

Maintain tables with continuous writes

Rebuild the index after an overwrite

INSERT OVERWRITE is controlled by odps.sql.auto.rebuild.vector.index. This mechanism increases the duration of write jobs, and the applicable table and index types depend on the target version. If a table has multiple vector indexes, explicitly run REBUILD INDEX to maintain the target index.

To schedule overwrites and index maintenance separately, disable chained rebuild in the overwrite job:

SET odps.sql.auto.rebuild.vector.index=false;

Then submit a separate rebuild job for the target index.

Maintain by partition

When only a few partitions are updated, the rebuild scope can be limited:

SET odps.sql.vector.index.enable.incremental.rebuild=true;

ALTER TABLE your_partitioned_table
PARTITION (ds='20261117')
REBUILD INDEX your_vector_index AUTO;

DESC INDEX your_vector_index ON your_partitioned_table
PARTITION (ds='20261117');

Replace the table name, partition column, partition value, and index name with the actual objects.

Schedule the maintenance frequency

The system may trigger index maintenance in the background, but the execution time is affected by system scheduling and resource queuing. If the maintenance completion time is critical, explicitly submit the REBUILD INDEX job through a business scheduler and wait for it to complete.

When query latency starts to increase, check the accumulation of new data and increase the maintenance frequency as appropriate. After large batch deletions, overwrites, or index definition changes, check whether a full rebuild is required.

FAQ

Why is there still no available index after incremental retrieval is enabled?

Incremental retrieval requires a fully built index as the basis. Create the index and run the initial full REBUILD INDEX.

To bypass the vector index, use VECTOR_SEARCH with {"use_brute_force":true}. Brute-force search does not generate a persisted index, and queries may be slow when the data volume is large.

Why is a full rebuild still performed after AUTO is specified?

AUTO allows the system to choose the build method based on the index status. For an initial build, a missing valid baseline, or an index that is not suitable for reuse, the system chooses a full rebuild. This does not indicate that the incremental rebuild switch is ineffective.

Why does the index remain STALE after an incremental query succeeds?

An incremental query supplements only the data used for the current query and does not publish new index files or metadata. Run REBUILD INDEX, wait for the job to succeed, and then check the index status.

Why do queries slow down as incremental data accumulates?

A query must load the existing index, read the newly added data, and perform visibility filtering. The more incremental data there is, the more significant this overhead may be. Use LogView to check the time consumed by each stage, and adjust the maintenance frequency of the persisted index.

How does the system handle a lagging index after incremental retrieval is disabled?

After this switch is disabled, the system does not use incremental retrieval to supplement a lagging persisted index. For information about how queries run when no index is available, see the VECTOR_SEARCH documentation.

Related documents