Chapter 5 of 8 · Free, no signup

Support Us

Generative AI & LLM Engineering · Chapter 5 · ~18 min · Hands-on Visuals

Vector Databases & Similarity Search

In Chapter 4 we saw how text becomes vectors. But when your app has 10 million documents, how do you find the 3 most relevant paragraphs in under 10 milliseconds? Enter the vector database.

  • Why SQL B-tree indexes fail for vectors
  • See HNSW highway graphs navigate high dimensions
  • Master Approximate Nearest Neighbors (ANN)
  • Combine semantic search with metadata security filters

1The High-Dimensional Scaling Problem

In a normal relational database, indexing numbers is easy. If you search WHERE age > 25, a B-tree index sorts numbers along a 1D line and finds your row in microseconds.

Vector embeddings have 1,536 dimensions. There is no such thing as "greater than" or alphabetical order in 1,536 dimensions! To find the closest match with standard SQL, you would have to calculate cosine distance against every single row in the database.

Exact Search (kNN) vs Approximate Search (ANN)

Why traditional SQL table scans choke on millions of 1,536-dimensional vectors.

Database Size:500,000 Vectors (1,536-dim each)
50,000 (Small App)1,000,000 (Enterprise Docs)5,000,000 (Large Knowledge Base)

Illustrative simulation only: displayed operation counts and latency are estimates, not measurements from a vector database. Real results depend on the data, index, hardware, and search settings.

🐢 Brute-Force kNN (Full Scan)O(N) Complexity

Calculates cosine distance against every single vector in the database one by one.

Comparisons:500,000
Query Latency:Run estimate
Accuracy:100% exact
⚡ HNSW Vector Index (ANN)Approximate search

Navigates a multi-layer graph and may examine fewer vectors than a full scan. Recall and work vary with the index, dataset, and search settings.

Comparisons:< 350 (simulated)
Query Latency:Run estimate
Accuracy (Recall):98–99% (Practically identical)
Testing against every row is called Exact kNN (k-Nearest Neighbors). At 1 million vectors, doing 1.5 billion floating-point calculations per query crashes server throughput.

Why can't traditional SQL B-tree indexes speed up vector similarity search?

2How ANN Works: HNSW Highways

To make search faster at scale, vector databases can use approximate search, trading some recall for lower query cost. The speed and accuracy tradeoff depends on the index, data, and configuration. This is called Approximate Nearest Neighbors (ANN).

The most famous algorithm is HNSW (Hierarchical Navigable Small World). It organizes vectors into multi-layer graphs like an express highway system:

How HNSW Works: The Skip-List Highway

Hierarchical Navigable Small World (HNSW) is the gold standard indexing algorithm behind Pinecone, Qdrant, and pgvector.

Layer 2 (Express Highway)Active Search Stage
A
C
Layer 1 (Regional Roads)Waiting
B
C
F
G
Layer 0 (Local Neighborhood — Top Results)Waiting
#1
#2
#3
Hop Step 1 of 3

Layer 2: Express Highway

Few sparse nodes with long-distance links. The query enters at the top entry point and makes giant leaps across meaning space.

Current Traversal:
Entry Node A → Express hop to C

Just like driving across the country: you take the interstate highway across states, exit onto arterial roads, then take local streets to the specific house address.

HNSW explores a limited set of graph links rather than comparing every vector. Its search is often described as roughly logarithmic in practice, but that is not a strict complexity guarantee; performance and recall depend on the graph and search settings.

What does the top layer (Layer 2) of an HNSW graph do?

3Metadata Filtering: Role & Tenant Security

In production apps, semantic similarity alone is dangerous. An intern asking "What are our company plans for next quarter?"should never retrieve confidential Board of Directors payroll documents, even if they are a 99% vector match!

We attach metadata payloads (JSON attributes like tenant_id, department, role, date) to each vector:

Metadata Filtering: The Security Boundary

A vector database must combine semantic similarity with hard tenant, department, and role permissions.

Department:
Clearance:
SQL Filter: 1=1 AND 1=1Matching documents: 7 of 7
#12026 Executive Compensation & Bonus Model
HRConfidential94%
#2API Gateway Architecture & Rate Limiting Guidelines
EngineeringInternal91%
#3Employee Travel & Expense Reimbursement Policy
FinanceInternal88%
#4Open Source Code Contribution & Licensing Rules
EngineeringPublic85%
#5Q3 Board of Directors Financial Risk Assessment
FinanceConfidential83%
#6Company Holiday Calendar & Sick Leave Guidelines
HRPublic81%
#7Master Service Agreement (MSA) Legal Template
LegalInternal78%
Single-stage pre-filtering vs Post-filtering: Early vector tools did post-filtering (retrieve top 100 vectors, then delete non-permitted rows). If all 100 were confidential, the user saw zero results! Modern vector databases use pre-filtered HNSW traversal so only allowed nodes are visited.

Why is 'single-stage pre-filtering' better than 'post-filtering' in vector search?

4Choosing your vector database

The vector landscape exploded with options. Which one should you actually pick for your project?

Which Vector Database Should You Choose?

Select your project stage and architecture constraints to see the recommended approach.

pgvector (PostgreSQL)

Best for 80% of startups & existing stacks

Advantages

  • Zero new infrastructure if you already run Postgres
  • ACID transactions, relational joins, and vectors in one database
  • Available on most managed PostgreSQL hosts

Trade-offs

  • Needs index tuning (HNSW / IVFFlat) at scale > 2M vectors
💡 Engineering take: If you already have PostgreSQL, start here. Adding a dedicated vector DB before 1M vectors is usually premature optimization.
Already using PostgreSQL? Most managed PostgreSQL hosts can enable pgvector with one command. You can write normal SQL queries with the <=> cosine distance operator without leaving your database!

You are building a startup app that already runs on PostgreSQL with 200,000 document chunks. What is the most practical choice?

5Distance metrics: Cosine vs Dot Product vs L2

Cosine Distance

Measures the angle between vectors (0 = identical, 1 = orthogonal). Best default for text and documents where length varies.

Dot Product

Fastest mathematical calculation. When vectors are normalized to unit length (length = 1.0), Dot Product is identical to Cosine!

Euclidean (L2)

Measures straight-line ruler distance between coordinates. Commonly used in computer vision, image classification, and facial recognition.

OpenAI's text-embedding-3 and modern embedding models return vectors that are already normalized. That means you can use Dot Product for maximum search speed with exact cosine accuracy!

6Chapter challenge

Vector Databases & ANN Mastery

Answer all four questions correctly to complete Chapter 5.

0 / 4 correct
  1. 1What does 'ANN' stand for in vector search?

  2. 2How does the HNSW algorithm achieve sub-10ms search over millions of vectors?

  3. 3In pgvector (PostgreSQL), which operator calculates Cosine Distance between two vectors?

  4. 4Why should multi-tenant SaaS apps always use metadata filtering during vector retrieval?

7Recap

  • Brute Force vs ANN: A full scan compares against every vector (O(N)). ANN indexes can reduce query work substantially; actual speed and recall depend on the index, dataset, and settings.
  • HNSW Graph: A multi-layer graph that can navigate toward nearby vectors with fewer comparisons; speed and recall depend on index and search settings.
  • Metadata Filtering: Combine semantic vector similarity with strict SQL-like filters for tenant and role-based security.
  • pgvector vs Dedicated: If you already run PostgreSQL, start with pgvector. Turn to Pinecone or Qdrant when you need dedicated serverless scale.

Solve the challenge above to complete the chapter, or

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us