From: jkatz (@jkatz) <noreply+jkatz@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
Date: Thu, 03 Oct 2024 13:54:14 +0000
Message-ID: <gh-pgvector-pgvector-690-c2391488825@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-690@github.com>
References: <gh-pgvector-pgvector-690@github.com>
For 1, IVFFlat index sizes are effectively `2 * sizeof(vectors in table) + (nlists * sizeof(vector))`. HNSW sizes are impacted by `m`, as `m` probabilistically dictates the number of levels and the rough distribution of how many vectors are in each level.
Consider [this script][1] that emulates how level placement occurs (though note that L0 is an underestimate as it should include all vectors):
```python
from collections import defaultdict
import math
import random
def generate_hnsw_levels(n, m):
ml = 1 / math.log(m)
levels = defaultdict(int)
for _ in range(n):
level = math.floor(-math.log(random.random()) * ml);
levels[level] += 1
return levels
def print_levels(levels):
for k, v in sorted(levels.items()):
print(k, v)
levels = generate_hnsw_levels(1_000_000, 24)
print_levels(levels)
```
Here is the output for the 1M vectors and m=16/24 cases:
```
m=16
0 937392
1 58649
2 3705
3 240
4 13
5 1
```
```
m=24
0 958595
1 39709
2 1619
3 74
4 2
5 1
```
For m=16, there are ~62K vectors in the upper layers of the graph, whereas m=24 has 41K vectors. Given all vectors are in L0, this means m=24 will have a smaller index overall, and could have a size that's comparable to an IVFFlat index. However, under the default pgvector settings of m=16, using the full, unquantized vectors typically yields an index size that's ~1.3x larger than the vectors stored in the table.
[1]: https://gist.github.com/jkatz/5c70179064bc4df92e10bfdbf9701ece