pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: hlinnaka (@hlinnaka) <noreply+hlinnaka@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #282: Hnsw iterator
Date: Fri, 29 Sep 2023 16:00:43 +0000
Message-ID: <gh-pgvector-pgvector-282-rc1341552666@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-282-rc1341132985@github.com>
References: <gh-pgvector-pgvector-282@github.com>
	<gh-pgvector-pgvector-282-rc1341132985@github.com>

(on src/hnswutils.c:624)

> > W should use `CompareFurthestCandidates`
> 
> Actually it is not completely true. pgvector really uses reverse (distance descending) order for W, because
> 
> 1. It needs to get largest distance already selected in W
> 
> 2. Be able to replace item with largest distance with better candidate.
> 
> 
> We do not need 2 and we need to extract best candidate from W (with smallest distance). In the original pgvector implementation it is solved by extracting all elements and appending them to the list (so finally in the list them are in correct distance ascending order). But in case of iterator I need to select just one best item.

Hmm, so to implement 1, you now keep track of the maximum distance seen separately. That won't produce the same results as without this. You will almost never be able to exit the search because of the `c->distance > layer->max_distance` condition, if you define `max_distance` as "the max distance ever seen" instead of "max distance among current set W".

The search is much slower with this PR now:

```
postgres=# select id from items_small order by embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]' limit 1;
  id   
-------
 27868
(1 row)

Time: 420.587 ms
postgres=# set hnsw.enable_iterator =off;
SET
Time: 0.328 ms
postgres=# select id from items_small order by embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]' limit 1;
  id   
-------
 27868
(1 row)

Time: 3.622 ms
```
 
I think it's because of that issue with max_distance calculation, but I didn't verify it.

view thread (33+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-282-rc1341552666@github.com>
Permalink:  ../gh-pgvector-pgvector-282-rc1341552666@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