Vector Databases Explained: Indexes, Metadata and Retrieval
A vector database is a retrieval engine, not a magical memory for an LLM.
Key takeaways
- A vector database is a retrieval engine built around approximate nearest-neighbour search — not a magical memory layer for an LLM.
- Approximate indexes trade a small amount of recall for a massive gain in query speed; the trade-off is tunable, not fixed.
- Metadata filtering usually matters more than index choice for real-world relevance.
- Picking a vector database is an infrastructure decision first and a feature-list decision second: think about scale, ops burden and existing stack before comparing benchmarks.
What a vector database actually does
A vector database stores embeddings alongside their source content and metadata, and answers one question extremely fast at scale: given a query vector, which stored vectors are closest to it? At small scale this is a brute-force problem — compare the query against every stored vector and sort. At the scale most production systems actually reach, brute force becomes too slow, and that is the entire reason vector databases exist.
The mechanism that makes this fast is an approximate nearest-neighbour (ANN) index. Instead of guaranteeing the true top-k closest vectors, it returns a result that is very likely to be the true top-k, computed far faster than an exhaustive scan. That "approximate" is a deliberate, tunable trade-off — not a limitation to work around.
How ANN indexes actually work
Three index families cover almost every production vector database. Each makes a different trade between build time, query speed, memory and recall.
| Index type | How it works | Trade-off |
|---|---|---|
| HNSW | Navigable small-world graph; search hops along edges toward the nearest neighbours | Fast queries, high recall, but memory-hungry and slower to build/update |
| IVF (inverted file) | Vectors are clustered; a query only searches the nearest clusters | Lower memory than HNSW, needs a training step, recall depends on cluster count |
| PQ / scalar quantization | Vectors compressed into smaller codes before comparison | Big memory savings, some recall loss; often combined with IVF or HNSW |
Most production vector databases combine these — for example IVF for coarse filtering plus PQ for compression, or HNSW with quantized vectors to cut memory. The names on a feature list matter less than the recall and latency numbers you measure on your own data.
Metadata filtering matters more than people expect
A pure similarity search will happily return a semantically relevant chunk that is outdated, from a document the current user has no permission to see, or written in the wrong language. Metadata filtering — by date, source, tenant, permission level, or category — is what turns a similarity search into a usable retrieval system.
- Pre-filtering — narrow the candidate set before running the vector search. More accurate, but can be slower on databases where filtering isn't index-aware.
- Post-filtering — run the vector search first, then discard results that fail the filter. Faster in some engines, but risks returning fewer than k results if too many top matches get filtered out.
Watch out: post-filtering with a strict permission filter can silently starve your results — if 8 of your top 10 matches belong to a document the user can't see, they simply vanish instead of being backfilled. Check whether your database supports filter-aware ANN search before assuming post-filtering is safe.
Choosing a vector database
The honest answer is that most popular options perform similarly on raw ANN benchmarks. The decision is usually made by everything around the index, not the index itself.
| Question | Why it matters |
|---|---|
| Do you already run Postgres? | pgvector avoids a new system to operate, at the cost of raw ANN performance versus a purpose-built engine |
| How many vectors, realistically? | Under a few million, almost any option works; past that, index choice and sharding strategy start to matter |
| Managed or self-hosted? | Managed services remove ops burden but add cost and a network hop; self-hosted needs someone to own upgrades and scaling |
| Do you need hybrid search? | Combining keyword (BM25) and vector search often beats either alone for technical or exact-match-heavy content |
| Filter-aware ANN? | Determines whether metadata filtering happens safely alongside the vector search, not as a lossy afterthought |
If you're under a few million vectors and already run Postgres, start with pgvector. Migrate to a dedicated vector database only once you've measured a real latency or recall ceiling — not before.
Where this fits in retrieval-augmented generation
The vector database is the search engine inside a RAG pipeline: it takes a query embedding and returns the top-k candidate chunks, ideally already filtered by metadata. How those embeddings are produced and chunked determines what the database has to search through in the first place — a strong index cannot compensate for weak chunking upstream.
A vector database is a retrieval engine, not a magical memory for an LLM. It returns candidates; your pipeline still decides what to trust.
Common mistakes
- Treating recall as fixed — most ANN indexes expose tunable parameters (like HNSW's
ef_search) that trade latency for recall at query time. Default settings are rarely optimal for a specific workload. - No reindexing plan — changing the embedding model without rebuilding the index leaves old and new vectors in incomparable spaces.
- Ignoring cold-start latency — some indexes need warm caches or a build/training step; first-query latency after a deploy can look far worse than steady-state.
- Choosing by benchmark alone — a database that wins a public ANN benchmark may still be the wrong choice if it doesn't support the metadata filtering or hybrid search your product actually needs.
A vector database's job is narrow and mechanical: index vectors, search them fast, filter them correctly. Getting production-quality retrieval still depends on everything upstream — chunking, embedding choice and evaluation — and everything downstream, like reranking and prompt assembly.
Frequently asked questions
Do I need a dedicated vector database?
Not always. If you already run Postgres and have under a few million vectors, pgvector is often enough. Move to a dedicated engine once you measure a real latency or recall ceiling.
What does approximate nearest-neighbour search mean?
It means the index returns results that are very likely, but not mathematically guaranteed, to be the true closest matches — trading a small amount of recall for much faster queries at scale.
Why does metadata filtering matter for RAG?
Without it, a similarity search can return outdated or permission-restricted content just because it is semantically close to the query. Filtering by date, source or access level is what makes results trustworthy.