Vector Databases and Similarity Search Algorithms
Learn similarity search metrics and vector database indexing algorithms like HNSW and IVF.
Vector Database Fundamentals
Traditional databases index records in sorted B-Trees. Vector databases (e.g. Pinecone, Chroma, Milvus, pgvector) store high-dimensional embeddings and query them using Approximate Nearest Neighbor (ANN) search.
Common Vector Distance Metrics:
- Cosine Similarity: Measures the angle between vectors, ignoring magnitude. Ranges from -1 to 1. Best for text semantic similarity.
- Dot Product: Measures angle and magnitude. Extremely fast if vectors are normalized.
- Euclidean Distance (L2): Measures straight-line distance in space.
ANN Search Indexing Algorithms
Performing brute-force searches across millions of vectors is too slow. Vector databases use approximate indexing:
• IVF (Inverted File Index): Groups vectors into clusters using k-means. Searches are limited to the closest cluster centroids, speeding up queries at the cost of slight recall loss. • HNSW (Hierarchical Navigable Small World): Builds a multi-layered graph where the top layers have wide connections (for fast routing) and the bottom layers have dense local connections (for exact neighbors). HNSW is the gold standard for low-latency queries, though it has high RAM consumption.