pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
6+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 09:10  "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: WalidMustapha (@WalidMustapha) @ 2025-01-18 09:10 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

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

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 09:29  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-01-18 09:29 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @WalidMustapha, an HNSW index won't help this query (it needs to scan all rows that match the `WHERE` condition).

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 09:29  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-01-18 09:29 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 10:21  "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: WalidMustapha (@WalidMustapha) @ 2025-01-18 10:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Just to fully understand the limitations with HNSW indexing and deduplication, am I correct in understanding that this query pattern would require a full scan:

```
WITH deduplicated AS (
    SELECT DISTINCT ON (id) *
    FROM table
)
SELECT *
FROM deduplicated
ORDER BY vector_column <-> query_vector;

```
And instead we must use this workaround (my current implementation):

```
WITH vector_search AS (
    SELECT *
    FROM table
    ORDER BY vector_column <-> query_vector
    LIMIT {increased_limit}  -- fetch more results to accomodate for duplicates
)
SELECT DISTINCT ON (id) *
FROM vector_search
ORDER BY vector_distance; 

```

The first query can't use the HNSW index because it needs to scan all rows after the DISTINCT operation, while the second query pattern can at least use the HNSW index for the initial vector search before deduplication. Is this correct?

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 21:40  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-01-18 21:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

That's correct about the first query.

The second query should fail with `SELECT DISTINCT ON expressions must match initial ORDER BY expressions` (and does when I test it). See #641 for more info.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication
@ 2025-01-18 22:07  "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: WalidMustapha (@WalidMustapha) @ 2025-01-18 22:07 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Appreciate you taking the time to reply.

You are correct, it does fail. I missed the 'id'. 

```
WITH vector_search AS (
SELECT *
FROM table
ORDER BY vector_column <-> query_vector
LIMIT {increased_limit}
)
SELECT DISTINCT ON (id) *
FROM vector_search
ORDER BY id, vector_distance
```



^ permalink  raw  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2025-01-18 22:07 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-18 09:10 [pgvector/pgvector] issue #760: HNSW Index Not Used When Combining ORDER BY Distance with Deduplication "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@github.com>
2025-01-18 09:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-18 09:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-18 10:21 ` "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@github.com>
2025-01-18 21:40 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-18 22:07 ` "WalidMustapha (@WalidMustapha)" <noreply+WalidMustapha@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