Approximate indexes: HNSW and IVFFlat
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
choose between HNSW and IVFFlat by recall, build time, and memory.
Planned topics
- Exact search checks every row; approximate (ANN) indexes are faster but may miss true nearest neighbours. The share found is the recall.
- HNSW: graph index; better recall and speed trade-off, slower to build, more memory, can be created on an empty table.
- IVFFlat: groups vectors into lists; faster build, smaller; build it after the data is loaded; recall depends on
listsandprobes. - Tune at query time (
hnsw.ef_search,ivfflat.probes); verify parameter names and defaults for your pgvector version. - Always measure recall against an exact query on a sample before trusting an index.
Draft outline details
Demonstration
CREATE INDEX ON help_chunk USING hnsw (embedding vector_cosine_ops);
SET hnsw.ef_search = 80;
-- recall check: compare top-10 with the index vs. top-10 from an exact scanCheck your understanding
Which index builds faster on a freshly loaded static corpus?
Show answer
IVFFlat; HNSW costs more build time and memory but usually gives better recall.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.