Vector Databasessearch at scale

Topic 50 of 90Module 6: RAG & Memory4 min read

Your Topic 48 exercise compared a query against 30 vectors by brute force — checking every single one. Fine at 30. At 10 million chunks × 1,536 dimensions, every query means ~15 billion multiplications, and your P99 latency dies. The fix is the same move databases made 50 years ago with B-trees: build an index so you don't look at everything.

For vectors, the indexes are ANN — approximate nearest neighbor: give up the guarantee of the exact top-k for a ~95–99% chance of it, and gain 100–1000× speed. (Recall the theme from speculative decoding and reranking-to-come: cheap-approximate wins when verified or when 98% is indistinguishable from 100% in product terms.)

HNSW — the algorithm you'll actually meet everywhere — in one analogy: navigating to a house via the road network. You don't check every house on Earth; you take the highway to roughly the right region, exit to city roads toward the right district, then walk the street to the door. HNSW builds exactly that: a multi-layer graph where the top layer has few nodes with long-range links (highways), each lower layer gets denser and more local (city roads → streets). A query enters at the top, greedily hops toward the target at each level, descends, and lands among the true nearest neighbors after touching a few hundred nodes instead of millions. Fast, high recall, the default index almost everywhere. (The other family, IVF, clusters the space into cells and searches only the nearest few cells — searching only the relevant library sections. Simpler, cheaper to build, common at huge scale.)

And quantization returns for its third appearance. 10M chunks × 1,536 floats × 4 bytes = ~61 GB of vectors — RAM is now your cost center. So vectors get quantized too: scalar (float32→int8, 4× smaller), product quantization, even binary (1 bit per dimension, 32× smaller, surprisingly usable when followed by exact rescoring of the shortlist). Topic 24's crayon boxes, drawing embeddings now.

What a vector database adds beyond the index — and this list is why "just use FAISS" (a library, not a database) stops sufficing: CRUD with persistence, metadata filtering (the crucial one: "nearest neighbors WHERE team='legal' AND date > 2025" — and note the subtlety that filtering during graph traversal is genuinely tricky, a real differentiator between products), hybrid search support (next topic), replication, and scaling.

The landscape, with the guidance that actually matters:

OptionIdentity
pgvectorPostgres extension — vectors as a column type, <=> as the distance operator
Chroma / LanceDBEmbedded, dev-friendly, local-first
Qdrant / Weaviate / MilvusDedicated vector DBs, serious scale
Pinecone / managed servicesZero-ops, pay-as-you-go
FAISSThe raw index library underneath many of the above

The guidance: below a few million vectors, pgvector in the Postgres you already run is the right answer — one system, real transactions, metadata filtering is just SQL WHERE. Since your stack is Supabase, you're one migration away from production vector search right now:

create table chunks (id bigserial primary key,
                     content text, metadata jsonb,
                     embedding vector(384));
create index on chunks using hnsw (embedding vector_cosine_ops);
 
select content from chunks
where metadata->>'team' = 'legal'
order by embedding <=> $query_vec limit 5;

Dedicated vector infrastructure earns its complexity at tens of millions of vectors, extreme QPS, or when its specialized features become load-bearing — not before.

Summary

Vector DBs make similarity search fast at scale via ANN indexes (HNSW's navigable layers, IVF's cells), compress vectors with quantization, and add the database-ness: filtering, persistence, hybrid support. Start with pgvector; graduate on evidence.

Mental model

A library organized by meaning-neighborhood instead of alphabet, with a highway-roads-streets navigation system so any book's nearest neighbors are a few hops away — plus a card catalog (metadata) for "only the legal section, only post-2025."

Mistakes to avoid

  • Standing up a dedicated vector cluster for 40K chunks. That's a résumé-driven architecture decision; pgvector handles it in the database you already operate.
  • Forgetting ANN is approximate when debugging. If ground-truth chunk #1 occasionally doesn't surface, check the index's recall settings (ef_search and friends) before blaming the embeddings.

Exercise

In Supabase (or local Postgres), enable pgvector and port your Topic 48 exercise into it: the table above, insert your chunks with embeddings, query with <=>, then add one metadata filter to a query. You've moved from a Python list to production-shaped infrastructure in ~30 minutes — and confirmed the whole stack is less exotic than it sounds.