Link Search Menu Expand Document

Vector Databases - 101

Vector Database

LLM

Vector Databases

  • Specialized systems designed as a storage for managing and retrieving unstructured data to stores the Embedding data / high-dimensional vectors (which are mathematical representations of meaning) in vector data type or multi-dimensional space to perform high-efficient queries based on similarity.
  • Powerful to solving semantic queries, ask about similarity and relation.
  • This DB acts as memory to get the data for LLM Model.

Overview

  • drawing

  • drawing

  • drawing

  • drawing

Vector Space

  • Vector is a quantity, such as velocity, completely specified by a magnitude and a direction.
  • Like a plotting cities on a map, Embeddings work in a similar way. Instead of physical locations, each sentence occupies a position inside a mathematical space called vector space.
  • In this space:
    • Sentences with similar meanings are located close together.
    • Sentences with different meanings are farther apart.
  • drawing

  • drawing

Why are Vectors Used in a Vector Database?

  • Data shows up in the world or 80% or more of data is unstructured.
  • Efficient Representation of Complex Data
    • Dimensionality - representing data in high-dimensional space
    • Uniformity - data can be converted into a uniform format (numerical vectors)
  • Enabling Similarity Search
  • Leveraging Machine Learning Models
  • Optimizing Performance and Scalability
  • Improving User Experience
    • Real-time interaction (recommendations, search results or data analysis outputs)

Why not use a normal database:

  • Relational databases are optimized for exact matching, sorting, filtering, and transactional operations.
  • They are not designed to perform high-speed similarity searches across millions of high-dimensional vectors.
  • Vector databases use specialized indexing algorithms that make similarity search incredibly fast.

Traditional Database vs Vector Database

RMDBS

  • Structured data: predefined columns and rows
  • Schema-based: database structure must be defined before hand.
  • Data manipulation and querying: manipulation through SQL
  • ACID Compliant: Atomic, Consistency, Isolation, Durability
  • Indexing: to speed up data retrieval

  • How data search works in traditional databases
    • drawing
  • Limitation
    • Scalability: hard to deal with complex queries across large tables
    • Flexibility: changing DB’s schema can be disruptive
    • Handling Unstructured Data: not well-suited for handling unstructured data (images, text, audio, video)

Vector Database

  • Data Representation as Vectors: vector is vectorized which brings lots of benefits for searching
  • Similarity Search: finding data points closest to a given query vector.
  • Efficiency in High-Dimensional Searches: use of specialized indexing structures that are highly optimized
  • Handling Unstructured Data: vector database are made to deal with unstructured data
  • Schema-less Design: don’t require schema - allowing more flexibility in handling various data types and structures

Embedding

  • An embedding is a numerical representation of data. This data are numerical representations derived from machine learning models, encapsulating the semantic meaning of unstructured data.
  • Embeddings are vectors that encodes semantic similarities between the items they represent.
  • Instead of storing text as words, an AI model converts the text into a long list of numbers.
  • These numbers capture the meaning of the sentence.
  • This numerical representation is called an embedding vector. Two sentences with similar meanings produce vectors that are close together. Two unrelated sentences produce vectors that are farther apart. This is the key idea behind semantic search.

drawing

  • Text with similar content and meaning will have similar vectors

drawing

Embedding Flow

  • drawing

Embedding Dimensions

Model Dimensions
text-embedding-3-small 1536 dim
text-embedding-3-large 3072 dim
Gemini embedding 768 dim
BGE-small 384 dim
Dimensions Storage Speed Nuance
384 Low ↓ Fast ↑ Good
768 Med Med Better
1536 High ↑ Slow ↓ Best

Traditional Search vs Semantic Search

  • traditional search understands words, but it doesn’t understand their meaning.
  • It simply compares text.
  • This approach works well for exact keyword matching but struggles when different words have similar meanings.
  • Semantic search works differently from compare text approach. Instead of matching words, it tries to understand the meaning behind the user’s query.
  • Semantic search recognizes that: car, automobile, vehicle are closely related. Similarly, maintain, service, repair also have similar meanings.
  • Because semantic search understands meaning, it returns much more relevant results. This is exactly why modern AI search systems feel much smarter than traditional keyword search.

Techical

  • Stores
    • Vector types
    • Metadata
    • Original content
  • Schemas
    • Dimension
    • columns
    • Tables
    • Keys (e.g. primary keys)
  • Supports
    • Fast similarity search
    • Filtering
    • Scalable retrieval
    • Insert, delete and update process
    • Relationships (e.g. graph)
    • Monitoring and logging
    • Authentication and Authorization
    • Security, Data Encryption and Privacy Measures
    • Auditing and Compliance Requirements
    • Disaster Recovery and Backup Strategies
  • Indexing
    • Inverted Indexes
    • K-d Trees
    • Priority Queues
    • Local Sensitive Hashing (LSH)
      • Similar vectors have higher chances of sharing similar hash codes.
      • Enables efficient nearst-neighbor searches.
      • Uses multiple hash functions for compact data representation.
    • Hierarchical Navigable Small World (HNSW)
      • Organize vectors into difference layers with varying probabilities into a hierarchical graph structure.
    • Approximate Nearest Neighbor Oh Yeah (ANNOY)
      • Organize high-dimensional data using binary tree.
  • Measure similarity with distance function
    • Cosine similarity
    • Euclidean distance
    • Dot product
    • Scoring hybrid system
    • vector_score * 0.7 + keyword_score * 0.3
  • Optimizing
    • Partitioning
    • Caching
    • Scaling
      • Horizontal scaling: adding more nodes/servers.
        • Sharding
        • Replication
      • vertical scaling: upgrading hardware.
    • Load balancing
    • Asynchronous processing
    • Indexing and query optimization

