From: Tibiritabara (@Tibiritabara) <noreply+Tibiritabara@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #719: SELECT WHERE ORDER BY LIMIT no results
Date: Mon, 10 Feb 2025 14:08:44 +0000
Message-ID: <gh-pgvector-pgvector-719-c2648102252@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-719@github.com>
References: <gh-pgvector-pgvector-719@github.com>
> > So, I have been able to test these settings locally. I currently have a table with vector and sparse embeddings of more than 40k records. As pointed out earlier, I was doing JSONB filtering and currently using v0.8.0.
> > After extensive testing, I was able to single out the `iterative_scan` as the most meaningful setting. When is on `strict_order`, the query still takes more than 10 secs for its execution. When using `relaxed_order` I see huge reduction in execution time, down to 600ms.
> > This is inline with the contents of your presentation [@jkatz](https://github.com/jkatz) , but I need to point out, despite using BTREE and GIN indexes on my metadata, and other filtering attributes, the performance remains poor, unless the `iterative_scan` changes ,and I use the HNSW indexes
>
> [@Tibiritabara](https://github.com/Tibiritabara) Thanks for testing, however, I'm not fully following the explanation. Do you only have 40K records total that you're searching over in the dataset? Additionally, it'd be helpful to see the query / indexes you've created / EXPLAIN plans. FWIW, in a lot of my tests, I didn't see a performance difference between strict/relaxed ordering, so I wonder if something else is happening.
Hey @jkatz thanks for coming back to me and for the support. I am still seeing performance issues when making queries. This is the current set of configurations I am doing:
```sql
SET hnsw.ef_search TO 200;
SET hnsw.iterative_scan TO 'relaxed_order';
SET hnsw.max_scan_tuples TO 20000;
SET hnsw.scan_mem_multiplier TO 2;
```
The query looks like this:
```SQL
WITH semantic_search AS (
SELECT ai.vector_store.id, ( embedding <=> ? ) AS cosine_distance
FROM ai.vector_store
WHERE ? = ? AND ( metadata ->> ? = ? or metadata ->> ? = ? )
ORDER BY cosine_distance
LIMIT ?
), sparse_search AS (
SELECT ai.vector_store.id, ( sparse <=> ? ) AS cosine_distance
FROM ai.vector_store
WHERE ? = ? AND ( metadata ->> ? = ? or metadata ->> ? = ? )
ORDER BY cosine_distance
LIMIT ?
), hybrid_search AS (
SELECT COALESCE ( semantic_search.id, sparse_search.id ) AS id, COALESCE ( ( ( ( ( ? - semantic_search.cosine_distance ) + ? ) / ? ) * ? ), ? ) + COALESCE ( ( ( ( ( ? - sparse_search.cosine_distance ) + ? ) / ? ) * ? ), ? ) AS score
FROM semantic_search
FULL OUTER JOIN sparse_search ON semantic_search.id = sparse_search.id
ORDER BY score DESC
LIMIT ?
)
SELECT *
FROM ai.vector_store
INNER JOIN hybrid_search ON ai.vector_store.id = hybrid_search.id
ORDER BY similarity DESC
LIMIT ? OFFSET ?
```
And the table looks like this:
```SQL
CREATE TABLE IF NOT EXISTS ai.vector_store
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
text character varying COLLATE pg_catalog."default" NOT NULL,
node_id character varying COLLATE pg_catalog."default",
metadata jsonb,
embedding vector(1536),
entity_id uuid NOT NULL,
similarity double precision,
created_at timestamp with time zone DEFAULT now(),
sparse sparsevec(30522),
CONSTRAINT vector_store_pkey PRIMARY KEY (id),
CONSTRAINT vector_store_id_fkey FOREIGN KEY (entity_id)
REFERENCES storage.files (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
)
CREATE INDEX IF NOT EXISTS vector_store_idx
ON ai.vector_store USING hnsw
(embedding vector_cosine_ops)
TABLESPACE pg_default;
CREATE INDEX IF NOT EXISTS vector_store_metadata_idx
ON ai.vector_store USING gin
(metadata)
TABLESPACE pg_default;
CREATE INDEX IF NOT EXISTS vector_store_sparse_idx
ON ai.vector_store USING hnsw
(sparse sparsevec_cosine_ops)
TABLESPACE pg_default;
CREATE INDEX IF NOT EXISTS vector_store_foreign_idx
ON ai.vector_store USING btree
(entity_id ASC NULLS LAST)
TABLESPACE pg_default;
```
And this is the output of the Explain analyze:
```sql
Limit (cost=13869.93..13922.56 rows=20 width=1293) (actual time=1411.403..1411.525 rows=20 loops=1)
-> Nested Loop (cost=13869.93..13948.87 rows=30 width=1293) (actual time=1411.402..1411.521 rows=20 loops=1)
-> Limit (cost=13869.52..13869.59 rows=30 width=24) (actual time=1411.386..1411.393 rows=20 loops=1)
-> Sort (cost=13869.52..13869.59 rows=30 width=24) (actual time=1411.385..1411.390 rows=20 loops=1)
Sort Key: ((COALESCE((((('1'::double precision - ((vector_store_2.embedding <=> '[...]'::vector))) + '1'::double precision) / '2'::double precision) * '0.5'::double precision), '0'::double precision) + COALESCE((((('1'::double precision - ((vector_store_1.sparse <=> '{...}/30522'::sparsevec))) + '1'::double precision) / '2'::double precision) * '0.5'::double precision), '0'::double precision))) DESC
Sort Method: quicksort Memory: 27kB
-> Hash Full Join (cost=10083.92..13868.78 rows=30 width=24) (actual time=1411.153..1411.169 rows=30 loops=1)
Hash Cond: (vector_store_1.id = vector_store_2.id)
-> Limit (cost=662.64..4446.11 rows=30 width=24) (actual time=1.760..1.761 rows=0 loops=1)
-> Index Scan using vector_store_sparse_idx on vector_store vector_store_1 (cost=662.64..59054.25 rows=463 width=24) (actual time=1.759..1.759 rows=0 loops=1)
Order By: (sparse <=> '{...}/30522'::sparsevec)
Filter: (((metadata ->> 'key'::text) = 'xxx'::text) OR ((metadata ->> 'entity_id'::text) = 'xxx'::text))
Rows Removed by Filter: 40
-> Hash (cost=9420.91..9420.91 rows=30 width=24) (actual time=1409.380..1409.382 rows=30 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 10kB
-> Limit (cost=9420.53..9420.61 rows=30 width=24) (actual time=1409.366..1409.373 rows=30 loops=1)
-> Sort (cost=9420.53..9421.69 rows=463 width=24) (actual time=1409.364..1409.368 rows=30 loops=1)
Sort Key: ((vector_store_2.embedding <=> '[...]'::vector))
Sort Method: top-N heapsort Memory: 28kB
-> Seq Scan on vector_store vector_store_2 (cost=0.00..9406.86 rows=463 width=24) (actual time=0.109..1408.807 rows=1561 loops=1)
Filter: (((metadata ->> 'key'::text) = 'xxx'::text) OR ((metadata ->> 'entity_id'::text) = 'xxx'::text))
Rows Removed by Filter: 44542
-> Index Scan using vector_store_pkey on vector_store (cost=0.41..2.63 rows=1 width=1285) (actual time=0.005..0.005 rows=1 loops=20)
Index Cond: (id = (COALESCE(vector_store_2.id, vector_store_1.id)))
Planning Time: 0.838 ms
Execution Time: 1411.618 ms
```
As seen, I built a hybrid search solution, running on the SQL server. For this hybrid search to work, we use both, dense and sparse search, and we merge the results to get a meaningful/ranked output.
I hope this helps