pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: huaimin016 (@huaimin016) <noreply+huaimin016@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
Date: Fri, 17 May 2024 03:19:39 +0000
Message-ID: <gh-pgvector-pgvector-559-c2116554874@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-559@github.com>
References: <gh-pgvector-pgvector-559@github.com>

Hi, 1M data is divided into 10 chunks, and 100,000 chunk size are inserted at a time. Here is the code:
```
def insert_data():
    path = "/mnt/workspace/web_data"
    web_data = np.load(
        os.path.join(path, "video_half_dataset_visual_features.npy"), mmap_mode="r"
    )
    num_of_chunk = 10
    chunk_size = 100000
    for i in range(num_of_chunk):
        start = i * chunk_size
        end = (i + 1) * chunk_size
        subset_data = web_data[start:end]
        url_data = pd.read_csv(os.path.join(path, "video_half_dataset.csv"), skiprows=start, nrows=chunk_size)
        # subset_data = np.random.rand(1, 512)
        sql = "INSERT INTO images (url, embedding) VALUES "
        sql_list = []
        for j in range(chunk_size):
            print(f"chunk {i}, row {j}")
            tmp = (
                f"('{url_data.iloc[j].iloc[5]}', '["
                + ",".join([str(e) for e in subset_data[j]])
                + "]')"
            )
            sql_list.append(tmp)
        # print(sql + ",".join(sql_list))
        start_time = time.time()
        conn.execute(sql + ",".join(sql_list))
        end_time = time.time()
        print(f"chunk {i} inserted, Time used: {end_time - start_time}")
```

create index code is:
```
def create_index():
    start_time = time.time()
    conn.execute("CREATE INDEX ON images USING hnsw (embedding vector_cosine_ops)")
    end_time = time.time()
    print("Time used:", end_time - start_time)
```

I don't give any parameters, just default.
When creating the index, the search code is:
```
sql = f"SELECT * FROM images ORDER BY embedding <=> '{str_embedding}' LIMIT 10;"
start_time = time.time()
res = conn.execute(sql)
end_time = time.time()
print(f"Time used: {end_time - start_time}, result:")
```
When the same code is not indexed, the search time is ms, but when indexing is creating, the search time is seconds.

And after I create index, I insert data one by one
```
INSERT INTO images (url, embedding) VALUES (xxx, xxxx)
INSERT INTO images (url, embedding) VALUES (xxx, xxxx)
...

```
And add one data need 5~8 seconds.




view thread (8+ messages)  latest in thread

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