Vector Databases 3/5: Indexes, Search and Performance
Understand exact and approximate nearest-neighbour search, HNSW, IVFFlat, hybrid retrieval, metadata filtering and performance evaluation.
Exact search and approximate neighbours
For N records of dimension d, brute-force comparison with every vector costs roughly O(Nd). Exact search returns the closest points under the chosen metric and makes a strong small-scale baseline. At a million vectors, repeated full scans can be expensive. Approximate nearest-neighbour methods skip candidates to reduce work, potentially missing true neighbours. This is a speed-recall trade-off, not an automatic improvement in semantic relevance.
HNSW: a layered neighbourhood graph
HNSW organizes vectors into a multilayer graph connecting nearby nodes. Sparse upper layers help navigate large distances and dense lower layers explore local neighbourhoods. Parameters such as M, ef_construction and ef_search govern connectivity and candidate breadth. Increasing them can improve recall at a memory, build-time or query-latency cost. Measure on your actual data and account for update workloads.
IVF, compression and data distribution
Inverted-file approaches partition vectors into groups and search selected groups nearest the query. IVFFlat compares full vectors within those partitions. Product quantization represents vectors with compact codes to reduce memory, introducing additional approximation. Changing data distributions can reduce partition quality. Neither HNSW nor IVF is universally superior: evaluate latency, memory, update rate, filter selectivity and distribution.
A pgvector SQL example
In PostgreSQL, pgvector integrates vector columns and operators into relational queries. The three dimensions below are illustrative: use the actual model dimension. The <=> operator represents cosine distance and ascending order returns closer vectors. Match the index operator class to the metric. Filtered approximate retrieval may need additional tuning to return sufficient results. Inspect actual execution plans and measure in staging before production changes.
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE hotels_demo (id bigint PRIMARY KEY, city text, embedding vector(3));
INSERT INTO hotels_demo VALUES (1,'Antalya','[1,0,0]'),(2,'Antalya','[0.8,0.6,0]'),(3,'Izmir','[0,1,0]');
CREATE INDEX hotels_demo_hnsw ON hotels_demo USING hnsw (embedding vector_cosine_ops);
SELECT id, embedding <=> '[1,0,0]'::vector AS distance
FROM hotels_demo WHERE city='Antalya'
ORDER BY embedding <=> '[1,0,0]'::vector LIMIT 2;
Metadata filters, hybrid search and reranking
Lexical search is strong for product codes, error identifiers and exact legal references; dense retrieval can help with open-ended phrasing. Hybrid search can combine independent rankings using reciprocal rank fusion, assigning 1/(k+rank) contributions instead of adding incomparable raw scores. A cross-encoder may rerank the shortlisted results. Metadata filters preserve tenant, access, language and location constraints. Authorization must remain a deterministic rule.
Recall, latency and real capacity
Use the exact top K under the same embeddings and metric as ANN ground truth. If both lists contain K candidates, intersection size divided by K gives ANN Recall@K. Human-labelled relevance is a different evaluation; use precision, recall or nDCG against judged results. Monitor p50, p95 and p99 latency. A hundred thousand 384-dimensional float32 vectors occupy about 153.6 MB in raw coordinates, excluding index, metadata and replica overhead.
Design a performance experiment
Select progressively larger samples and prepare diverse queries covering language, rare terms and exact filters. First calculate exact neighbours; then compare approximate configurations for recall and latency under cold and warm conditions. Include update traffic and different filter selectivities. Document hardware, versions, dimensions, record counts and concurrency. Treat parameter changes, model changes and hybrid retrieval as separate hypotheses.
Sources and all five chapters
This is part 3 of a five-part series. Start with the foundations or jump directly to another chapter. The links below lead to the other articles in the same series, followed by primary technical references. Code listings are educational examples; evaluate performance and model relevance in your own environment.
Reader discussion
Bu gelişmeyi kendi alanınızda uygulamadan önce hangi veriyi doğrulamak isterdiniz?
No comments yet. Be the first to contribute.
Write a comment