Uni-Sine
Computer ScienceDatabases

What is a vector database?

An ordinary database answers questions like "find the customer whose email is sam@example.com". A vector database answers questions like "find the five help articles most like this question", even when they share no words with it. Search "my laptop won't turn on" and it can find an article titled "Fixing a computer that has no power".

What is a vector embedding?

An embedding is a list of numbers produced by an AI model called an embedding model. OpenAI's documentation defines it simply: an embedding is a vector (list) of floating point numbers, and the distance between two vectors measures how related the two texts are. The model is trained so that things with similar meanings get similar numbers.

Real embeddings are long. OpenAI's text-embedding-3-small model gives 1,536 numbers per text by default, and text-embedding-3-large gives 3,072. Each number is a position along one direction, so an embedding is a point in a space with thousands of dimensions. No single number means anything on its own, like "is an animal". The meaning lives in the pattern.

In two dimensions you can draw it. Picture a toy model where similar words point in similar directions:

0123451234562°Ocatkittencar
A toy 2D example: "cat" and "kitten" are 9° apart, "cat" and "car" about 62° apart. Real embeddings have thousands of dimensions

How do you measure how similar two vectors are?

Vector databases use a distance or similarity measure. The three common ones are:

  • Cosine similarity: the cosine of the angle between two vectors. 1 means they point the same way, 0 means unrelated (at right angles), and −1 means opposite.
  • Dot product: multiply matching numbers and add. For vectors scaled to length 1, it equals cosine similarity.
  • Euclidean distance: the straight-line distance between the two points. Smaller means more similar.
\cos\theta=\frac{\mathbf{a}\cdot\mathbf{b}}{\left|\mathbf{a}\right|\left|\mathbf{b}\right|}Cosine similarity: the dot product divided by the two lengths

OpenAI recommends cosine similarity for its embeddings. Because they are already scaled to length 1, a plain dot product gives the same answer slightly faster, and cosine similarity and Euclidean distance rank results the same way. You can try dot products and angles between vectors in our Vectors Calculator.

Worked example: cosine similarity

Take three tiny 3-dimensional vectors: a = (1, 2, 2), b = (2, 1, 2) and c = (−2, 1, 0).

  1. Dot product a · b = 1×2 + 2×1 + 2×2 = 8. Lengths: |a| = √(1 + 4 + 4) = 3, and |b| = 3 too.
  2. Cosine similarity of a and b = 8 ÷ (3 × 3) = 0.89: very similar.
  3. Dot product a · c = 1×(−2) + 2×1 + 2×0 = 0, so their cosine similarity is 0: unrelated.

A vector database does exactly this, but with thousands of numbers per vector and millions of vectors.

How does a vector database work?

  1. Embed: each item, such as a paragraph, product description or image, is turned into a vector by an embedding model.
  2. Store: the database saves each vector with an ID and metadata, such as the title, date or the document it came from.
  3. Index: it builds a special index so it can find near neighbours quickly.
  4. Query: your question is turned into a vector by the same embedding model.
  5. Search: the database returns the stored vectors closest to the query vector, often filtered by metadata, such as "only documents from 2026".
  6. Use the results: show them as search results, or hand them to an AI model to answer from.

Why it needs special indexes

The simplest search compares the query with every stored vector. With a million vectors of 1,536 numbers each, that is over 1.5 billion multiplications per query. pgvector, for example, does this exact search by default, which gives perfect recall, but it gets slow as data grows.

So vector databases use approximate nearest neighbour (ANN) indexes. They find vectors that are very probably the closest, far faster, at the cost of sometimes missing one. pgvector's documentation puts it plainly: approximate indexes trade some recall for speed. Common types:

IndexHow it works
HNSW (Hierarchical Navigable Small World)Links each vector to its neighbours in a layered graph. A search starts on a sparse top layer and hops closer and closer to the target, like taking motorways then side streets
IVF (inverted file)Groups vectors into clusters, then only searches the few clusters nearest the query
Product quantisationCompresses each vector into a short code so millions fit in memory, at some cost in accuracy

HNSW was described by Malkov and Yashunin, and is now one of the most common choices. pgvector notes it gives a better speed and recall trade-off than IVFFlat, but builds more slowly and uses more memory. Meta's open-source Faiss library implements many of these methods and sits underneath a number of vector databases.

practice
Pick an answer, then check it.
Why do vector databases usually use approximate nearest neighbour search?

