Conventional indexes cannot answer the question “what resembles this query”: similarity in text, images or audio comes down to nearby vectors in a space of hundreds or thousands of dimensions, and comparing the query in turn with every vector across millions of records takes far too long. A vector database solves both problems: it stores vectors alongside the regular fields of a record and returns nearest neighbours in milliseconds.
What’s inside
| Component | Purpose |
|---|---|
| Vector fields | a fixed-dimension array of numbers — 1,536 for popular text models, for example; stored together with the regular attributes of a record |
| Distance metrics | cosine similarity, dot product and Euclidean distance — the metric is chosen to match the embedding model |
| ANN indexes | approximate nearest neighbour search indexes (HNSW, IVFFlat) that find neighbours without a full scan |
| Hybrid search | a combination of vector search with keyword search (BM25, for example); regular record fields act as filters |
How nearest neighbours are found
Exact search computes the distance from the query to every stored vector: for a million vectors of 1,536 dimensions that is almost 1.5 billion operations per query. Databases therefore use approximate nearest neighbour search (ANN): it finds almost the same neighbours while examining only a small fraction of the records.
The main ANN index is HNSW, described by Malkov and Yashunin in 2016: a multi-layer proximity graph with few points on the upper layers and all the vectors on the bottom one. The search descends from the top layer and greedily moves towards the target, examining only tens to hundreds of candidates instead of millions — as a result, the cost grows logarithmically with the volume of data. Index parameters trade memory and speed for completeness: the search breadth ef_search in pgvector defaults to 40, and the higher it is, the higher the recall and the more expensive the query.
Where it is used
The classic scenario is semantic search: embeddings of document chunks are loaded into the database, the ones closest to the query are found, and they are either shown as results or handed to a language model as context. The same technique finds similar products, articles, music and faces, detects duplicates and clusters data.
CREATE EXTENSION vector; CREATE TABLE docs (id bigserial PRIMARY KEY, body text, emb vector(1536)); CREATE INDEX ON docs USING hnsw (emb vector_cosine_ops); -- five documents closest to the query vector SELECT id, body FROM docs ORDER BY emb <=> :query_vector LIMIT 5; Vectors are stored in different ways: dedicated DBMSs — Milvus, Qdrant, Weaviate — provide sharding, replication and hybrid search out of the box; pgvector adds the vector type, HNSW and IVFFlat indexes, L2 distance, dot product and cosine similarity to the familiar PostgreSQL; Meta’s FAISS library is only an index and search over datasets of any size, with no record storage or transactions.
Sources
the term is written from these pagesVerified 9 Nov