Skip to content
BytePatterns

Vector Databases

AI & ML: lesson 12 of 15

Nearest-neighbour search that stays fast at scale.

Lesson 12 of 15 · 5 min

Vector Databases

Step 1 of 13

A vector database answers one question: which stored vectors sit nearest this one?

The Idea

A vector database stores embeddings and answers one question: which stored vectors sit nearest this one? Comparing against every row works until the collection grows, then an index groups neighbours so a query touches only a fraction.

Real-World Example

A textile warehouse shelved by shade and weave rather than by order number. A buyer holding a swatch walks straight to a neighbourhood of near-matches instead of unrolling every bolt in the building.

The Tradeoff

Approximate indexes swap recall for speed, and a missed neighbour never shows up as an error — the results simply look slightly worse. Exact search stays correct but scales linearly with the collection. For small corpora, brute force inside the database you already run is usually simpler and quick enough.

Your turn

Fill in the blank.

store = {"a": [1, 0], "b": [0, 1], "c": [0.9, 0.1]}
q = [1, 0]

def score(v):
  return sum(x * y for x, y in zip(q, v))

# rank the keys by similarity to q -> want ['a', 'c']
top = sorted(store, key=___, reverse=True)[:2]
print(top)

Mini quiz

1 / 3

An approximate nearest-neighbour index gives you: