pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
8+ messages / 4 participants
[nested] [flat]
* [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-16 07:18 "huaimin016 (@huaimin016)" <noreply+huaimin016@github.com>
0 siblings, 0 replies; 8+ messages in thread
From: huaimin016 (@huaimin016) @ 2024-05-16 07:18 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hello, I found that after creating the hnsw index, adding another piece of data is much slower than before.\
Here's what I did:
1. create table by `CREATE TABLE images (id bigserial PRIMARY KEY, url text, embedding vector(512))`
2. insert 1M data
3. create hnsw index by `CREATE INDEX ON images USING hnsw (embedding vector_cosine_ops)`
After these operations, I added data one by one, adding 100 items, and found that the average adding time was 5-8s. The adding speed before was very fast. Although there was no timer, it was definitely at the millisecond level.
And one more quesetion, when I create hnsw index, query data will also mush slower than before.
So is there a way to solve these two problems? Thanks
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-16 15:18 "jkatz (@jkatz)" <noreply+jkatz@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: jkatz (@jkatz) @ 2024-05-16 15:18 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@huaimin016 How are you creating the data? Is it INSERTing 100 times, or using a bulk insert, e.g.
```sql
INSERT INTO ... VALUES
(row1),
(row2),
...
```
What are your index build parameters? For your "slow queries" what is your query and what is your explain plan?
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-17 03:19 "huaimin016 (@huaimin016)" <noreply+huaimin016@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: huaimin016 (@huaimin016) @ 2024-05-17 03:19 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@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.
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-17 11:33 "jkatz (@jkatz)" <noreply+jkatz@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: jkatz (@jkatz) @ 2024-05-17 11:33 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@huaimin016 Using `INSERT` for this operation will be slow. You should use `COPY`, and specifically, `BINARY` mode with `COPY` to avoid the overhead of converting from binary => text => binary.
Here is an example of how to do that, and its benefits: https://github.com/erikbern/ann-benchmarks/pull/488/files
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-19 17:35 "ankane (@ankane)" <noreply+ankane@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: ankane (@ankane) @ 2024-05-19 17:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @huaimin016, I haven't seen / am not sure how to reproduce that behavior. Can you paste the output of `EXPLAIN ANALYZE` for the `SELECT` and `INSERT` statements (note that it will perform the insert)? Also, have you tried [tuning the instance](https://github.com/pgvector/pgvector?tab=readme-ov-file#tuning)?
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-05-24 13:07 "ankane (@ankane)" <noreply+ankane@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: ankane (@ankane) @ 2024-05-24 13:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-07-29 02:00 "Essence9999 (@Essence9999)" <noreply+Essence9999@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: Essence9999 (@Essence9999) @ 2024-07-29 02:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@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
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower
@ 2024-07-29 03:09 "huaimin016 (@huaimin016)" <noreply+huaimin016@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: huaimin016 (@huaimin016) @ 2024-07-29 03:09 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@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.
^ permalink raw [nested|flat] 8+ messages in thread
end of thread, other threads:[~2024-07-29 03:09 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-05-16 07:18 [pgvector/pgvector] issue #559: Adding data after building hnsw index is much slower "huaimin016 (@huaimin016)" <noreply+huaimin016@github.com>
2024-05-16 15:18 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-05-17 03:19 ` "huaimin016 (@huaimin016)" <noreply+huaimin016@github.com>
2024-05-17 11:33 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-05-19 17:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-05-24 13:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-07-29 02:00 ` "Essence9999 (@Essence9999)" <noreply+Essence9999@github.com>
2024-07-29 03:09 ` "huaimin016 (@huaimin016)" <noreply+huaimin016@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