From: lounging-lizard (@lounging-lizard) <noreply+lounging-lizard@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #812: Can an index be used with a nested loop?
Date: Mon, 31 Mar 2025 23:25:59 +0000
Message-ID: <gh-pgvector-pgvector-812@github.com> (raw)
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.