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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
