pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
@ 2024-08-28 07:22  "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-28 07:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Issue with Index Utilization for Cosine Similarity Searches Using `<=>` Operator

### Description
I am currently working with PostgreSQL and using the HNSW index to optimize vector similarity searches. My aim is to leverage the `<=>` operator for cosine similarity measures. However, I've encountered an issue where this operator does not utilize the HNSW index as expected, resulting in a full sequential scan rather than an indexed scan.

### Details
- **Index used**: HNSW
- **Data type**: vector 

### Observations
I conducted several tests using `EXPLAIN ANALYZE` to better understand the query planner's behavior. Here's what I found:
- When using the `<->` operator (designed for Euclidean distance), the query planner successfully uses the HNSW index, resulting in significantly faster query times.
- Conversely, when I switch to the `<=>` operator for cosine similarity, PostgreSQL reverts to a full sequential scan. This seems to indicate a lack of support or optimization for cosine similarity in conjunction with the HNSW index.

#### Query Details
Here are the queries I executed:

1. **Euclidean Distance (Indexed)**:

```sql
   EXPLAIN ANALYZE SELECT * FROM my_table ORDER BY vector_column <-> query_vector LIMIT 10;
```

Result: Index Scan using my_table_vector_idx on my_table

2. Cosine Similarity (Sequential Scan):

```sql
EXPLAIN ANALYZE SELECT * FROM my_table ORDER BY vector_column <=> query_vector LIMIT 10;
```
Result: Sequential Scan on my_table

## Request
Could you please provide insights into:

- Whether the <=> operator is intended to support HNSW index utilization for cosine similarity in PostgreSQL?
- Any potential configuration or implementation steps I might have missed that could enable indexed searches with cosine similarity?
- If this functionality is not currently supported, could it be considered for future development?

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

* Re: [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
@ 2024-08-28 07:32  "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-28 07:32 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Uh oh, seems like it was a scam 🥶


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

* Re: [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
@ 2024-08-28 07:40  "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-08-28 07:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @RakshitKhajuria, it sounds like you've indexed the L2 distance but not the cosine distance. You'll need to create an index for each distance function you want to use ([docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#hnsw)).

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

* Re: [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
@ 2024-08-28 07:40  "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-08-28 07:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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


end of thread, other threads:[~2024-08-28 07:40 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-28 07:22 [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-28 07:32 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-28 07:40 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-28 07:40 ` "ankane (@ankane)" <noreply+ankane@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