When a vector database supports multiple distance metrics, the underlying indexes must be optimized differently to align with the mathematical properties and computational demands of each metric. Here's how storage and optimization strategies might vary:
1. Data Structure and Preprocessing: Indexes optimized for L2 (Euclidean) distance often rely on spatial partitioning structures like KD-trees, VP-trees, or hierarchical navigable small-world (HNSW) graphs, which prioritize grouping vectors by geometric proximity. These structures assume that vectors closer in space are more similar. In contrast, inner product (IP) or cosine similarity metrics focus on angular relationships between vectors. For IP, vectors may be normalized during preprocessing to convert the problem into a cosine similarity calculation, allowing the use of angular partitioning methods like ball trees or polar coordinate-based indexing. For example, a database might store normalized vectors for IP-optimized indexes but retain raw magnitudes for L2-optimized ones.
2. Quantization and Compression: Vector databases often compress data using techniques like product quantization (PQ) to reduce memory usage. For L2, PQ codebooks are trained to minimize reconstruction error in Euclidean space. For IP, codebooks might instead optimize for preserving directional similarity, such as by maximizing the alignment of quantized vectors with their original directions. Additionally, L2-optimized indexes might use scalar quantization (e.g., 8-bit integers) to store vector components, while IP-optimized indexes could prioritize preserving sign information (positive/negative values) critical for dot product accuracy.
3. Search Algorithms and Pruning Rules: During search, L2-optimized indexes leverage pruning strategies based on bounding hyper-spheres (e.g., in IVF indexes) or triangular inequality optimizations. For IP, pruning might involve maintaining upper bounds of dot products between clusters and query vectors. For instance, HNSW graphs optimized for IP would prioritize edges between vectors with high directional similarity, whereas L2-focused HNSW would prioritize spatial neighbors. Databases like FAISS allow users to specify the distance metric during index construction, which directly influences how cluster centroids are computed (e.g., k-means for L2 vs. spherical k-means for cosine/IP).
In practice, databases like Milvus or Pinecone may create separate index instances for each metric or use adaptive structures like HNSW that dynamically adjust traversal logic based on the metric. This ensures efficient nearest-neighbor search without compromising accuracy, but requires additional memory and compute overhead to maintain multiple optimization paths.