Vector Database
A vector database indexes high-dimensional embeddings of text, images, or other unstructured data using Approximate Nearest Neighbor (ANN) structures such as HNSW or IVF so semantic queries execute in milliseconds. At query time, the input is embedded into the same vector space and compared against stored entries via dot product, cosine similarity, or L2 distance to retrieve top-k context for RAG and search.


Vector databases enable fast semantic similarity search over unstructured data and serve as the retrieval backbone of Retrieval-Augmented Generation (RAG).
Step-by-Step Walkthrough (From Prof. Tom Yeh’s Walkthrough)
[1] Given
- A toy dataset of three sentences, each containing words (tokens).
- In production, a corpus can hold millions or billions of chunks, and encoder/LLM context windows range from hundreds of tokens up to tens of thousands (e.g., tokens in models like Mistral-7B v0.3, or in newer models).
Indexing sentence 1: "how are you"
[2] Token / Word Embeddings
- Look up each word’s embedding vector from an embedding table of rows (vocabulary size ).
- In practice, subword vocabularies typically contain – tokens, with embedding dimensions commonly in the hundreds or thousands (e.g., , , , or ).
[3] Sequence Encoding
- Pass the sequence of token vectors through an encoder to produce one contextualized feature vector per token.
- In the toy example, the encoder is a single-layer perceptron (
Linear + ReLU). In practice, it is a Transformer encoder (e.g., BERT/RoBERTa bi-encoders) or embedding-tuned LLM.
[4] Mean Pooling
- Aggregate the token feature vectors into a single fixed-size sentence/text embedding by averaging across the token positions (shown as averaging across columns in the diagram’s column-per-token layout).
- Pooling alternatives: Bidirectional encoders sometimes extract the
[CLS]token representation, while decoder-only embedding models often use last-token pooling, though mean pooling remains one of the strongest general-purpose defaults.
[5] Indexing & Compression
- Reduce the sentence embedding from dimensions to using a projection matrix ( dimensionality reduction).
- In practice, random projections (via the Johnson-Lindenstrauss lemma / Locality-Sensitive Hashing), learned Matryoshka truncation, or vector quantization (e.g., Product Quantization / Scalar Quantization) shrink memory footprint and speed up distance computations.
- Store the resulting index vectors in the vector index.
[6–7] Index Remaining Corpus Documents
- Repeat steps [2]–[5] for
"who are you"and"who am I"so all three vectors are stored in the database.
[8] Query Encoding: "am I you"
- Run the exact same pipeline [2]–[5] on the incoming query to obtain a -D query vector in the same embedding space.
[9] Similarity Scoring (Dot Products)
- Compute the dot product between the query vector and every indexed database vector; transposing the query vector turns all comparisons into a single matrix multiplication.
- (Note: when embeddings are -normalized to unit length, the dot product equals cosine similarity.)
[10] Nearest Neighbor Retrieval
- In this 3-document example, a simple linear scan finds
"who am I"as the highest-scoring nearest neighbor. - In production over millions or billions of vectors, exact linear scans are too slow, so vector databases use Approximate Nearest Neighbor (ANN) algorithms such as Hierarchical Navigable Small World (HNSW) graphs or Inverted File (IVF) indexes for sub-linear retrieval latency.