Find the most similar thing — fast — brute-force vector search and the 10 ms wall
Once every item is turned into a list of numbers, 'find the most similar item' means 'find the closest list' — but checking every stored list one-by-one is exact yet hopeless at a billion items, and that single wall is the reason this whole system exists.
Scene 01
Find the most similar thing — fast
- Watch
- Try it
- Predict
- Capture
An app needs to answer one question over and over: 'find the items most similar to this one.' To make 'similar' something a computer can compute, we turn every item into a short list of numbers — here, each song becomes two numbers: how acoustic-vs-electronic it is, and how calm-vs-energetic. Now 'the most similar song' just means 'the song whose list of numbers is closest to mine.' Watch: 'Now Playing' lights up, and the search measures the straight-line gap to every single song, one at a time, before it can rank them. For 14 songs that's instant. Hold onto one fact: to be SURE of the closest, it had to touch every point.
Highlighted lines are the ones running in the diagram right now.
def search(query, k):heap = [] # k smallest distances so farfor vec in all_vectors: # touches all Nd = distance(query, vec)heap.push((d, vec.id))if len(heap) > k:heap.pop_largest()return argsort(heap)[:k] # the true top-k
def distance(query, vec):total = 0.0# every dimension counts — closeness lives in all Dfor i in range(D): # D = 1536diff = query[i] - vec[i]total += diff * diff # one multiply-addreturn total
def search_parallel(query, k, cores):shards = split(all_vectors, cores)partials = parallel_map(lambda shard: scan(shard, query, k),shards,)# each core still scans its whole shard:# sum of shard sizes == N, work is unchangedreturn merge_top_k(partials, k)
Where this sits in Build a vector database (Pinecone / Weaviate / pgvector style)
Scene 01 of 15, in the Why vectors act — Why a billion-vector exact scan can't hit 10 ms, and what 'closest' even means.. Turn items into lists of numbers and 'most similar' becomes 'closest list' — but checking every one is exact and hopeless at a billion. That wall is why this whole system exists.
Up next. We keep saying 'closest list of numbers', but we never said what 'closest' actually means. Before we can make search fast, we have to pin down exactly how to measure how similar two vectors are — and it turns out there is more than one way, and picking the wrong one silently returns garbage.
All 15 scenes in Build a vector database (Pinecone / Weaviate / pgvector style) · Every curriculum