Sub topic 4
Why exact search stops scaling
This chapter assumes you can write the brute-force search from the previous chapter and know what a dot product is. You should also know that a database index narrows down which rows a query has to look at.
The cost of brute force
Exact search compares the query with every stored vector. With N vectors of dimension d, one query does about N × d multiply-add operations. It also has to read all N × d numbers from memory.
Memory traffic is usually the bottleneck. A float32 takes 4 bytes, so the data size is easy to compute.
| Vectors (N) | Dimension (d) | Size in float32 | Work per query |
|---|---|---|---|
| 1,000 | 64 | 256 KB | 64 thousand operations |
| 1,000,000 | 768 | about 3 GB | 768 million operations |
| 100,000,000 | 768 | about 307 GB | 76.8 billion operations |
Latency grows linearly with N. Doubling the collection doubles the time per query. Under heavy traffic, the cost per second is queries per second times N × d.
Measuring it
The script below times brute-force search as N grows. Exact numbers depend on your machine, but the trend does not.
import time
import numpy as np
rng = np.random.default_rng(0)
d = 256
query = rng.normal(size=d).astype("float32")
query /= np.linalg.norm(query)
for n in [10_000, 100_000, 1_000_000]:
vectors = rng.normal(size=(n, d)).astype("float32")
vectors /= np.linalg.norm(vectors, axis=1, keepdims=True)
start = time.perf_counter()
scores = vectors @ query
top5 = np.argpartition(-scores, 5)[:5]
ms = (time.perf_counter() - start) * 1000
print(f"N={n:>9,} {ms:8.1f} ms per query")
del vectorsRun it and the time per query rises about tenfold for each tenfold increase in N. The 1,000,000-vector case needs about 1 GB of RAM.
Why a normal index does not help
A B-tree works because values have an order. You can ask "is the key greater than 40?" and discard half the tree. Vectors have no such order. Closeness depends on all d coordinates at once.
Space-partitioning trees such as k-d trees do exist. They split space along one coordinate at a time. In low dimensions (2 or 3) they prune most of the data. In hundreds of dimensions they break down. This is part of what is called the curse of dimensionality.
Distances concentrate
In high dimensions, the distances from a query to random points become similar. The nearest point is barely closer than the average point. A tree that prunes branches based on "this region is too far away" finds that almost no region is too far. The search ends up visiting nearly every branch, which costs as much as brute force plus tree overhead.
Real embeddings are not random. They lie near lower-dimensional structure, so the effect is weaker than in random data. It is still strong enough that exact tree indexes give little benefit beyond roughly 10 to 20 dimensions.
When brute force is still right
Exact search is not wrong. It is a good choice when:
- The collection is small, for example under 100,000 vectors.
- You need perfect results, such as when building ground truth to evaluate other indexes.
- Queries are rare, or you can batch many queries into one matrix multiplication on a GPU.
What comes next
Since you cannot avoid the cost of comparing against everything, the next step is to accept a small error. Approximate indexes skip most vectors and usually still find the true neighbors. The following chapters cover how that trade works.
Exercise
A collection holds 50 million float32 vectors with 1,024 dimensions. Roughly how much memory do the raw vectors need, and how many multiply-add operations does one brute-force query take?
Show answer
Memory is 50,000,000 × 1,024 × 4 bytes, about 205 GB. One query takes 50,000,000 × 1,024, about 51 billion multiply-adds. Both are far too large for low-latency search on a single ordinary machine.
Exercise
Why does a k-d tree prune well in 3 dimensions but poorly in 500 dimensions?
Show answer
In 500 dimensions, distances from a query to most points are nearly equal, so few regions can be ruled out as too far away. The search must visit most branches and ends up comparing against almost every point.