Skip to Content

How do you confirm your HNSW index is actually being used?

Category: Database & RAG

Answer

EXPLAIN (ANALYZE, BUFFERS) on a representative query. The plan should show Index Scan using ... hnsw-... not Seq Scan. If you see Seq Scan on a million-row table, the planner fell back — usually because the operator doesn’t match the index op-class, or the planner picked a sequential scan as “cheaper” for a tiny query.

Concrete examples from the fca project context

Example 1

SET hnsw.ef_search=100; EXPLAIN ANALYZE SELECT … ORDER BY embedding <=> :q LIMIT 10.

Example 2

Look for “Index Scan … hnsw …” — if you see “Seq Scan” your index is silently ignored.

Example 3

Pair the index op-class with the query operator: cosine_op with <=>, l2_op with <->.

Data flow / flow chart

Query -> planner -> {Seq Scan} OR {Index Scan using hnsw}
  Always assert the right one before shipping

Takeaway

Trust EXPLAIN ANALYZE, not “I created the index.” Use it in CI.