pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feedFrom: Jontpan (@Jontpan) <noreply+Jontpan@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #662: Normal filtering doesn't seem to use normal index
Date: Tue, 27 Aug 2024 20:31:23 +0000
Message-ID: <gh-pgvector-pgvector-662@github.com> (raw)
Hi,
I have a use case which might be a bit unique. I am not using any index on the vector column, since for my use case I will filter out almost all rows anyways, and the performance benefit will be small.
The problem I am having is with the query planner grossly underestimating the performance of index searching over sequential. I am asking here only because there might be something with the size of the vectors which leads to the underestimation, otherwise I am fully aware that this is a common issue in postgres (but all common solutions doesn't seem to apply).
The table(s):
```
postgres=# \d info_blob_chunks
Table "public.info_blob_chunks"
Column | Type | Collation | Nullable | Default
--------------+--------------------------+-----------+----------+-------------------
text | character varying | | not null |
chunk_no | integer | | not null |
embedding | vector | | not null |
info_blob_id | uuid | | not null |
id | uuid | | not null | gen_random_uuid()
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
size | integer | | not null |
tenant_id | uuid | | not null |
Indexes:
"info_blob_chunks_pkey" PRIMARY KEY, btree (id)
"ix_info_blob_chunks_info_blob_id" btree (info_blob_id)
"ix_info_blob_chunks_tenant_id" btree (tenant_id)
Foreign-key constraints:
"info_blob_chunks_info_blobs_fkey" FOREIGN KEY (info_blob_id) REFERENCES info_blobs(id) ON DELETE CASCADE
"info_blob_chunks_tenants_fkey" FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
postgres=# \d info_blobs
Table "public.info_blobs"
Column | Type | Collation | Nullable | Default
--------------------+--------------------------+-----------+----------+-------------------
title | character varying | | |
url | character varying | | |
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
text | character varying | | not null |
size | integer | | not null |
id | uuid | | not null | gen_random_uuid()
group_id | uuid | | |
embedding_model_id | uuid | | |
website_id | uuid | | |
user_id | uuid | | not null |
tenant_id | uuid | | not null |
Indexes:
"info_blobs_pkey" PRIMARY KEY, btree (id)
"ix_info_blobs_group_id" btree (group_id)
"ix_info_blobs_user_id" btree (user_id)
"ix_info_blobs_website_id" btree (website_id)
Foreign-key constraints:
"info_blobs_embedding_model_id_fkey" FOREIGN KEY (embedding_model_id) REFERENCES embedding_models(id) ON DELETE SET NULL
"info_blobs_groups_fkey" FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE
"info_blobs_tenants_fkey" FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
"info_blobs_users_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
"info_blobs_website_id_fkey" FOREIGN KEY (website_id) REFERENCES websites(id) ON DELETE CASCADE
Referenced by:
TABLE "info_blob_chunks" CONSTRAINT "info_blob_chunks_info_blobs_fkey" FOREIGN KEY (info_blob_id) REFERENCES info_blobs(id) ON DELETE CASCADE
TABLE "info_blob_references" CONSTRAINT "info_blob_references_info_blobs_fkey" FOREIGN KEY (info_blob_id) REFERENCES info_blobs(id) ON DELETE CASCADE
```
the query:
```
set enable_seqscan = on;
explain (analyze, buffers) SELECT * FROM info_blob_chunks ic, info_blobs ib
where ib.website_id = '83134042-0e0e-478b-b679-fa872120e100' and ib.id = ic.info_blob_id
order by ic.embedding <-> '[1, 2, ..., 3]'
limit 30
```
the plan:
```
"Limit (cost=775733.05..775736.55 rows=30 width=977) (actual time=45623.467..45633.459 rows=30 loops=1)"
" Buffers: shared hit=304285 read=768573"
" -> Gather Merge (cost=775733.05..791118.03 rows=131862 width=977) (actual time=45444.785..45454.771 rows=30 loops=1)"
" Workers Planned: 2"
" Workers Launched: 2"
" Buffers: shared hit=304285 read=768573"
" -> Sort (cost=774733.03..774897.86 rows=65931 width=977) (actual time=45394.928..45394.956 rows=23 loops=3)"
" Sort Key: (((ic.embedding)::vector(512) <-> '[1, 2, ..., 3]'::vector))"
" Sort Method: top-N heapsort Memory: 78kB"
" Buffers: shared hit=304285 read=768573"
" Worker 0: Sort Method: top-N heapsort Memory: 85kB"
" Worker 1: Sort Method: top-N heapsort Memory: 79kB"
" -> Parallel Hash Join (cost=4026.22..772785.79 rows=65931 width=977) (actual time=159.539..45365.266 rows=27721 loops=3)"
" Hash Cond: (ic.info_blob_id = ib.id)"
" Buffers: shared hit=304274 read=768570"
" -> Parallel Seq Scan on info_blob_chunks ic (cost=0.00..762455.63 rows=2275763 width=530) (actual time=152.812..40830.112 rows=1804381 loops=3)"
" Buffers: shared read=739698"
" -> Parallel Hash (cost=4013.00..4013.00 rows=1058 width=439) (actual time=6.056..6.068 rows=569 loops=3)"
" Buckets: 2048 Batches: 1 Memory Usage: 784kB"
" Buffers: shared hit=1 read=357"
" -> Parallel Bitmap Heap Scan on info_blobs ib (cost=30.22..4013.00 rows=1058 width=439) (actual time=0.795..15.385 rows=1706 loops=1)"
" Recheck Cond: (website_id = '83134042-0e0e-478b-b679-fa872120e100'::uuid)"
" Heap Blocks: exact=355"
" Buffers: shared hit=1 read=357"
" -> Bitmap Index Scan on ix_info_blobs_website_id (cost=0.00..29.77 rows=1798 width=0) (actual time=0.533..0.537 rows=1706 loops=1)"
" Index Cond: (website_id = '83134042-0e0e-478b-b679-fa872120e100'::uuid)"
" Buffers: shared read=3"
"Planning:"
" Buffers: shared hit=3 read=14"
"Planning Time: 1.622 ms"
"JIT:"
" Functions: 37"
" Options: Inlining true, Optimization true, Expressions true, Deforming true"
" Timing: Generation 6.615 ms, Inlining 156.776 ms, Optimization 291.671 ms, Emission 188.550 ms, Total 643.612 ms"
"Execution Time: 45637.132 ms"
```
The plan with `set enable_seq_scan = off;`
```
"Limit (cost=922690.41..922693.91 rows=30 width=977) (actual time=3951.426..3962.398 rows=30 loops=1)"
" Buffers: shared hit=371394 read=40108"
" -> Gather Merge (cost=922690.41..938075.39 rows=131862 width=977) (actual time=3769.332..3780.299 rows=30 loops=1)"
" Workers Planned: 2"
" Workers Launched: 2"
" Buffers: shared hit=371394 read=40108"
" -> Sort (cost=921690.39..921855.22 rows=65931 width=977) (actual time=3720.676..3720.688 rows=23 loops=3)"
" Sort Key: (((ic.embedding)::vector(512) <-> '[1, 2, ..., 3]'::vector))"
" Sort Method: top-N heapsort Memory: 77kB"
" Buffers: shared hit=371394 read=40108"
" Worker 0: Sort Method: top-N heapsort Memory: 77kB"
" Worker 1: Sort Method: top-N heapsort Memory: 82kB"
" -> Nested Loop (cost=7.79..919743.15 rows=65931 width=977) (actual time=130.508..3692.147 rows=27721 loops=3)"
" Buffers: shared hit=371380 read=40108"
" -> Parallel Index Scan using info_blobs_pkey on info_blobs ib (cost=0.41..86053.75 rows=749 width=439) (actual time=128.323..675.461 rows=569 loops=3)"
" Filter: (website_id = '83134042-0e0e-478b-b679-fa872120e100'::uuid)"
" Rows Removed by Filter: 21289"
" Buffers: shared hit=60678 read=5313"
" -> Bitmap Heap Scan on info_blob_chunks ic (cost=7.37..1108.44 rows=419 width=530) (actual time=0.327..2.255 rows=49 loops=1706)"
" Recheck Cond: (info_blob_id = ib.id)"
" Heap Blocks: exact=2698"
" Buffers: shared hit=4044 read=8725"
" -> Bitmap Index Scan on ix_info_blob_chunks_info_blob_id (cost=0.00..7.27 rows=419 width=0) (actual time=0.295..0.295 rows=49 loops=1706)"
" Index Cond: (info_blob_id = ib.id)"
" Buffers: shared hit=3539 read=1582"
"Planning:"
" Buffers: shared hit=3 read=14"
"Planning Time: 2.255 ms"
"JIT:"
" Functions: 31"
" Options: Inlining true, Optimization true, Expressions true, Deforming true"
" Timing: Generation 6.589 ms, Inlining 164.223 ms, Optimization 246.281 ms, Emission 136.396 ms, Total 553.489 ms"
"Execution Time: 3966.551 ms"
```
I have limited the vectors everywhere for readability. Happy for any and all help.
view thread (3+ messages) latest in thread
Message-ID: <gh-pgvector-pgvector-662@github.com>
Permalink: ../gh-pgvector-pgvector-662@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