From: jeonghyeon00 (@jeonghyeon00) <noreply+jeonghyeon00@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #768: pgvector not using index on 0.8.0 but using on 0.7.0
Date: Sun, 26 Jan 2025 14:48:42 +0000
Message-ID: <gh-pgvector-pgvector-768@github.com> (raw)
This query used index in postgresql 16.3, pgvector 0.7.0.
I've updated to postgresql 17.2, pgvector 0.8.0. After update, it doesn't use index.
What can i do to use index?
Index is ivfflat index.
Query
`SELECT ad_product_vector_b.id
FROM ad_product_vector_b
JOIN ad_product ap ON ap.id = ad_product_vector_b.id and ap.deleted_at is null
JOIN ad_brand ab on ap.ad_brand_id = ab.id
WHERE ap.category1 = 'TOP'
and is_display is true
and ad_platform = 'OWN_STORE'
ORDER BY vector <=>
'[0]'
LIMIT 9 OFFSET 0`
Before
`Limit (cost=577.51..10717.72 rows=9 width=31) (actual time=6.559..8.242 rows=9 loops=1)
-> Nested Loop (cost=577.51..3086580.89 rows=2739 width=31) (actual time=6.558..8.239 rows=9 loops=1)
-> Nested Loop (cost=577.22..3052619.61 rows=813864 width=1078) (actual time=6.531..7.671 rows=83 loops=1)
-> Index Scan using ad_product_vector_b_vector_idx on ad_product_vector_b (cost=576.79..6868.51 rows=1335838 width=1055) (actual time=6.471..6.593 rows=85 loops=1)
" Order By: (vector <=> '[0]'::halfvec)"
Filter: (is_display IS TRUE)
-> Index Scan using ad_product_pkey on ad_product ap (cost=0.43..2.28 rows=1 width=46) (actual time=0.012..0.012 rows=1 loops=85)
Index Cond: ((id)::text = (ad_product_vector_b.id)::text)
Filter: ((deleted_at IS NULL) AND ((category1)::text = 'TOP'::text))
Rows Removed by Filter: 0
-> Memoize (cost=0.30..1.52 rows=1 width=23) (actual time=0.006..0.006 rows=0 loops=83)
Cache Key: ap.ad_brand_id
Cache Mode: logical
Hits: 8 Misses: 75 Evictions: 0 Overflows: 0 Memory Usage: 7kB
-> Index Scan using ad_brand_pkey on ad_brand ab (cost=0.29..1.51 rows=1 width=23) (actual time=0.006..0.006 rows=0 loops=75)
Index Cond: ((id)::text = (ap.ad_brand_id)::text)
Filter: ((ad_platform)::text = 'OWN_STORE'::text)
Rows Removed by Filter: 1
Planning Time: 0.820 ms
Execution Time: 8.503 ms
`
The table name is different, but it has same data and same index.
After
`Limit (cost=70951.93..70951.94 rows=1 width=70) (actual time=42893.725..42893.839 rows=9 loops=1)
-> Sort (cost=70951.93..70951.94 rows=1 width=70) (actual time=42893.724..42893.836 rows=9 loops=1)
" Sort Key: ((ad_product_vector_a.vector <=> '[0]'::halfvec))"
Sort Method: top-N heapsort Memory: 26kB
-> Nested Loop (cost=1077.76..70951.92 rows=1 width=70) (actual time=6.070..42810.151 rows=90920 loops=1)
-> Gather (cost=1077.33..70943.47 rows=1 width=62) (actual time=5.466..537.288 rows=92318 loops=1)
Workers Planned: 1
Workers Launched: 1
-> Nested Loop (cost=77.33..69943.37 rows=1 width=62) (actual time=7.531..8224.772 rows=46159 loops=2)
-> Parallel Index Scan using ad_brand_pkey on ad_brand ab (cost=0.41..8373.98 rows=9 width=62) (actual time=2.026..76.573 rows=53 loops=2)
Filter: ((ad_platform)::text = 'OWN_STORE'::text)
Rows Removed by Filter: 15854
-> Bitmap Heap Scan on ad_product ap (cost=76.92..6841.03 rows=1 width=124) (actual time=2.801..153.599 rows=871 loops=106)
Recheck Cond: ((ad_brand_id)::text = (ab.id)::text)
Filter: ((deleted_at IS NULL) AND ((category1)::text = 'TOP'::text))
Rows Removed by Filter: 1888
Heap Blocks: exact=4468
-> Bitmap Index Scan on ad_product_ad_brand_id_index (cost=0.00..76.92 rows=1951 width=0) (actual time=1.509..1.509 rows=3152 loops=106)
Index Cond: ((ad_brand_id)::text = (ab.id)::text)
-> Index Scan using ad_product_vector_a_pkey on ad_product_vector_a (cost=0.43..8.45 rows=1 width=94) (actual time=0.455..0.455 rows=1 loops=92318)
Index Cond: ((id)::text = (ap.id)::text)
Filter: (is_display IS TRUE)
Rows Removed by Filter: 0
Planning Time: 0.303 ms
Execution Time: 42893.899 ms
`