From: damaon (@damaon) <noreply+damaon@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #825: Question: HNSW index params
Date: Mon, 21 Apr 2025 16:53:50 +0000
Message-ID: <gh-pgvector-pgvector-825@github.com> (raw)
I have such query:
```sql
WITH similarity_candidates AS (
SELECT id, embedding <=> :embedding AS vector_score
FROM topic
WHERE is_recent = true
ORDER BY embedding <=> :embedding
LIMIT 500
)
```
And such index:
```sql
CREATE INDEX CONCURRENTLY idx_topic_embedding_hnsw_recent
ON topic USING hnsw (embedding vector_cosine_ops)
WHERE is_recent = TRUE;
```
When I change to too high ef.search (90), the query planer starts to simply use is_recent index and compute embedding for all candidates (which can be up to 100K) and query is slow (3s+) instead of < 20ms when index triggered.
The problem is that I do further filtering and actually embedding match is most selective from these 100K rows so I need top 500 or even better top 1000 to further filter it later but with default index settings it doesn't work.
I really searched a lot of internet for some good answer to this issue but it's very hard to get some solid advice that's why I am posting here.
Question:
How should I tweak HNSW index params to make it work well for searching top 500 candidates from 100K rows?
More context:
This is for searching news so data is coming all the time that's why I decided to use partial index that's recomputed and HNSW as it seems to handle data addition better than IVFlat AFAIK. It's also read heavy app.
My more general question: https://stackoverflow.com/questions/79584189/postgres-query-optimization-partial-indexes
Extra debug info:
`pg_vector` version: `0.5.1`, EDIT: updated to `0.8.0` same issue
`postgres` version: `PostgreSQL 14.17 on x86_64-pc-linux-gnu, compiled by Debian clang version 12.0.1, 64-bit`