Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

What Is a Vector Database?

Understand what a vector database stores, how it differs from SQL and keyword search engines, and build a minimal in-memory vector store with NumPy.

A vector database stores vectors, lists of numbers such as [0.12, -0.80, 0.33, ...], together with the data they describe, and answers one question very well: "which stored vectors are closest to this one?"

Where does a vector come from? A machine learning model, called an embedding model, turns a piece of content (a sentence, a product photo, a song) into a vector so that similar content gets nearby vectors. Searching by closeness then means searching by meaning. You will build embeddings in the next chapter; here we focus on what the store itself does.

What a record looks like

A record in a vector database usually has three parts:

Part Example Purpose
id "doc-42#chunk-3" Identify and update the record
vector 768 floats What similarity search compares
payload / metadata {"source": "faq.md", "lang": "en"} Filters, citations, the original text

The core operations are upsert (insert or replace by id), delete, query by vector (top-k nearest neighbours, optionally with a metadata filter) and get by id.

How it differs from other databases

Relational (SQL) Keyword search (inverted index) Vector database
Matches on Exact values and ranges Exact words (and stems) Closeness in vector space
Typical query WHERE price < 20 "refund policy" "something like this"
Handles synonyms No Only with a thesaurus Yes, via the embedding
Result Exact rows Ranked by word statistics Ranked by similarity

They are not rivals. Real systems combine them: filter by lang = 'en' (relational), match product codes exactly (keyword), find conceptually similar text (vector).

Where vector search is used

  • RAG: find passages to give an LLM.
  • Semantic search: search by meaning, not exact words.
  • Recommendations: "users who liked this also liked..."
  • Deduplication: find near-duplicate documents or images.
  • Image, audio and code search: embed non-text content.
  • Anomaly detection: points far from every neighbour.

What it stores

ID A unique key per record.
Vector The embedding to compare.
Payload Text and metadata for filters.
1 / 2

A minimal vector store

The simplest possible vector database is a matrix plus a list of ids. Search is a matrix-vector product. We normalise vectors to length 1, so the dot product equals cosine similarity (more on that in similarity metrics).

Python A 40-line vector store

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note two things. The query for "mostly fruit" returns apple and banana first. And the filtered query only considers kind == "vehicle" records, so it returns the truck even though it is a poor match. Filtering rules decide which records compete; similarity ranks the survivors.

Why not just use NumPy forever?

This store compares the query against every vector (exact or brute-force search). That is perfect for thousands of vectors. At tens of millions it becomes too slow, and you need:

  • Approximate nearest neighbour (ANN) indexes that skip most comparisons (chapter 3).
  • Persistence: survive restarts, replicate, back up.
  • Concurrent writes and reads, access control, monitoring.
  • Filtering that is fast even when it removes 99% of the records.
Python How the cost grows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The time grows roughly linearly with the number of vectors. One query is fine; thousands of users per second are not. That is the problem vector databases are built to solve.

Kinds of products

  • Purpose-built vector databases: Qdrant, Milvus, Weaviate, Pinecone, Chroma.
  • Extensions to existing databases: pgvector for PostgreSQL, vector search in MongoDB, Redis, Elasticsearch/OpenSearch, SQLite extensions.
  • Libraries (an index, not a full database): FAISS, hnswlib, ScaNN, Annoy.

We compare them in the landscape lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a vector database primarily answer?

    Nearest-neighbour search over vectors is the defining operation, usually combined with metadata filtering.
  2. 2. What is an upsert?

    Upsert avoids duplicates and makes re-ingesting a changed document simple.
  3. 3. Why is brute-force search a problem at tens of millions of vectors?

    Exact search is accurate but linear in the data size; ANN indexes trade a little accuracy for large speedups.
  4. 4. In a typical record, what is the payload used for?

    The payload carries the human-readable content and attributes; the vector is only what similarity compares.

Summary

  • A vector database stores id + vector + payload and finds nearest neighbours.
  • Embeddings place similar content close together, so closeness means similarity.
  • Exact search is simple but linear; ANN indexes, persistence and filtering are what products add.
  • Vector search complements SQL and keyword search; it rarely replaces them.

Next: Your First RAG Pipeline