Sub topic 8
Why Approximate Search Exists and What It Costs in Recall
Assume you know basic PostgreSQL commands and understand that an embedding is a numeric vector representing text, an image, or another object. This chapter explains why a database may return approximate nearest neighbors instead of checking every stored vector.
Exact search is accurate but expensive
An exact nearest-neighbor query calculates the distance from the query vector to every row, sorts the results, and returns the closest rows.
SELECT id, content
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;The <=> operator calculates cosine distance. PostgreSQL can scan the whole table, so this query finds the true five closest rows. However, its work grows with the number of vectors. A table with 10 million embeddings may require millions of distance calculations for one query.
Approximate nearest-neighbor, or ANN, search avoids comparing the query with every vector. It uses an index to inspect a promising subset of vectors. This usually makes queries much faster, but the subset might not contain every true nearest neighbor.
| Search method | Vectors examined | Result guarantee | Typical use |
|---|---|---|---|
| Exact search | All or nearly all | True nearest neighbors | Evaluation and small tables |
| Approximate search | A selected subset | Usually close, but not guaranteed | Large production tables |
Recall is the cost of approximation
Recall measures how many of the true nearest neighbors appear in the approximate result.
For example, suppose exact search returns these five IDs:
1, 2, 3, 4, 5An ANN query returns:
1, 2, 3, 6, 7The result has three correct IDs out of five, so its recall at 5 is 3 / 5 = 0.60.
Approximate indexes expose search settings that control this trade-off. Searching more candidates generally improves recall and increases latency and resource use.
How HNSW and IVFFlat limit work
HNSW, or Hierarchical Navigable Small World, stores connections between vectors in multiple graph layers. A query follows graph connections toward nearby vectors. The hnsw.ef_search setting controls how many candidates the search explores. A larger value usually improves recall but takes more time.
IVFFlat divides vectors into lists, or clusters. A query first finds nearby clusters and then searches only selected lists. The ivfflat.probes setting controls how many lists it searches. A larger probe count improves recall and reduces the speed advantage.
CREATE INDEX items_embedding_hnsw
ON items USING hnsw (embedding vector_cosine_ops);
SET hnsw.ef_search = 100;
SELECT id, content
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;
CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);
SET ivfflat.probes = 10;The index operator class must match the distance operator. For example, vector_cosine_ops supports cosine distance, while vector_l2_ops supports Euclidean distance.
Exercise
Exercise
If exact search returns IDs 10, 11, 12, 13, and an ANN query returns IDs 10, 11, 14, what is recall at 3?
Show answer
Recall is 2/3, or about 0.67, because two of the three approximate results are in the exact result set.