Vector databases and indexes
A vector database stores embeddings and finds the nearest ones to a query fast. The trick is approximate search.
Words on this page (12)
- vector
- : An ordered list of numbers. Embeddings and model activations are vectors.
- vector database
- : A database that stores embeddings and finds the nearest ones quickly.
- embedding
- : A list of numbers that represents the meaning of a piece of text or an image, so similar things sit close together.
- ANN
- : Search that returns vectors that are almost always the closest ones, much faster than checking every vector.
- HNSW
- : A graph-based index that finds approximate nearest neighbors quickly by jumping through layers of connections.
- quantization
- : Storing weights with fewer bits (for example 4 instead of 16) to save memory at a small quality cost.
- reranker
- : A second model that rescores a short list of search results by reading query and passage together.
- metadata
- : Extra labels stored with a document or chunk, such as its title, date or country, which let you filter results.
- hybrid search
- : Combining keyword search and vector search.
- chunk
- : A small piece of a document, sized for indexing and retrieval.
- recall
- : Of the truly positive cases, the share the system found.
- benchmark
- : A fixed test with a score used to compare models.
The problem
Exact nearest-neighbor search compares the query with every . With a million vectors of 1,536 numbers that is over a billion operations per query. (ANN) indexes skip most of the comparisons and return results that are almost always the true nearest.
Index types
builds layers of graphs: start at a sparse top layer, jump toward the query, then refine in denser layers. It is fast and accurate and the common default, but uses memory. IVF clusters the vectors and searches only the few closest clusters. Product compresses vectors into short codes to save memory at some accuracy cost. Flat indexes do exact search and are fine up to roughly 100 thousand vectors.
Choosing one
Under a million vectors, almost anything works, so choose by what you already run. pgvector adds vector columns to Postgres. Chroma and LanceDB are simple embedded options. Qdrant, Weaviate and Milvus are dedicated open-source servers. Pinecone is a managed service. Supabase (Postgres with pgvector) and MongoDB Atlas add vector search to databases many teams already use. Jina and Cohere sell and models. Look for filtering, and how deletes and updates behave.
A worked example: why indexes exist
One million vectors of 1,536 numbers: comparing a query with all of them is about 1.5 billion multiplications per search. An HNSW index jumps through a graph of neighbors and checks maybe a few thousand vectors, finding the nearest almost every time. You trade a tiny chance of missing the very best match for a hundred-fold speedup.
How a graph search finds a neighbor
An HNSW index links each vector to a few nearby vectors, and keeps sparser copies of the graph on top. A search runs like this:
- Start at a fixed entry point in the top layer.
- Move to whichever linked vector is closest to the query, and repeat until no neighbor is closer.
- Drop to the next, denser layer and continue from that spot.
- At the bottom layer, keep a short list of the best candidates and return the closest ones.
It is like finding a street by first picking the right city, then the district, then the block. Implementations usually have a setting for how wide that final candidate list is. A wider list is slower and more likely to find the true nearest vector.
A second example: when you do not need one
Say you have 5,000 of 1,536 numbers. Comparing a query with all of them is 5,000 × 1,536 = 7,680,000 multiplications, which a laptop does in a blink. A plain array and a loop are enough, with no database to install. An index starts to pay off when scanning everything becomes the slow part, so measure before you add one.
What goes wrong
Filtering is the common trap. Some systems find the 10 nearest vectors first and apply the filter afterwards. Ask for 10 results in country = Canada, and if only 2 of the overall top 10 are Canadian, you get 2 results. The symptom is fewer results than you asked for. Confirm it by running the same query without the filter and counting how many of the top 10 pass it.
Other quiet problems: deleted documents that may still appear in some systems until the index is cleaned up, and an index too big for memory, which slows every query. Check how your database handles both before you depend on it.
Measuring the trade-off
Approximate search can miss the true best match. Measure : for 100 test queries, compare the index's top 10 with the exact top 10 from a plain scan. If 9.5 of 10 match on average, recall is 95 percent. Then ask whether a lost 5 percent matters for your product. For a help center it usually does not, because the 11th-best passage is rarely needed.
Common mistake
Choosing by charts rather than by what your team can operate. Operations cost more than a few milliseconds.
A worked example: sizing
50,000 chunks of 1,536 numbers each, at 4 bytes per number, is about 300 MB of vectors. That fits in memory on a laptop, so no special cluster is needed. At 50 million chunks the same math gives 300 GB, and now index choice, sharding and cost start to matter. Plan for the size you have, not the size you hope for.
Finding the closest book among ten is easy. Finding the closest among ten million, in a tenth of a second, needs an index. A vector database is the search system that keeps millions of embeddings organized so that nearest neighbors can be found quickly instead of by checking everything.
Check yourself
Why use approximate nearest neighbor search?
Not quite. ANN indexes skip most comparisons while almost always finding the true nearest.
Under a million vectors, how should you choose a database?
Not quite. Most options perform fine at that size, so operational simplicity wins.
You already run Postgres and have 80,000 chunks. What is a reasonable choice?
Not quite. One system to run is simpler, and the size is small.
You got 0 of 3 right.
Go deeper
- Repo
pgvector · Vector search inside Postgres.
- Repo
Qdrant · Open-source vector database with strong filtering.
- Repo
Chroma · Simple, embedded vector database for getting started.
- Repo
Meta · The reference library for similarity search indexes.
- Paper
Malkov and Yashunin · The HNSW paper.
- RepoLanceDBRepo
LanceDB · Embedded, serverless vector database built on a columnar format.
- Docs
Supabase · pgvector in a hosted Postgres.
- Docs
MongoDB · Vector search next to your documents.
- ToolFAISSTool
Meta · Library for efficient similarity search and clustering.
Spotted a mistake or something unclear? Report it on GitHub.