Product Quantized HNSW Indexes

Overview

HNSW index offers excellent performance in terms of accuracy and speed, but it incurs significant memory overhead—especially with high-dimensional vectors and large datasets. Integrating product quantization techniques with HNSW index helps to reduce memory consumption while improving search efficiency.

Product quantization is a data compression technique used to shrink large, high-dimensional vector embeddings to reduce memory usage. Product quantization compresses each vector into a short code, and then the HNSW index uses compact representations and approximate distances during search. Integrating product quantization with HNSW index is beneficial in the following ways:

How Product Quantization Works?

The Product Quantization has the following stages that Oracle AI Database executes automatically when you create HNSW index with product quantization:

Create HNSW Index with Product Quantization

To create a product-quantized HNSW index, specify in the the CREATE VECTOR INDEX statement.:

Note Oracle AI Database might not be able to produce every requested compression ratio for every vector dimension. The range of compression factors allowed by product quantization depends on the vector datatype and dimension. For example, B is the number of bytes required to store a dimension (4 for FLOAT32 and 8 for FLOAT64) and D is the number of dimensions. A compression factor C is allowed if: (1) C is a positive number less or equal to 128, (2) B divides C and (3) C/B divides D. Select a compression ratio that is compatible with the dimensions and format of the indexed vectors

CREATE VECTOR INDEX vecidx ON vectab (veccol) 

ORGANIZATION inmemory graph DISTANCE cosine

QUANTIZATION PRODUCT COMPRESSION RATIO 16

PARAMETERS (type: HNSW, neighbors 32, efConstruction 64,

	             quantization rescore factor: 10);

Monitor Product Quantization

Additional product quantization information is available through V$VECTOR_INDEX. The information includes:

Query Rescoring for Product-Quantized HNSW Indexes

Product quantization reduces the memory required by an HNSW index. It divides a vector into smaller subvectors and replaces each subvector with the identifier of its nearest centroid in a trained codebook. The index stores these compact identifiers instead of the original vector values. During an approximate search, the HNSW index calculates distances using the product-quantized representations. Query rescoring refines the top-K results by recalculating distances using the original, unquantized vectors.

For a query that requests K rows, the database retrieves the following number of candidates: K Ă— rescore_factor. The database fetches the corresponding rows, calculates their distances using the original vectors, sorts the rescored candidates, and returns the final K rows. For example, if a query requests 10 rows and specifies a rescore factor of 4, the database rescores 40 candidates and returns the final 10 results.

The rescore factor is an integer from 1 through 100. Specifying RESCORE FACTOR is optional. Include it in the either HNSW index definition or an approximate search when you want the database to rescore a larger candidate set using the original vectors.

Specifying rescore factor when creating a product-quantized HNSW index:

CREATE VECTOR INDEX vecidx ON vectab (veccol)
  ORGANIZATION INMEMORY NEIGHBOR GRAPH
  WITH TARGET ACCURACY 90
  DISTANCE EUCLIDEAN
  QUANTIZATION PRODUCT
  COMPRESSION RATIO 16
  PARAMETERS (
    TYPE HNSW,
    NEIGHBORS 32,
    EFCONSTRUCTION 200,
    RESCORE FACTOR 4
  );

Specifying a rescore factor for an individual approximate search:



SELECT item_id,
       VECTOR_DISTANCE(embedding, :query_vector) AS distance
FROM product_vectors
ORDER BY distance
FETCH APPROX FIRST 10 ROWS ONLY
WITH TARGET ACCURACY PARAMETERS (
  EFSEARCH       50,
  RESCORE FACTOR 4
);

Supported Vector Formats and Distance Metrics

Product-quantized HNSW indexes support the following vector formats:

Product-quantized HNSW indexes support the following distance metrics:

Updates, Checkpoints, and Index Repopulation

Updates to Vector Data

Vectors waiting to be inserted into a product-quantized HNSW index remain unquantized until Oracle AI Database adds them to the index. During a rescored search, Oracle AI Database considers candidates from both the HNSW index and vectors waiting to be inserted. Oracle AI Database merges the candidates, recalculates their distances using the original vectors, and returns the final top-K results. You do not need to quantize pending vectors manually.

Checkpoints

Oracle AI Database automatically manages the product quantization codebook and associates it with the checkpoint of the HNSW graph that the codebook was used to quantize. You do not need to save, restore, or validate the codebook separately.

Snapshots

When Oracle AI Database creates a snapshot of a product-quantized HNSW index, it quantizes the vectors during snapshot creation. The remaining snapshot lifecycle is unchanged.

Index Repopulation

Index repopulation creates a complete new HNSW index. Oracle AI Database can repopulate a product-quantized HNSW index in the following situations:

These transitions occur without requiring you to create the codebook or manually convert the intermediate index.

See Also