pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feedFrom: 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: Mon, 29 Jul 2024 03:09:31 +0000
Message-ID: <gh-pgvector-pgvector-559-c2254857617@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.
>
> I had the same problem. I want to know how did you solve it? thank you
I deployed pg on a local device. I replaced it with a hard drive with faster read/write speed and the problem did not reoccur. Therefore, I think it is likely that the disk read/write speed limits the performance in this area.
view thread (8+ messages)
Message-ID: <gh-pgvector-pgvector-559-c2254857617@github.com>
Permalink: ../gh-pgvector-pgvector-559-c2254857617@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