pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #812: Can an index be used with a nested loop?
4+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #812: Can an index be used with a nested loop?
@ 2025-03-31 23:25 "lounging-lizard (@lounging-lizard)" <noreply+lounging-lizard@github.com>
0 siblings, 0 replies; 4+ messages in thread
From: lounging-lizard (@lounging-lizard) @ 2025-03-31 23:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
First up scanned the issues, thanks heaps to @ankane I see you constantly responding to issues regarding queries only for your questions to never be replied to and I can understand how frustrating that must be.
## Context
A table called `face` with a column called `embedding` (vector 512 dimensions), which has an `hnsw` index on it (default options).
I intend to query for a list of rows from `face` and use them to find their nearest neighbors. In natural language _for this set of faces find the N most similar faces._
## Queries
_Queries have been reduce to the simplest working example._
For the selected 5 face embeddings find the closest matching 10 face(s). My `face` table has around 670k rows.
```
CREATE TEMP TABLE search_embeddings AS
SELECT face.embedding
FROM face
LIMIT 5;
SELECT face.id
FROM face
CROSS JOIN search_embeddings
ORDER BY face.embedding <=> search_embeddings.embedding ASC
LIMIT 10;
```
_Note: I have also tried `LATERAL JOIN`, `INNER JOIN`, subquery but to the same effect._
#### EXPLAIN Output
```
Limit (cost=33388037.07..33388037.10 rows=10 width=24)
-> Sort (cost=33388037.07..35666516.47 rows=911391760 width=24)
Sort Key: ((face.embedding <=> search_embeddings.embedding))
-> Nested Loop (cost=0.00..13693188.81 rows=911391760 width=24)
-> Seq Scan on face (cost=0.00..22285.41 rows=670141 width=34)
-> Materialize (cost=0.00..30.40 rows=1360 width=32)
-> Seq Scan on search_embeddings (cost=0.00..23.60 rows=1360 width=32)
JIT:
Functions: 4
Options: Inlining true, Optimization true, Expressions true, Deforming true
```
#### EXPLAIN ANALYZE Output
```
Limit (cost=33388037.07..33388037.10 rows=10 width=24) (actual time=18908.692..18908.695 rows=10 loops=1)
-> Sort (cost=33388037.07..35666516.47 rows=911391760 width=24) (actual time=18814.916..18814.918 rows=10 loops=1)
Sort Key: ((face.embedding <=> search_embeddings.embedding))
Sort Method: top-N heapsort Memory: 26kB
-> Nested Loop (cost=0.00..13693188.81 rows=911391760 width=24) (actual time=0.057..17913.608 rows=3350705 loops=1)
-> Seq Scan on face (cost=0.00..22285.41 rows=670141 width=34) (actual time=0.009..210.992 rows=670141 loops=1)
-> Materialize (cost=0.00..30.40 rows=1360 width=32) (actual time=0.000..0.001 rows=5 loops=670141)
-> Seq Scan on search_embeddings (cost=0.00..23.60 rows=1360 width=32) (actual time=0.005..0.006 rows=5 loops=1)
Planning Time: 0.193 ms
JIT:
Functions: 4
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 0.309 ms, Inlining 59.438 ms, Optimization 19.419 ms, Emission 14.917 ms, Total 94.083 ms
Execution Time: 18929.096 ms
```
## My Workaround
As a work around I am able to essentially retrieve the `search_embeddings` then use a `FOR ... IN ...` to iterate the embeddings and create a big `SELECT ... UNION ALL SELECT ...` where the select statements look like so `SELECT frame_id FROM face ORDER BY embedding <=> %L ASC LIMIT 5` (where `%L` is replaced by the nth face embedding as it iterates.
This uses the index and I believe it is due to the fact that it goes 1 by 1 performing the query it is significantly faster then the above approach due to this fact.
However I believe if I can figure out how to use the index with the loop on a sub query I would get even greater performance.
I have reviewed the docs but it could be that I have missed a limitation and apologize if I have.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #812: Can an index be used with a nested loop?
@ 2025-04-01 00:24 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2025-04-01 00:24 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @lounging-lizard, the `UNION ALL` approach is the best way to accomplish this - see #59 and https://github.com/pgvector/pgvector/issues/713#issuecomment-2469289695 (even if the Postgres planner gained the ability to use the index in the first query, it would still need to do 5 separate index lookups).
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #812: Can an index be used with a nested loop?
@ 2025-04-01 00:24 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2025-04-01 00:24 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #812: Can an index be used with a nested loop?
@ 2025-04-01 00:52 "lounging-lizard (@lounging-lizard)" <noreply+lounging-lizard@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: lounging-lizard (@lounging-lizard) @ 2025-04-01 00:52 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks heaps @ankane
^ permalink raw [nested|flat] 4+ messages in thread
end of thread, other threads:[~2025-04-01 00:52 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-31 23:25 [pgvector/pgvector] issue #812: Can an index be used with a nested loop? "lounging-lizard (@lounging-lizard)" <noreply+lounging-lizard@github.com>
2025-04-01 00:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-01 00:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-01 00:52 ` "lounging-lizard (@lounging-lizard)" <noreply+lounging-lizard@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