What is a vector database used for?

  • Semantic search: finding documents, products or help articles by meaning rather than exact words.
  • Retrieval-augmented generation (RAG): before an AI chatbot answers, it searches a vector database of your documents and adds the best passages to its prompt, so the answer is based on your data. The idea was set out in a 2020 paper and is now common in company chatbots.
  • Recommendations: "customers who liked this also liked" by finding items with nearby vectors.
  • Image and audio search: finding similar photos, or identifying a song from a recording.
  • Duplicate and fraud detection: spotting near-identical items, or behaviour unlike the usual pattern.

How documents get into a vector database

  1. Split each document into chunks, such as paragraphs or sections, often with a little overlap so no idea is cut in half at a boundary.
  2. Embed each chunk with the embedding model, giving one vector per chunk.
  3. Store each vector with its text and metadata: the source document, the section heading, the date and who may see it.
  4. Keep it fresh: when a document changes, re-embed its chunks and replace the old vectors, or the search will return out-of-date passages.

Chunk size is a trade-off. Small chunks match a question precisely but can lose context, and big chunks keep context but blur the meaning of the vector. Most teams test a few sizes on real questions and keep whichever retrieves the right passages most often.

Vector database vs relational database

Relational databaseVector database
StoresRows and columns of exact valuesVectors plus metadata
FindsExact matches and ranges: WHERE city = 'Leeds'Nearest neighbours: most similar items
Query languageSQLAn API call, or SQL with vector extensions
IndexB-tree and similarHNSW, IVF and other ANN indexes
Good forOrders, accounts, anything that must be exactSearch by meaning, AI retrieval, recommendations

They often work together. Many apps keep their main data in a relational database and add vectors either in a separate vector database or in the same database through an extension such as pgvector for PostgreSQL. Our guide to how relational databases handle indexing covers the B-tree side, and the Databases course covers SQL and NoSQL.

Examples of vector databases

  • Dedicated vector databases: Pinecone, Weaviate, Milvus, Qdrant and Chroma.
  • Extensions to existing databases: pgvector for PostgreSQL, and vector search features in databases and search engines such as Elasticsearch, OpenSearch, MongoDB and Redis.
  • Libraries: Faiss, for building vector search into your own software.

A dedicated vector database is worth it at very large scale or when vector search is the core of the product. For many projects, adding vectors to the database you already run is simpler.

How much space do vectors take?

Each number is usually stored as a 4-byte floating point value. One million vectors of 1,536 numbers take 1,000,000 × 1,536 × 4 = 6,144,000,000 bytes, about 6.1 GB, before the index and metadata. That is why compression such as product quantisation and smaller embedding sizes matter at scale.

practice
Pick an answer, then check it.
Two embeddings have a cosine similarity of 0.95. What does that suggest?

Common mistakes

  • Mixing embedding models. Vectors from different models live in different spaces and can't be compared. Embed the query with the same model as the stored data.
  • Thinking vector search replaces keyword search. It is weak at exact things like product codes and names. Many systems run both, called hybrid search.
  • Expecting ANN to be exact. Approximate indexes can occasionally miss the true closest match. Tune them for the recall you need.
  • Embedding whole long documents as one vector. Split them into sensible chunks, such as paragraphs, so each vector has a clear meaning.
  • Forgetting metadata filters. Search results often need limits like user, language or date, which the database applies alongside similarity.

Common questions

What is a vector database in simple terms?
A database that stores items as lists of numbers describing their meaning, and finds the items most similar to what you ask for, rather than exact matches.
What is the difference between a vector database and a regular database?
A regular relational database finds exact values with SQL and B-tree indexes. A vector database finds the nearest vectors by similarity, using indexes such as HNSW.
Why are vector databases important for AI?
They let AI systems look up relevant information quickly. In retrieval-augmented generation, a chatbot searches a vector database for passages related to your question, then answers using them.
Is PostgreSQL a vector database?
Not by itself, but the pgvector extension adds vector storage and similarity search, so many teams use PostgreSQL as their vector store.
What is an embedding model?
An AI model that turns text, images or audio into vectors so that similar things end up close together. The same model must be used for stored data and queries.
What is HNSW?
Hierarchical Navigable Small World, a graph-based index for approximate nearest neighbour search. It finds close vectors by hopping between neighbours across layers, very quickly.

Revision cards

Say the answer first, then flip the card to check it.

Where to go next

  • Solve it

    Vectors Calculator

    Visualise magnitude, direction and operations

    Open the tool
  • Learn it

    Databases

    SQL, NoSQL and everything in between for storing and querying data

    Start the course
Read next
Computer Science · DatabasesWhat is a database? Types, software and examplesWhat a database is, how tables, records and fields work, what database software (a DBMS) does, the main types of database and a first SQL query.