Vector Database Fundamentals
Why Regular Databases Can't Do This
Why Not Just Use PostgreSQL?
You can store vectors in a regular database column. PostgreSQL with pgvector does exactly this. But the moment you need to search millions of vectors for the nearest neighbors, you hit a wall: brute-force search is O(n).
Searching 1 million vectors with brute force takes ~100ms. At 100 million vectors, that's 10 seconds. At a billion, you're waiting minutes. Vector databases solve this with approximate nearest neighbor (ANN) algorithms that trade a tiny bit of accuracy for massive speed gains.
Exact vs Approximate Search
| Approach | Speed | Accuracy | When to Use |
|---|---|---|---|
| Brute force (flat) | O(n) — slow at scale | 100% recall | < 100K vectors, or when accuracy is critical |
| ANN (indexed) | O(log n) — fast | 95-99% recall | > 100K vectors, production search |
Recall measures what percentage of true nearest neighbors the algorithm finds. 95% recall means it finds 95 out of 100 true nearest neighbors — usually good enough.
How ANN Indexes Work
Three dominant indexing strategies:
HNSW (Hierarchical Navigable Small World)
The most popular index type. Builds a multi-layer graph where each vector connects to its nearest neighbors. Search starts at the top layer (coarse, few nodes) and drills down to the bottom layer (fine, all nodes).
How it works:
Trade-offs:
IVF (Inverted File Index)
Clusters vectors into buckets using k-means, then only searches relevant buckets at query time.
How it works:
Trade-offs:
Product Quantization (PQ)
Compresses vectors by splitting them into subvectors and quantizing each. Reduces memory 4-32x but loses some precision.
How it works:
Trade-offs:
The Build vs Query Trade-off
Every index has tuning parameters that trade build time and memory for query speed and recall:
| Parameter | Higher Value = | Lower Value = |
|---|---|---|
| HNSW M | Better recall, more memory | Faster build, less memory |
| HNSW efSearch | Better recall, slower query | Faster query, lower recall |
| IVF nprobe | Better recall, slower query | Faster query, lower recall |
| PQ segments | Better recall, more memory | More compression, lower recall |
Filtering: The Hidden Challenge
Real applications don't just search by similarity — they filter. "Find similar documents created in the last 30 days by this author in this department."
Vector databases handle filtering differently:
| Strategy | How | Trade-off |
|---|---|---|
| Pre-filter | Filter first, then ANN search on subset | Accurate filters, but small subsets break ANN assumptions |
| Post-filter | ANN search first, then filter results | Fast, but may return fewer results than requested |
| Integrated | Filter-aware ANN search | Best quality, but complex to implement |
This is why choosing the right vector database matters — filtering performance varies dramatically.
Storage Architecture
Vector databases must handle three types of data:
Storage layout choices:
Consistency and Durability
Unlike traditional databases, most vector databases prioritize availability over consistency:
If your application needs strong consistency (e.g., a vector must be searchable immediately after insert), your options are more limited — pgvector with PostgreSQL provides this.
Key Takeaways
This is chapter 3 of Vector Databases & Embeddings.
Get the full hands-on course — free during early access. Build the complete system. Your projects become your portfolio.
View course details