From: kinghuang (@kinghuang) <noreply+kinghuang@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes?
Date: Mon, 28 Oct 2024 17:41:02 +0000
Message-ID: <gh-pgvector-pgvector-703@github.com> (raw)
I have a table with 1.6 million rows and a `label_embedding` column with vector(1536) values. With the following query, the query planner always chooses a sequential scan over using a HNSW index.
<details>
<summary>Original Query (~75 seconds; cost=96282.11..97629.97)</summary>
```sql
select label, cs.dist as dist
from topic
cross join lateral (select "label_embedding" <=> ((select label_embedding from topic where label = 'Example')) dist) as cs
where cs.dist < 0.4
order by cs.dist
```
**Query Plan:**
```
Sort (cost=96282.11..97629.97 rows=539145 width=25)
Sort Key: ((topic.label_embedding <=> $0))
InitPlan 1 (returns $0)
-> Index Scan using topic_label_d63051e4_like on topic topic_1 (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
InitPlan 2 (returns $1)
-> Index Scan using topic_label_d63051e4_like on topic topic_2 (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
-> Seq Scan on topic (cost=0.00..44949.37 rows=539145 width=25)
Filter: ((label_embedding <=> $1) < '0.4'::double precision)
```
</details>
If I remove the where clause, the index is used.
<details>
<summary>Query Without Where Clause (~100 milliseconds; cost=127.79..108798.91)</summary>
```sql
select label, cs.dist as dist
from topic
cross join lateral (select "label_embedding" <=> ((select label_embedding from topic where label = 'Example')) dist) as cs
order by cs.dist
```
**Query Plan:**
```
Index Scan using topic_label_embed_idx on topic (cost=127.79..108798.91 rows=1617434 width=25)
Order By: (label_embedding <=> $0)
InitPlan 1 (returns $0)
-> Index Scan using topic_label_d63051e4_like on topic topic_1 (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
JIT:
Functions: 8
Options: Inlining false, Optimization false, Expressions true, Deforming true
```
</details>
I can rewrite the original query to materialize a CTE without filtering for `cs_dist < 0.4` and then do that separately.
<details>
<summary>Query with CTE Materialized (~100 milliseconds; cost=108798.91..145191.18)</summary>
```sql
with candidates as materialized (
select label, cs.dist as cs_dist
from topic
cross join lateral (select label_embedding <=> ((select label_embedding from topic where label = 'Example')) dist) as cs
order bycs.dist
)
select * from candidates
where cs_dist < 0.4
```
**Query Plan:**
```
CTE Scan on candidates (cost=108798.91..145191.18 rows=539145 width=524)
Filter: (cs_dist < '0.4'::double precision)
CTE candidates
-> Index Scan using topic_label_embed_idx on topic topic_1 (cost=127.79..108798.91 rows=1617434 width=25)
Order By: (label_embedding <=> $0)
InitPlan 1 (returns $0)
-> Index Scan using topic_label_d63051e4_like on topic (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
JIT:
Functions: 10
Options: Inlining false, Optimization false, Expressions true, Deforming true
```
</details>
And, for reference, here's the query plan without explicitly materializing the `candidates` CTE.
<details>
<summary>Query with CTE not Materialized (~77 seconds; cost=96282.11..97629.97)</summary>
```sql
with candidates as (
select label, cs.dist as cs_dist
from topic
cross join lateral (select label_embedding <=> ((select label_embedding from topic where label = 'Example')) dist) as cs
order bycs.dist
)
select * from candidates
where cs_dist < 0.4
```
**Query Plan:**
```
Sort (cost=96282.11..97629.97 rows=539145 width=25)
Sort Key: ((topic.label_embedding <=> $0))
InitPlan 1 (returns $0)
-> Index Scan using topic_label_d63051e4_like on topic topic_1 (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
InitPlan 2 (returns $1)
-> Index Scan using topic_label_d63051e4_like on topic topic_2 (cost=0.43..2.65 rows=1 width=18)
Index Cond: ((label)::text = 'Example'::text)
-> Seq Scan on topic (cost=0.00..44949.37 rows=539145 width=25)
Filter: ((label_embedding <=> $1) < '0.4'::double precision)
```
</details>
I've read through #133 and #152, and I can get the original query to use the vector index if I deliberately set `random_page_cost` to an artificially low value like `0.8`. I've also seen suggestions about changing the storage for the vector column to `PLAIN` so that it's not toasted. What's considered the best practice? Are there other settings I should be looking at?