Skip to content
Ameen Abozaid
Planned lessonMID-067-L04

Approximate indexes: HNSW and IVFFlat

  • Lesson 4 of 6
  • Mid-level
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 lists and probes.
  • 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

sql
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 scan

Check 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.