pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
5+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
@ 2024-08-18 12:04  "matan-airis (@matan-airis)" <noreply+matan-airis@github.com>
  0 siblings, 0 replies; 5+ messages in thread

From: matan-airis (@matan-airis) @ 2024-08-18 12:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I have a situation where I have a bunch of embeddings I want to lookup. So far, the fastest way I managed to do that is by making a database query per vector, which is reasonably fast (looking 100 vectors up in a table with 1M rows takes ~30 seconds if I run the queries serially and even faster if I parallelize the queries).

Trying to do this with one query takes several times longer, about 3 minutes. The query I have so far looks something like this, where `t_temp` is a temp table with 100 vectors and `t` is a table with 1M vectors. For this application I need exact nearest-neighbor, so I'm not using an index.

```sql
SELECT t_results.* FROM (
  SELECT embedding
  FROM t_temp
) t_v
JOIN LATERAL (
  SELECT (t_v.embedding <#> embedding) * -1 AS similarity
  FROM t
  ORDER BY similarity DESC
  LIMIT 1
) t_results ON true
```

Is there a better way to do this query, or should I keep using multiple queries?

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

* Re: [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
@ 2024-08-18 20:11  "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2024-08-18 20:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @matan-airis, I suspect parallelizing the queries will be the fastest approach in terms of wall time. You could try minimizing network requests by combining multiple queries with `UNION ALL`, but it probably won't make much of a difference for exact search over 1M vectors.

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

* Re: [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
@ 2024-08-18 20:11  "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2024-08-18 20:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
@ 2024-08-18 20:16  "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2024-08-18 20:16 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Also, if you're trying to find near duplicates over most / all vectors, you may want to use an in-memory library with optimized matrix operations. https://github.com/pgvector/pgvector-python/issues/85

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

* Re: [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)?
@ 2025-08-26 07:09  "CorentinvdBdO (@CorentinvdBdO)" <noreply+CorentinvdBdO@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: CorentinvdBdO (@CorentinvdBdO) @ 2025-08-26 07:09 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

The lack of batch search (100/1000 query vectors) is the sole reason we moved back to Qdrant in my company. Performances are horrendeous.
I prefer managing pgvector over two different databases but we have to go back.

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


end of thread, other threads:[~2025-08-26 07:09 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-18 12:04 [pgvector/pgvector] issue #645: What is the best way to find nearest-neighbor for N vectors (batch query)? "matan-airis (@matan-airis)" <noreply+matan-airis@github.com>
2024-08-18 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-18 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-18 20:16 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-08-26 07:09 ` "CorentinvdBdO (@CorentinvdBdO)" <noreply+CorentinvdBdO@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