From: prathier (@prathier) <noreply+prathier@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #719: SELECT WHERE ORDER BY LIMIT no results
Date: Wed, 27 Nov 2024 16:11:42 +0000
Message-ID: <gh-pgvector-pgvector-719-c2504268865@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-719@github.com>
References: <gh-pgvector-pgvector-719@github.com>
Yes, I've run `ANALYZE`.
Here a working query with plan:
```
SELECT
v.id,
(v.embedding <#> '[-0.033441752, ... 0.012570631]') as distance
FROM
public_vector_table v
LEFT JOIN public_files f ON f.id = v.file_id
WHERE f.status = 'INDEXATION_ERROR'
ORDER BY distance
limit 10;
```
Correct plan:
```
Limit (cost=913.17..913.18 rows=5 width=16) (actual time=0.139..0.141 rows=10 loops=1)
Buffers: shared hit=59
-> Sort (cost=913.17..913.18 rows=5 width=16) (actual time=0.138..0.139 rows=10 loops=1)
Sort Key: ((v.embedding <#> '[-0.033447266,...,0.012573242]'::halfvec))
Sort Method: top-N heapsort Memory: 25kB
Buffers: shared hit=59
-> Nested Loop (cost=0.86..913.11 rows=5 width=16) (actual time=0.036..0.124 rows=25 loops=1)
Buffers: shared hit=59
-> Index Scan using public_files_status_idx on public_files f (cost=0.43..4.45 rows=1 width=8) (actual time=0.019..0.037 rows=10 loops=1)
Index Cond: ((status)::text = 'INDEXATION_ERROR'::text)
Buffers: shared hit=13
-> Index Scan using public_vector_table_file_id_idx on public_vector_table v (cost=0.43..906.15 rows=251 width=959) (actual time=0.004..0.007 rows=2 loops=10)
Index Cond: (file_id = f.id)
Buffers: shared hit=46
Planning:
Buffers: shared hit=19
Planning Time: 0.405 ms
Execution Time: 0.174 ms
```
Here a non-working query (only the `WHERE` changes):
```
SELECT
v.id,
(v.embedding <#> '[-0.033441752, ... 0.012570631]') as distance
FROM
public_vector_table v
LEFT JOIN public_files f ON f.id = v.file_id
WHERE f.type = 'COLLECTIVE'
ORDER BY distance
limit 10;
```
plan:
```
Limit (cost=257.88..13212.86 rows=10 width=16) (actual time=5.227..5.229 rows=0 loops=1)
Buffers: shared hit=2485
-> Nested Loop (cost=257.88..11328096.92 rows=8744 width=16) (actual time=5.225..5.227 rows=0 loops=1)
Buffers: shared hit=2485
-> Index Scan using public_vector_table_embedding_idx on public_vector_table v (cost=257.44..11133279.04 rows=7090160 width=959) (actual time=4.869..4.941 rows=41 loops=1)
Order By: (embedding <#> '[-0.033447266,...,0.012573242]'::halfvec)
Buffers: shared hit=2353
-> Memoize (cost=0.44..0.63 rows=1 width=8) (actual time=0.007..0.007 rows=0 loops=41)
Cache Key: v.file_id
Cache Mode: logical
Hits: 8 Misses: 33 Evictions: 0 Overflows: 0 Memory Usage: 3kB
Buffers: shared hit=132
-> Index Scan using public_files_pkey on public_files f (cost=0.43..0.62 rows=1 width=8) (actual time=0.007..0.007 rows=0 loops=33)
Index Cond: (id = v.file_id)
Filter: ((type)::text = 'COLLECTIVE'::text)
Rows Removed by Filter: 1
Buffers: shared hit=132
Planning:
Buffers: shared hit=19
Planning Time: 0.417 ms
Execution Time: 6.060 ms
```
Both `status` and `type` column are indexed with btree, and both WHERE represents a very small subset of the data.
I really don't get why we don't have the same plan.