pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes?
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes?
@ 2024-10-28 17:41 "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: kinghuang (@kinghuang) @ 2024-10-28 17:41 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
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?
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes?
@ 2024-10-29 04:08 "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2024-10-29 04:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @kinghuang, the planner should use the index if you add a `LIMIT` to the query ([docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#why-isnt-a-query-using-an-index)).
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes?
@ 2024-10-29 04:08 "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2024-10-29 04:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 3+ messages in thread
end of thread, other threads:[~2024-10-29 04:08 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-10-28 17:41 [pgvector/pgvector] issue #703: Best practices to encourage query planner to use vector indexes? "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
2024-10-29 04:08 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-29 04:08 ` "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