pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
6+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:07 "masdeval (@masdeval)" <noreply+masdeval@github.com>
0 siblings, 0 replies; 6+ messages in thread
From: masdeval (@masdeval) @ 2024-11-11 23:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Consider two tables A and B. Both tables have only two columns: ID and embedding, where embedding is a pg_vector.
Also consider that both tables have a HNSW index created such as:
`CREATE INDEX ON A USING hnsw (embedding vector_cosine_ops);
`CREATE INDEX ON B USING hnsw (embedding vector_cosine_ops);
`
Then we run the query:
`EXPLAIN (ANALYZE, VERBOSE) select A.ID, B.ID, 1-(A.embedding <=> B.embedding)as sim
from A , B
order by (A.embedding <=> B.embedding) ASC
limit 100;
`
This takes forever to execute and when it finishes the query plan returned is not using the index:
> Limit (cost=121120518.71..121120530.37 rows=100 width=153) (actual time=2565720.140..2565728.806 rows=100 loops=1)
Output: A.ID, B.ID, (('1'::double precision - (A.embedding_original <=> B.embedding_original))), ((A.embedding_original <=> B.embedding_original))
Buffers: shared hit=6470639309 read=98636 written=14108
-> Gather Merge (cost=121120518.71..421169280.95 rows=2571666976 width=153) (actual time=2565628.341..2565636.997 rows=100 loops=1)
Output: A.ID, B.ID, (('1'::double precision - (A.embedding_original <=> B.embedding_original))), ((A.embedding_original <=> B.embedding_original))
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=6470639309 read=98636 written=14108
-> Sort (cost=121119518.68..124334102.40 rows=1285833488 width=153) (actual time=2565274.734..2565274.740 rows=80 loops=3)
Output: A.ID, B.ID, (('1'::double precision - (A.embedding_original <=> B.embedding_original))), ((A.embedding_original <=> B.embedding_original))
Sort Key: ((A.embedding_original <=> B.embedding_original))
Sort Method: top-N heapsort Memory: 63kB
Buffers: shared hit=6470639309 read=98636 written=14108
Worker 0: actual time=2565135.507..2565135.512 rows=100 loops=1
Sort Method: top-N heapsort Memory: 67kB
JIT:
Functions: 3
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 0.390 ms, Inlining 66.458 ms, Optimization 44.012 ms, Emission 24.689 ms, Total 135.550 ms
Buffers: shared hit=2157710525 read=31222 written=4157
Worker 1: actual time=2565306.941..2565306.946 rows=100 loops=1
Sort Method: top-N heapsort Memory: 67kB
JIT:
Functions: 3
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 0.379 ms, Inlining 66.651 ms, Optimization 42.803 ms, Emission 26.573 ms, Total 136.405 ms
Buffers: shared hit=2162862904 read=30880 written=3523
-> Nested Loop (cost=0.00..71975887.35 rows=1285833488 width=153) (actual time=90.717..2396491.368 rows=1028666790 loops=3)
Output: A.ID, B.ID, ('1'::double precision - (A.embedding_original <=> B.embedding_original)), (A.embedding_original <=> B.embedding_original)
Buffers: shared hit=6470639203 read=98628 written=14108
Worker 0: actual time=135.725..2396158.052 rows=1029086775 loops=1
Buffers: shared hit=2157710474 read=31216 written=4157
Worker 1: actual time=136.403..2396554.486 rows=1031384340 loops=1
Buffers: shared hit=2162862849 read=30878 written=3523
-> Parallel Seq Scan on public.sinonimos_yobbconstrucao A (cost=0.00..26672.49 rows=17349 width=65) (actual time=0.021..128.641 rows=13879 loops=3)
Output: A.id, A.ID, A.sinonimo, A.embedding_sinonimo, A.embedding_original, A.category, A.embedding_sinonimo_gguf, A.embedding_original_gguf
Buffers: shared read=26499
Worker 0: actual time=0.025..125.807 rows=13885 loops=1
Buffers: shared read=8382
Worker 1: actual time=0.026..131.216 rows=13916 loops=1
Buffers: shared read=9572
-> Seq Scan on public.sinonimos_sinapi B (cost=0.00..2850.15 rows=74115 width=108) (actual time=0.001..6.034 rows=74115 loops=41638)
Output: B.id, B.ID, B.sinonimo, B.embedding_original, B.embedding_sinonimo
Buffers: shared hit=87812293 read=2249 written=2265
Worker 0: actual time=0.001..6.011 rows=74115 loops=13885
Buffers: shared hit=29282823 read=642 written=723
Worker 1: actual time=0.001..6.029 rows=74115 loops=13916
Buffers: shared hit=29348605 read=239 written=383
Planning:
Buffers: shared hit=25 read=1
Planning Time: 64.180 ms
JIT:
Functions: 10
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 1.241 ms, Inlining 158.804 ms, Optimization 128.876 ms, Emission 75.163 ms, Total 364.084 ms
Execution Time: 2565729.349 ms
What am I doing wrong?
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: ankane (@ankane) @ 2024-11-11 23:15 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @masdeval, an index won't help this query (comparing all vectors in A with all vectors in B), which is why it's not being used.
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: ankane (@ankane) @ 2024-11-11 23:15 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 #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:20 ` "masdeval (@masdeval)" <noreply+masdeval@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: masdeval (@masdeval) @ 2024-11-11 23:20 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Hi @masdeval, an index won't help this query (comparing all vectors in A with all vectors in B), which is why it's not being used.
Is there any way I can change this query to get the same result while improving the performance?
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:30 ` "masdeval (@masdeval)" <noreply+masdeval@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: masdeval (@masdeval) @ 2024-11-11 23:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> Hi @masdeval, an index won't help this query (comparing all vectors in A with all vectors in B), which is why it's not being used.
I have seen queries like :
> WITH question_in_focus AS (
SELECT * FROM question_embedding_duplicates
WHERE _id = '001154ea-7c07-4914-b061-805fd99e64a3'
)
SELECT * FROM question_embedding_duplicates
WHERE question_embedding_duplicates.complete_description_embedding IS NOT NULL
ORDER BY complete_description_embedding <=> (SELECT complete_description_embedding FROM question_in_focus) ASC
LIMIT 10;
Why in this type of query the index allegedly works and in mine don't?
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario
@ 2024-11-11 23:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: ankane (@ankane) @ 2024-11-11 23:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
That query compares a single vector with a column (instead of a column with a column). You could try running separate queries for each row in A (or groups of rows with `UNION ALL` - #59) to use the approximate index, then combine the results in-memory to get the top 100.
Edit: Thinking about it more, an index might be able to help the original query. However, the Postgres planner doesn't seem to consider it (even with `enable_seqscan = off`), so this would likely require an upstream change.
^ permalink raw [nested|flat] 6+ messages in thread
end of thread, other threads:[~2024-11-11 23:35 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-11-11 23:07 [pgvector/pgvector] issue #713: Could not make a vector index work even in the simplest scenario "masdeval (@masdeval)" <noreply+masdeval@github.com>
2024-11-11 23:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-11-11 23:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-11-11 23:20 ` "masdeval (@masdeval)" <noreply+masdeval@github.com>
2024-11-11 23:30 ` "masdeval (@masdeval)" <noreply+masdeval@github.com>
2024-11-11 23:35 ` "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