From: kwunhang (@kwunhang) <noreply+kwunhang@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
Date: Thu, 03 Oct 2024 09:13:08 +0000
Message-ID: <gh-pgvector-pgvector-690@github.com> (raw)
1. I am testing PGVector using both ivfflat and HNSW, and I have a few questions. The testing data consists of 768-dimensional vectors using `Vector` and 2024-dimensional vectors using `halfvec`.
I checked the index size with the following code:
```sql
SELECT pg_size_pretty(pg_relation_size('index_name'));
```
I found that the index sizes of HNSW and ivfflat are very similar. For example, with 1 million data points of 768 dimensions, setting `nlist=1000` for ivfflat and `m=24`, `efConstruction=80` for HNSW, the index sizes are 3912MB and 3904MB, respectively.
May I ask why the index sizes of these two indexing methods are so similar?
2. I also have another question regarding how the index is stored in PGVector. I found issue #688 and [this article](https://lantern.dev/blog/pgvector-storage) for reference. I tried to read the source code to understand the structure, but I still have some misunderstandings.
<img width="832" alt="螢幕截圖 2024-10-03 下午5 10 53" src="https://github.com/user-attachments/assets/1e80e536-3121-4606-b745-f687dd19d44b";
When more data is inserted into the database, a HNSW graph is created, and elements are located at different levels. For example, consider an HNSW graph with 3 levels (level 0, 1, and 2) and `m=3`. For an element in level 1, my understanding is that the element would have 3 neighbors in level 1 and 6 neighbors in level 0.
In this case, how is the neighbor information stored in the page? Is there a computation of 2 neighbor arrays that are stored on the page, or is it handled in another way?
Thank you for your assistance!