From: huaimin016 (@huaimin016) <noreply+huaimin016@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #666: search too slow when I clean the cache
Date: Thu, 05 Sep 2024 04:31:35 +0000
Message-ID: <gh-pgvector-pgvector-666@github.com> (raw)
Hello, Team!
I create table and create index like:
```
create table if not exists test_pg_hnsw (id SERIAL PRIMARY KEY, file_id bigint NOT NULL, vector vector(512) NOT NULL, start_time integer NOT NULL, end_time integer NOT NULL);
create index on test_pg_hnsw using hnsw (vector vector_cosine_ops)
```
And I insert 1M data in this table. I found that when I clear the cache, it is very slow to seach.
I use `iotop -oP` to checkout my disk read, only 14-17M/s when I search sometings.
```
61173 be/4 postgres 16.70 M/s 0.00 B/s postgres: postgres postgres 192.168.31.131(50962) SELECT
```
here is my sql, query_text_vector is 512 dimensions vector:
```
set hnsw.ef_search = 1000;
explain analyze
select
file_id
from
test_pg_hnsw
order by
vector <=> '{query_text_vector}'
limit 500;
```
response is:
```
Limit (cost=128.72..153.79 rows=500 width=16) (actual time=78338.588..83604.449 rows=500 loops=1)
-> Index Scan using test_pg_hnsw_vector_idx on test_pg_hnsw (cost=128.72..50260.72 rows=1000000 width=16) (actual time=78338.585..83603.711 rows=500 loops=1)
Order By: (vector <=> '[-0.017074585,-0.03387451,0.06323242,0.24108887,0.061706543,-0.07702637,-0.22314453,-1.4638672,-0.07965088,0.20825195,-0.19995117,-0.05380249,0.09399414,-0.06451416,0.18127441,-0.05557251,0.04119873,-0.038085938,0.16052246,-0.265625,0.47875977,0.22473145,-0.25170898,0.13879395,-0.29638672,-0.05429077,0.26293945,0.042907715,-0.095214844,0.03189087,0.39038086,0.06591797,-0.42089844,0.079589844,0.15490723,0.23376465,-0.026473999,-0.00040078163,0.008773804,0.16418457,-0.35913086,0.16369629,-0.011650085,-0.24633789,0.30371094,0.3408203,-0.29760742,0.2487793,0.14428711,-0.07269287,0.039276123,0.0519104,0.1439209,-0.29858398,-0.2446289,-0.15759277,-0.109069824,0.22045898,-0.25390625,0.01966858,0.26782227,-0.06518555,-0.1821289,-0.36376953,0.04284668,-0.09448242,0.15319824,-0.48999023,-0.14086914,-0.10418701,0.4333496,0.10852051,0.15673828,0.05432129,0.21166992,0.23754883,0.2705078,0.359375,0.16369629,-0.21862793,-0.40478516,0.16687012,0.010856628,0.4873047,-0.14575195,0.1550293,-0.1508789,-0.19934082,0.22424316,-0.031097412,-0.032440186,0.20666504,-1.7802734,0.12432861,0.38549805,0.0064430237,0.24938965,0.25024414,0.004928589,0.050476074,0.35131836,0.4248047,0.38134766,0.16906738,-0.066467285,0.019683838,0.28344727,0.03744507,-0.044891357,-0.17749023,0.1126709,0.031021118,0.2956543,0.14904785,-0.038146973,-0.24328613,0.13354492,0.3425293,-0.18603516,-0.22692871,0.19299316,-0.8901367,-0.24658203,-0.18310547,0.20959473,-0.27148438,-0.0947876,0.06951904,0.11047363,0.7949219,-0.046020508,-0.034423828,-0.072021484,7.2226562,-0.046417236,0.27490234,-0.50439453,-0.25561523,-0.14221191,0.25146484,0.0073890686,-0.30688477,0.028366089,0.045654297,-0.31762695,0.003900528,0.082458496,0.15332031,-0.39135742,-0.1842041,0.15185547,-0.20471191,-0.048614502,0.092041016,-0.04522705,-0.34887695,-0.057647705,0.044525146,-0.1394043,0.14331055,-0.33911133,-0.095947266,-0.19897461,-0.10882568,0.13452148,0.09777832,-0.20568848,-0.036499023,0.014945984,-0.13635254,0.074157715,0.16137695,-0.34350586,0.22680664,-0.14050293,0.19372559,-0.2697754,0.0124053955,0.8071289,0.124694824,0.3527832,0.2927246,0.20166016,-0.3466797,-0.10040283,-0.030349731,-0.09161377,0.09851074,0.18579102,0.1685791,-0.057922363,-0.21740723,-0.32641602,-0.13549805,-0.35375977,0.19799805,0.1508789,0.23657227,0.3737793,-0.26464844,0.08862305,-0.1126709,-0.11773682,0.28955078,0.00881958,-0.030914307,-0.056854248,-0.2277832,-0.07055664,0.16662598,0.9423828,0.4338379,0.1348877,-0.030014038,0.04046631,0.0289917,-0.060913086,-0.091918945,0.11633301,-0.43481445,-0.21875,-0.3942871,-0.0725708,-0.24255371,-0.21069336,-0.028320312,0.08929443,0.22680664,-0.4663086,0.08807373,0.14440918,-0.0002720356,0.05734253,-0.25317383,0.25634766,-0.21728516,0.24194336,0.18444824,-0.30126953,-0.17346191,0.03326416,0.26660156,-0.06518555,-0.0146102905,-0.2244873,-0.16809082,-0.03414917,0.03564453,0.09020996,-0.04425049,0.22509766,0.09387207,-0.16418457,0.37939453,-0.16711426,0.24719238,0.34057617,-0.36816406,-0.06341553,0.045074463,0.0690918,-0.26000977,0.043029785,-0.0715332,-0.06451416,0.17541504,0.090270996,-0.047790527,0.13244629,-0.15759277,0.14611816,0.1381836,-0.01537323,-0.090026855,0.09484863,-0.23449707,-0.24279785,0.03363037,-0.09490967,-0.02279663,0.16809082,-0.11755371,0.3474121,-0.13305664,-0.08380127,0.08996582,-0.2783203,-0.34423828,-0.23217773,-0.2866211,-0.025787354,-0.04321289,0.07824707,0.36376953,-0.040527344,-0.33447266,-0.11279297,-0.2758789,0.14990234,-0.17504883,-0.09387207,-0.044555664,-0.08227539,-0.28759766,-0.16455078,0.1060791,-0.19885254,0.03137207,0.28735352,0.44189453,0.07421875,0.09649658,7.2148438,0.05444336,-0.046142578,0.5126953,-0.06695557,-0.13757324,0.63134766,0.23461914,0.010017395,0.5292969,0.13464355,0.33618164,-0.08050537,-0.027496338,-0.12225342,-0.07476807,-0.04949951,-2.0625,0.1862793,0.13330078,0.11199951,-0.24499512,-0.22192383,0.09082031,0.10675049,0.34716797,-0.15429688,-0.08581543,0.14746094,-0.22875977,-0.038391113,0.010246277,0.013168335,-0.13305664,-0.076416016,0.26293945,-0.30541992,0.2993164,0.11199951,-0.11999512,0.18005371,-0.11212158,-0.06744385,0.38916016,-0.21472168,0.14624023,-0.081848145,0.052581787,0.34155273,-0.12902832,-0.0010347366,0.0692749,-0.1274414,-0.26464844,-0.0814209,-0.2512207,-0.0340271,0.111450195,-0.10296631,-0.010879517,0.33544922,0.44482422,-0.33251953,0.06088257,0.06793213,0.23828125,-0.16052246,0.04660034,-0.32373047,-0.093322754,0.11505127,-0.21020508,-0.04598999,0.064575195,0.038604736,-0.17797852,-0.2064209,0.21984863,-0.23327637,-0.01675415,0.0982666,-0.001578331,0.13928223,-0.038116455,-0.20800781,-0.051513672,-0.033355713,0.4411621,-0.05117798,0.07525635,0.18273926,0.12200928,-0.014884949,0.0871582,0.056488037,-0.0949707,-0.19348145,-0.3515625,0.21386719,0.022201538,0.10546875,0.42700195,0.09631348,0.039001465,-0.039154053,-0.035583496,0.1697998,0.052856445,0.11230469,0.0049476624,-0.08227539,0.50146484,0.27172852,0.12585449,0.26000977,-0.064331055,0.113586426,0.45166016,-0.021469116,-0.17260742,0.15478516,-0.09094238,-0.30029297,0.41796875,-0.021270752,-0.28198242,-0.35083008,-0.040100098,0.011489868,0.008369446,-0.25952148,-0.18322754,0.18334961,0.23144531,-0.29101562,-0.53271484,0.34448242,0.14892578,-0.028686523,-0.14746094,-0.021026611,0.10949707,0.03289795,0.07922363,0.19262695,0.24645996,-0.31420898,-0.05722046,0.0154953,-0.06036377,0.03125,0.07885742,0.19934082,-0.15979004,0.4675293,-0.032684326,-0.2211914,-0.30493164,-0.09210205,0.05050659,0.07946777,-0.3005371,-0.076416016,0.121276855,0.2956543,0.3544922,0.26733398,0.24780273,0.1161499,-0.35253906,0.14001465,-0.30444336,-0.3395996,0.051452637,0.017593384,0.22546387,-0.09112549,0.11151123,-0.047546387,0.26782227,-0.017318726,0.93359375,-0.0048561096,-0.03189087,0.22424316,-0.12036133,0.029083252,0.15112305,0.021911621,0.03125,0.2512207,0.38671875,0.17626953,-0.05432129,-0.029922485,-0.097229004,-0.24633789,-0.35351562,-0.28515625,-0.08068848]'::vector)
Planning Time: 18.789 ms
Execution Time: 83607.983 ms
```
So I guess pg loads the index from disk into the cache to improve search speed. After I continued to search for different content, the search speed did improve significantly and was able to reach around 100ms.
But there are not only postgres in my working environment, but also other applications. This causes the cache loaded by pg to be preempted by other applications. Once this happens, the search speed will drop sharply.
I want to keep my search speed very high, the possible ways I thought of doing this are:
1. Reduce the size of the index, such as using halfvec consine ops or binary quantization to build the index. I have tried this method, and the index is indeed much smaller. Without cache, it is indeed much faster than vector consine ops, but It takes 30 seconds at the fastest, and the recall of binary quantization is very low.
2. Is there a way to keep the loaded cache from being preempted by other applications?
3. Warmup, I have used the following configuration to warmup, and it is indeed effective, but it cannot completely warmup to 100ms search time, and there is still a situation where the cache is preempted by other applications.
Can you give me any advice or help? Thank you very much