Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
What a vector database is: storing embeddings, approximate nearest neighbour search with HNSW and IVF, filtering, and when pgvector is all you need.
Quick answer: A vector database stores embeddings, the lists of numbers that represent the meaning of text, images and other content, and finds the stored vectors most similar to a query vector. Ordinary database indexes are built for exact matches and ranges, not for "closest in meaning" across hundreds of dimensions. Comparing a query with every stored vector is too slow at scale, so vector databases use approximate nearest neighbour (ANN) indexes such as HNSW, which find almost all of the closest matches in milliseconds. AI applications use them for semantic search, recommendations, and retrieving context for language models.
Also Read: How Tokenization Works and Why It Affects AI - How To's
AI models turn content into embeddings: vectors with hundreds or thousands of numbers, arranged so that similar content has similar vectors. See how embeddings work.
The core operation is nearest neighbour search: given a query vector, find the k stored vectors closest to it.
The straightforward way is to compare the query against every vector. For ten thousand vectors that is instant. For a hundred million vectors of 1,500 numbers each, every query would mean hundreds of billions of arithmetic operations.
Traditional indexes do not help. A B-tree sorts values along one dimension. In hundreds of dimensions there is no useful sort order, and tree-based spatial indexes break down too, a problem known as the curse of dimensionality.
Exact nearest neighbour search in high dimensions is slow. But applications rarely need the mathematically exact top 10. Getting 95 to 99% of them, a hundred times faster, is an excellent trade.
That is approximate nearest neighbour (ANN) search. The measure of quality is recall: the fraction of the true nearest neighbours that the search returned.
Every ANN index lets you tune a three-way trade-off between recall, speed and memory.
Hierarchical Navigable Small World graphs, introduced in this 2016 paper, are the most widely used index.
It works like finding an address: take the motorway to the right city, main roads to the right district, then side streets to the door. Search time grows roughly with the logarithm of the collection size.
Trade-offs: very fast with high recall, but memory-hungry, and building the index takes time.
Also Read: Transformers Explained: The Architecture Behind AI
An inverted file index groups vectors into clusters, using an algorithm such as k-means. Each cluster has a centre point.
Trade-offs: uses less memory than HNSW and is quick to build. Recall depends on how many clusters you probe, and it needs a training step.
Product quantisation and related techniques compress each vector into a short code, shrinking memory use many times over at some cost in accuracy. Systems often search the compressed vectors first, then re-score the best candidates with the full vectors.
| Index | Speed | Recall | Memory | Notes |
|---|---|---|---|---|
| Flat (brute force) | Slow at scale | Exact | High | Fine for small collections |
| HNSW | Very fast | High | High | The common default |
| IVF | Fast | Good, tunable | Medium | Needs training on the data |
| IVF + quantisation | Fast | Lower | Low | For very large collections |
| Disk-based graphs | Fast | High | Low RAM | Keep most data on SSD |
An index alone is a library. A database wraps it with the features applications need:
| Category | Examples | When it fits |
|---|---|---|
| Purpose-built vector databases | Pinecone, Weaviate, Milvus, Qdrant, Chroma | Large collections, heavy query load, advanced filtering |
| Extensions to databases you already use | pgvector for PostgreSQL; vector search in Elasticsearch, OpenSearch, Redis, MongoDB | Your data already lives there; moderate scale |
| Libraries | FAISS, hnswlib, ScaNN | Embedding search directly in an application or pipeline |
For many projects, the simplest good answer is pgvector. It adds a vector column type and HNSW and IVF indexes to PostgreSQL, so your embeddings sit next to your ordinary tables. You get joins, transactions and filters with plain SQL, and one less system to run:
CREATE TABLE docs (id bigserial PRIMARY KEY, body text, embedding vector(1536));
CREATE INDEX ON docs USING hnsw (embedding vector_cosine_ops);
SELECT id, body
FROM docs
ORDER BY embedding <=> $1 -- cosine distance to the query vector
LIMIT 5;
A dedicated vector database earns its place when you have many millions of vectors, high query volume, or filtering and tenancy requirements the simpler option cannot meet.
Not always.
Adopt a specialised system when measurements show you need it.
A database that stores embeddings and quickly finds the ones most similar to a given query, enabling search by meaning instead of exact words.
A family of algorithms that find most of the closest vectors much faster than checking every one, accepting a small loss of accuracy.
A graph-based index in which vectors are linked to their neighbours across several layers, allowing a search to hop quickly towards the query's nearest matches.
No. The embedding model creates the vectors. The vector database stores and searches them.
Vector databases exist because "find the most similar" is a different problem from "find the exact match", and hundreds of dimensions defeat ordinary indexes. Approximate indexes such as HNSW make similarity search fast, and the surrounding database features make it usable. Start with the simplest option that works, often an extension to the database you already have.