Database / ChromaDB Interview Questions
What is the HNSW index in ChromaDB and what parameters can you tune?
ChromaDB uses HNSW (Hierarchical Navigable Small World) as its Approximate Nearest Neighbour (ANN) index. HNSW builds a layered graph structure where each node connects to its closest neighbours — queries traverse this graph efficiently to find approximate nearest neighbours in O(log n) time instead of exhaustive O(n) linear scan.
import chromadb client = chromadb.Client() # HNSW parameters are set as metadata at collection creation collection = client.create_collection( name="tuned_collection", metadata={ "hnsw:space": "cosine", # distance metric "hnsw:construction_ef": 200, # default 100 # Controls quality of index during construction. # Higher = better recall, slower inserts. "hnsw:search_ef": 100, # default 10 # Controls quality of search at query time. # Higher = better recall, slower queries. "hnsw:M": 16, # default 16 # Number of bi-directional links per node. # Higher = better recall + more memory + slower inserts. # Typical range: 4-64. }, ) # Note: HNSW parameters cannot be changed after collection creation # You would need to recreate the collection and re-insert data collection.add( documents=[f"Document number {i}" for i in range(10000)], ids=[str(i) for i in range(10000)], )
| Parameter | Default | Effect of increasing | Effect of decreasing |
|---|---|---|---|
| hnsw:space | l2 | Changes metric (cosine/ip) | — |
| hnsw:M | 16 | Better recall, more memory, slower inserts | Faster inserts, less memory, lower recall |
| hnsw:construction_ef | 100 | Better index quality, slower inserts | Faster inserts, lower quality graph |
| hnsw:search_ef | 10 | Better recall, slower queries | Faster queries, lower recall |
For most RAG use cases, the defaults work well for collections under ~100K documents. For large collections or when recall matters, increase hnsw:search_ef to 50–200 and set hnsw:construction_ef to at least 200 when building the index.
More Related questions...