LLMs & GenAI

Vector Databases & ANN Search

Indexing and searching high dimensional embedding vectors in sub millisecond speeds using Approximate Nearest Neighbor algorithms.

🟡 intermediate5 min readragretrieval
Vector Databases store, index, and query high dimensional vector embeddings for RAG and semantic search systems. Traditional relational databases rely on exact B-tree comparisons that fail in high dimensions. Vector Databases use Approximate Nearest Neighbor (ANN) search algorithms (HNSW, IVF, PQ) to trade 100 percent exact recall for sub-millisecond search speed across millions of vectors.

What is a Vector Database?

Traditional relational databases (PostgreSQL, MySQL) index structured data using B-Trees or Hash tables to execute exact matches:

SELECT * FROM products WHERE price > 50 AND category = 'Electronics';

However, in AI applications, data is represented as High-Dimensional Dense Vector Embeddings (e.g. $768$-d or $1536$-d vectors generated by embedding models).

Finding the most similar vector requires measuring geometric distance (Cosine Distance or L2 Distance).

  EXACT SEARCH (Brute-Force k-NN):
  Compare Query Vector q against ALL 10 Million Vectors in Database!
  Complexity: O(N * d) ──► Takes 3.5 Seconds per query! (UNACCEPTABLE FOR PRODUCTION APIs!)

Vector Databases solve this using Approximate Nearest Neighbor (ANN) Search, returning top matches in under 5 milliseconds!

  APPROXIMATE NEAREST NEIGHBOR (ANN SEARCH):
  Trades 1% exact recall accuracy to gain 1000x FASTER SUB-MILLISECOND SEARCH SPEED!

Key Vector Distance Metrics

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. COSINE SIMILARITY     │ 2. EUCLIDEAN DISTANCE    │ 3. INNER PRODUCT (DOT)   │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Measures angle direction │ Measures straight-line   │ Measures directional     │
│ independent of magnitude.│ spatial distance.        │ alignment scaled by      │
│ Range: -1.0 to +1.0.     │ Best for un-normalized.  │ magnitude (Normalized!). │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

$$\text{Cosine Similarity}(u, v) = \frac{u \cdot v}{|u| |v|}$$

If vectors $u$ and $v$ are normalized to unit length ($|u| = 1$), Cosine Similarity is mathematically equal to the Dot Product ($u \cdot v$), simplifying matrix hardware search operations.

Core Vector Database Features

  1. Hybrid Metadata Filtering: Combining vector similarity search with structured SQL metadata filters in a single pass (e.g. "Find documents similar to query vector $q$, BUT ONLY where department == 'HR'").
  2. Persistence & HNSW Indexing: Storing millions of vectors on disk or RAM indexed via Hierarchical Navigable Small World (HNSW) graphs.
  3. Multi-Tenancy & Access Control: Restricting vector search namespaces by user ID or organization ID for enterprise security.

Production Vector Database Ecosystem

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. MANAGED CLOUD         │ 2. OPEN SOURCE / SELF    │ 3. DATABASE EXTENSIONS   │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Pinecone                 │ Qdrant                   │ pgvector (PostgreSQL)    │
│ Fully managed serverless.│ Rust-based, high performance | Simple SQL extension for │
│ Instant setup.           │ Milvus / Weaviate        │ existing Postgres DBs.   │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

Say this out loud

Vector Databases store and query high dimensional embedding vectors for semantic search and RAG. Traditional exact distance searches scale linearly O(N * d), taking seconds across millions of vectors. Vector Databases use Approximate Nearest Neighbor (ANN) search algorithms to trade 1 to 2 percent exact recall for sub-millisecond search speed.

Followups to expect

  1. When should you choose pgvector over a dedicated vector database like Qdrant or Pinecone? Choose pgvector when your dataset has less than 1 million vectors and already lives in PostgreSQL, avoiding the operational overhead of running a separate database service.
  2. What is Quantization in Vector Databases (Scalar / Product Quantization)? Compressing 32-bit float vector elements to 8-bit integers (Scalar Quantization) to fit 4x more vectors into GPU/CPU RAM while retaining 99 percent search recall accuracy.

Check yourself

Question 1 of 3

Why do traditional relational SQL databases fail when executing high dimensional vector similarity searches across 10 million embeddings?

More in LLMs & GenAI

See all →
Pretraining → SFT → RLHF5 minFine-Tune vs RAG vs Prompt: Choosing5 minRetrieval-Augmented Generation5 min