pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: WalidMustapha (@WalidMustapha) <noreply+WalidMustapha@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
Date: Sat, 18 Jan 2025 09:10:53 +0000
Message-ID: <gh-pgvector-pgvector-760@github.com> (raw)

**Description**
When using HNSW indexing with a query that combines both deduplication and ordering by vector distance, the HNSW index is not being utilized. This occurs specifically when using `.distinct()` followed by `.order_by()` in SQLAlchemy (or equivalent in raw SQL).

**Current Behavior**
The HNSW index is skipped when the query includes:

1. A DISTINCT clause on any column
2. An ORDER BY on vector distance

This leads to significantly slower query performance as a sequential scan is performed.

**Example**
```
SELECT DISTINCT ON (id) *
FROM table
WHERE some_conditions
ORDER BY vector_column <-> query_vector;

```

**Related Information**
- Using pgvector 0.8
- HNSW index is properly created and works when used without deduplication

**Questions**
- Is this a known limitation of the current HNSW implementation?
- Would implementing HQANN or supporting bitmap scans in HNSW be considered as potential solutions?

**Additional Context**
This issue is related to the broader discussion about combining HNSW with filtering (#259 ) but specifically focuses on the deduplication aspect which appears to prevent HNSW index usage.

**Current Workaround**
The current solution is to fetch more results than needed and then deduplicate afterwards. This is suboptimal because:

- It requires manually tuning the number of results to fetch
- It increases memory usage and processing time
- It may still miss relevant results if the initial fetch size isn't large enough

/cc @ankane  @jkatz 
Let me know if you would like me to provide any additional details or clarification.

view thread (6+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-760@github.com>
Permalink:  ../gh-pgvector-pgvector-760@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox