From: idobenamram (@idobenamram) <noreply+idobenamram@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
Date: Mon, 14 Apr 2025 14:31:47 +0000
Message-ID: <gh-pgvector-pgvector-821-c2801919031@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-821@github.com>
References: <gh-pgvector-pgvector-821@github.com>
here is the query i used, the only difference is the table i query and the way i cast
```
explain (analyze, buffers) WITH query_embeddings AS (
SELECT ordinality AS query_id,
embedding
FROM unnest(array[
'[...]'::halfvec
]) WITH ORDINALITY AS q(embedding, ordinality)
)
SELECT qe.query_id,
sp.product_id,
sp.similarity
FROM query_embeddings qe,
LATERAL (
SELECT product_id,
(embedding <#> qe.embedding) AS similarity
FROM embeddings_table_half
WHERE
network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'
ORDER BY embedding <#> qe.embedding ASC
LIMIT 5
) sp;
```
explain output for half
```
Nested Loop (cost=300.98..301.05 rows=5 width=32) (actual time=1.049..1.053 rows=5 loops=1)
Buffers: shared hit=273
-> Function Scan on unnest q (cost=0.00..0.01 rows=1 width=40) (actual time=0.008..0.009 rows=1 loops=1)
-> Limit (cost=300.97..300.99 rows=5 width=24) (actual time=1.037..1.038 rows=5 loops=1)
Buffers: shared hit=273
-> Sort (cost=300.97..303.19 rows=885 width=24) (actual time=1.035..1.036 rows=5 loops=1)
Sort Key: ((embeddings_table_half.embedding <#> q.embedding))
Sort Method: top-N heapsort Memory: 25kB
Buffers: shared hit=273
-> Seq Scan on embeddings_table_half (cost=0.00..286.27 rows=885 width=24) (actual time=0.015..0.906 rows=885 loops=1)
Filter: (network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'::uuid)
Buffers: shared hit=273
Planning:
Buffers: shared hit=1
Planning Time: 0.301 ms
Execution Time: 1.088 ms
```
explain output for vector
```
Nested Loop (cost=20.08..31.40 rows=5 width=32) (actual time=0.681..0.731 rows=5 loops=1)
Buffers: shared hit=410
-> Function Scan on unnest q (cost=0.00..0.01 rows=1 width=40) (actual time=0.007..0.008 rows=1 loops=1)
-> Limit (cost=20.08..31.29 rows=5 width=32) (actual time=0.671..0.718 rows=5 loops=1)
Buffers: shared hit=410
-> Index Scan using embeddings_table_1c81823c_embedding_idx on embeddings_table_1c81823c embeddings_table (cost=20.08..2449.81 rows=1084 width=32) (actual time=0.668..0.714 rows=5 loops=1)
Order By: (embedding <#> q.embedding)
Filter: (network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'::uuid)
Buffers: shared hit=410
Planning:
Buffers: shared hit=1
Planning Time: 0.209 ms
Execution Time: 0.764 ms
```
again the only difference between the tables is the embedding column is halfvec instead of vector. same indices, same everything.
you can achieve the same result with a simpler knn query like:
```
select product_id, -1 * (embedding <#> $1) as similarity, from embeddings_table
where network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'
order by embedding <#> $1 asc
limit 5
```
these checks were done by explicitly passing a vector like `'[floats...]::halfvec/vector'`