From: rferraton (@rferraton) <noreply+rferraton@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #781: index not used with cte
Date: Fri, 21 Feb 2025 16:30:49 +0000
Message-ID: <gh-pgvector-pgvector-781@github.com> (raw)
Index (ivfflat at least) are not used when the query is a **cte** :
```sql
WITH vars AS (
SELECT chunk_embedding AS qvt_embedding
FROM dbpedia_14
WHERE chunk_index = 473679
)
SELECT
a.title,
vars.qvt_embedding <-> a.chunk_embedding AS l2distance
FROM dbpedia_14 a
CROSS JOIN vars
ORDER BY l2distance asc
limit 10;
```
==>
```
Limit (cost=83624.74..83624.76 rows=10 width=27) (actual time=3349.047..3349.060 rows=10 loops=1)
Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
-> Sort (cost=83624.74..85199.92 rows=630073 width=27) (actual time=3349.046..3349.058 rows=10 loops=1)
Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
Sort Key: ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
Sort Method: top-N heapsort Memory: 26kB
-> Nested Loop (cost=0.42..70009.09 rows=630073 width=27) (actual time=0.105..3276.230 rows=630073 loops=1)
Output: a.title, (dbpedia_14.chunk_embedding <-> a.chunk_embedding)
-> Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14 (cost=0.42..8.44 rows=1 width=18) (actual time=0.068..0.080 rows=1 loops=1)
Output: dbpedia_14.label, dbpedia_14.title, dbpedia_14.content, dbpedia_14.split, dbpedia_14.content_word_count, dbpedia_14.chunk, dbpedia_14.content_n_chunks, dbpedia_14.chunk_index, dbpedia_14.chunk_word_count, dbpedia_14.chunk_embedding
Index Cond: (dbpedia_14.chunk_index = 473679)
-> Seq Scan on public.dbpedia_14 a (cost=0.00..62124.73 rows=630073 width=37) (actual time=0.008..163.984 rows=630073 loops=1)
Output: a.label, a.title, a.content, a.split, a.content_word_count, a.chunk, a.content_n_chunks, a.chunk_index, a.chunk_word_count, a.chunk_embedding
Planning Time: 0.129 ms
Execution Time: 3349.075 ms
```
Whereas the index is used using a **subquery** :
```sql
SELECT
a.title,
(SELECT chunk_embedding AS qvt_embedding FROM dbpedia_14 WHERE chunk_index = 473679) <-> a.chunk_embedding AS l2distance
FROM dbpedia_14 a
ORDER BY l2distance
limit 10;
```
```
Limit (cost=12603.37..12619.43 rows=10 width=27) (actual time=15.279..15.413 rows=10 loops=1)
Output: a.title, (((InitPlan 1).col1 <-> a.chunk_embedding))
InitPlan 1
-> Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14 (cost=0.42..8.44 rows=1 width=18) (actual time=0.020..0.021 rows=1 loops=1)
Output: dbpedia_14.chunk_embedding
Index Cond: (dbpedia_14.chunk_index = 473679)
-> Index Scan using dbpedia_14_chunk_embedding_idx on public.dbpedia_14 a (cost=12594.93..1024652.46 rows=630073 width=27) (actual time=15.278..15.410 rows=10 loops=1)
Output: a.title, ((InitPlan 1).col1 <-> a.chunk_embedding)
Order By: (a.chunk_embedding <-> (InitPlan 1).col1)
Planning Time: 0.127 ms
Execution Time: 15.436 ms
```
Test on 0.80.0 pgvector and postgreSQL 15 (windows)
Test on 0.80.0 pgvector and postgreSQL 17 (linux/docker)