Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Vector Database Basics

The whole lesson is free and interactive. Read it here, or press Start lab to run it for real in a live lab, with no account needed.

Container lab8 min read6 commands

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Where does a search feature keep its vectors?

A vector database stores vectors with their data and finds the ones nearest a question, fast. Every search by meaning, from a shop to a chatbot that reads documents, keeps its vectors in one. This lab has two: Qdrant, built for the job, and PostgreSQL with pgvector.

By the end of this lesson

Note

I can search a vector database and measure how much an index misses.

Key words

  • Vector: a list of numbers standing for a text or a picture.
  • Collection: a set of vectors of one length, searched together.
  • Exact search: comparing the question with every vector.
  • Index: a structure that avoids comparing every vector.
  • Recall: the share of the true nearest vectors a search found.

Starter: what you already know

Note

Answer in your head. 1. What does a database index make faster? 2. What does SQL's LIMIT do? 3. Two points on a map: how do you say which is nearer? Answers: finding rows without reading every one; it keeps only the first rows; compare the straight-line distances.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. Qdrant is a service called qdrant, PostgreSQL is pg, and the scripts are in tools. The data takes under a minute to load after the lab starts. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Two stores, the same data

The script lists what each store holds. Qdrant has three collections, and docs holds 20000 points of 48 numbers each. PostgreSQL has the same 20000 rows in its docs table, in a column of type vector(48). The numbers are made up, the same in every copy, not embeddings from a model.

Command
docker exec tools python3 steps/stores.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

2. A search inside one shop

The canteen's 12 items each have 4 numbers: how hot, cold, sweet and savoury they are. The first search asks for something hot and sweet across both shops: hot chocolate, porridge and coffee. The second asks inside the north shop only: hot chocolate, flapjack and tea. Keeping each shop's data apart like this is what a namespace is for.

Command
docker exec tools python3 steps/shop.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
docker exec tools python3 steps/shop.py north

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

3. Exact search reads everything

The script compares one question with all 20000 vectors in plain Python and prints vectors compared: 20000. Qdrant's exact search finds the same five, so it prints the same five: True. Exact search never misses, but its work grows with every vector added.

Command
docker exec tools python3 steps/exact.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

4. An index reads a little and may miss

The first command builds an HNSW index on PostgreSQL's docs table in about two seconds. The second measures it over 50 questions: the plan says Index Scan using docs_hnsw, and recall@10 is about 1.0, so it found nearly all of the true top 10 for each question. Your copy may show 0.99: the index is built with chance in it.

Command
docker exec pg psql -U lab -d shop -c "CREATE INDEX docs_hnsw ON docs USING hnsw (embedding vector_cosine_ops)"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
docker exec tools python3 steps/pgrecall.py hnsw 40

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Exit check

Tip

Answer without looking back. 1. How many vectors did the exact search compare? 2. Which item came first for hot and sweet? 3. What does an index save, and what can it cost? 4. What does recall of 1.0 mean? Answers: 20000; hot chocolate; reading every vector, at the cost of sometimes missing one; it found every true neighbour.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is three small services, and nothing here marks itself: the student hands in a short report of their own copy, with recall for four ef_search and four probes values, a filtered search for hilltop in both stores and a store chosen for 2 million documents, none of which this lesson does for them. Key learning points: 1. A vector database finds the nearest vectors and keeps each tenant's data apart. 2. Exact search compares with every vector. 3. An index reads a little and may miss, measured as recall. Common mistake: thinking an index returns slightly wrong scores; it returns exact scores and may miss items. Equipment: the lab and its terminal. Tip: ask students to guess the shop's top three before the Run. The module Vector databases in practice goes further in six lessons. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

Teaching a class? Set these lessons for your students with marking and a lab for every student.