Measuring Similarity

  • Once text has been converted into vectors, comparing two pieces of text becomes a mathematical problem.
  • Instead of comparing words, we compare vectors.
  • One of the most common techniques is called Cosine Similarity. Cosine Similarity measures how similar two vectors are. If two vectors point in nearly the same direction, they are considered highly similar. If they point in different directions, they are less similar.

Cost of Vector DB

  • Large storage for storing vectors
  • RAM heavy
  • Indexing is complex

Core Architecture of a Vector Database

  • Ingestion Layer - Consume the data
    • Raw data
    • Vectors
    • Metadata
  • Indexing Layer - Build Appoximate Nearest Neighbor (ANN) indexes. Using Graph and clustering to indexing.
    • HNSW
    • IVF
    • PQ
  • Storage Layer
    • Vectors
    • Metadata
    • IDs
  • Query Engine
    • A vector
    • Filters
    • Top K
    • return most similar items

ELI5: Sparse Vector vs Dense Vector

  • e.g. we have a huge sentence with 100000 words
  • Sparse Vector
    • We create a list with 100,000 slots.

        Slot #523 = "love" → 1
        Slot #1829 = "pizza" → 1
        Slot #7321 = "I" → 1
        Everything else = 0
      
    • Result: [0,0,0,0,1,0,0,0,0,0,…,1,…,1,…]
    • Most values are zero. That’s why it’s called sparse (mostly empty) and only exact overlapping terms contribute.
    • Sparse vectors are usually generated by algorithms like:
      • TF-IDF
      • BM25
  • Dense Vector
    • Instead of storing every word position, an AI model converts the sentence into something like:
      [0.24, -0.87, 0.56, 0.13, ...]
    
    • Maybe only 768 numbers long. These numbers don’t correspond to specific words. Instead they capture the meaning of the sentence.
    • Sentence with similar sentence will produce vectors that are close together. That’s why it’s called dense (almost every dimension contains information).
Feature Sparse Vector Dense Vector
Representation Mostly zeros Mostly non-zero
Dimensions Very high (100k+) Lower (384–4096)
Captures meaning No/limited Yes
Exact keyword matching Excellent Moderate
Synonym handling Poor Excellent
Explainability High Low
Storage efficiency Sparse compression Fixed-size vectors
Best for Search engines, IDs, codes Semantic search, RAG
Typical algorithm BM25, TF-IDF, SPLADE Embeddings from transformers
Modern usage Hybrid search Hybrid search

BM25 or Best Match 25

  • Technic to search by frequency methods.
  • BM25 scores a document against a query by looking at it from multiple directions. Its main components are:
    • TF term frequency component: asks how often the term appears in this specific document. BM25 applies a saturation function rather than just using the raw frequency. Because of this saturation function the score grows rapidly in the starting and then flattens.
    • IDF component: how difficult it is to find a term anywhere in the corpus. IDF gives rare terms more weightage.
    • The length normalisation component penalises longer documents. A longer document naturally contains more term occurrences.
  • It is a bag-of-words model and word order and semantics do not matter to it.

Vector DB Usage

  • Semantic Search
  • Recommendation engines
  • AI agents with memory
  • Document QA
  • Similarity matching
  • Fraud detection
  • Image and audio Retrieval & Similarity search
  • Enterprise Knowledge Bases
  • Bioinformatics

Vector DB tools

  • Dedicated DB Examples:
    • ChromaDB
    • LanceDB
    • Milvus
    • Weaviate
    • Pinecone
  • DB Support vector search:
    • PostgreSQL (pgvector)
    • Cassandra
    • ClickHouse
    • OpenSearch
    • elasticsearch
    • Redis
DATABASE BEST FOR PRICING SELF-HOST Tier
pgvector PostgreSQL Users, Hybrid Search, Simple Use Cases Free (Open Source) or Cloud Enterpise Standard
Pinecone Managed Service, Scalability, Ease of Use, serverless Tiered (Usage-based) / Free Tier Managed Vector DB
Chroma Local Development, Python Ecosystem, Rapid Prototyping Free (Open Source) / Cloud (Soon) Local dev Vector DB
Qdrant High Performance, Rust-based, Filtering & Search Free (Open Source) / Cloud Managed Rising star
Weaviate GraphQL, Modules (ML models), Production-ready Free (Open Source) / Cloud Managed Rising star
Milvus Large-scale Deployments, High Throughput, Feature-rich Free (Open Source) / Cloud Managed Enterpise level

Vector DB Decision Framework

  flowchart TD;
      A[Prototyping?]-->B[Have Postgres?];
      A[Prototyping?]-->D[Chroma DB];
      B[Have Postgres?]-->C[Want managed?];
      B[Have Postgres?]-->E[pgvector];
      C[Want managed?]-->F[Pinecone];
      C[Want managed?]-->G[Qdrant];

Answer these questions

  • What’s your scale? user activity, data storage.
  • What’s your ops capacity? who is managing the operation?
  • What’s your budget priority?
  • Need data integration? is it standalone, or will join with other data or need ACID transaction?
  • Lock-in tolerance?

Analogy

Context vector geometry
data basic unstructured data unstructured data
data type storing a vector data storing a geometry data
how it store (in python) commonly store as list in python commonly store as geom or list in python
how data visualized [0.12, 0.86, 0.32, 0.1, 0.3 …, 0.23] POLYGON ((8.45950 13.82035, 8.47341 13.83227))
Objects type dense and sparse point, polygon, and multipolygon
what element that data hold numerical data in float data type numerical data in float data type
meaning store position of text in matematical space store position of thing in space
how it store in DB - Combine with transactional data using vector extension
- Store in single file at folder in local
- Dedicated database
- combine with transactional data using pg_geometry extension
- Store in single folder as fgdb
- Dedicated database