Add hibernate-vector module: pgvector similarity search with Hibernate 7.4.17.Final and Spring Data JPA 4.2.0 (float[] to vector/halfvec, cosine and L2 in HQL and Criteria, SearchResults methods, HNSW plans from auto_explain and five ways a query stops using the index, recall vs hnsw.ef_search, filtered-search row loss, Spring AI PgVectorStore comparison; PostgreSQL 16.15, pgvector 0.8.7)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4XrpfdpsacrvqnrBM
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
# HNSW index on 20,000 x 64-dimension rows: which Hibernate queries use it (auto_explain on the statements Hibernate really sent)
|
||||
# Hibernate ORM 7.4.17.Final (on the classpath), PostgreSQL 16.15, pgvector 0.8.7, JDK 25
|
||||
rows: 20000, dimensions: 64, clusters: 10
|
||||
|
||||
== 1. No index yet: the nearest-neighbour query reads every row
|
||||
Query Text: select c1_0.id from chunk c1_0 order by (c1_0.embedding<=>cast($1 as vector)) fetch first 10 rows only
|
||||
Limit (cost=1452.19..1452.22 rows=10 width=16) (actual time=5.744..5.747 rows=10 loops=1)
|
||||
-> Sort (cost=1452.19..1502.19 rows=20000 width=16) (actual time=5.742..5.744 rows=10 loops=1)
|
||||
Sort Key: ((embedding <=> '[<query vector>]'::vector))
|
||||
Sort Method: top-N heapsort Memory: 25kB
|
||||
-> Seq Scan on chunk c1_0 (cost=0.00..1020.00 rows=20000 width=16) (actual time=0.006..3.997 rows=20000 loops=1)
|
||||
|
||||
== 2. Create the HNSW index (the schema generator cannot: see T08)
|
||||
sql/hnsw-index.sql: create index chunk_hnsw_cos on chunk using hnsw (embedding vector_cosine_ops) with (m = 16, ef_construction = 64)
|
||||
build time (indicative, this machine): 4044 ms
|
||||
table size: 6192 kB, HNSW index size: 11 MB
|
||||
indexdef: CREATE INDEX chunk_hnsw_cos ON public.chunk USING hnsw (embedding vector_cosine_ops) WITH (m='16', ef_construction='64')
|
||||
|
||||
== 3. Same HQL, now with the index: select c.id from Chunk c order by cosine_distance(c.embedding, :q) limit 10
|
||||
Query Text: select c1_0.id from chunk c1_0 order by (c1_0.embedding<=>cast($1 as vector)) fetch first 10 rows only
|
||||
Limit (cost=167.05..171.49 rows=10 width=16) (actual time=1.037..1.052 rows=10 loops=1)
|
||||
-> Index Scan using chunk_hnsw_cos on chunk c1_0 (cost=167.05..9048.00 rows=20000 width=16) (actual time=1.036..1.050 rows=10 loops=1)
|
||||
Order By: (embedding <=> '[<query vector>]'::vector)
|
||||
|
||||
== 4. Trap: L2 distance against a cosine index: select c.id from Chunk c order by euclidean_distance(c.embedding, :q) limit 10
|
||||
Query Text: select c1_0.id from chunk c1_0 order by (c1_0.embedding<->cast($1 as vector)) fetch first 10 rows only
|
||||
Limit (cost=1452.19..1452.22 rows=10 width=16) (actual time=5.240..5.243 rows=10 loops=1)
|
||||
-> Sort (cost=1452.19..1502.19 rows=20000 width=16) (actual time=5.237..5.239 rows=10 loops=1)
|
||||
Sort Key: ((embedding <-> '[<query vector>]'::vector))
|
||||
Sort Method: top-N heapsort Memory: 25kB
|
||||
-> Seq Scan on chunk c1_0 (cost=0.00..1020.00 rows=20000 width=16) (actual time=0.006..3.476 rows=20000 loops=1)
|
||||
|
||||
== 5. Trap: a distance threshold with no ORDER BY ... LIMIT: select c.id from Chunk c where cosine_distance(c.embedding, :q) < 0.3
|
||||
Query Text: select c1_0.id from chunk c1_0 where (c1_0.embedding<=>cast($1 as vector))<0.3
|
||||
Seq Scan on chunk c1_0 (cost=0.00..1070.00 rows=6667 width=8) (actual time=0.282..6.192 rows=7 loops=1)
|
||||
Filter: ((embedding <=> '[<query vector>]'::vector) < '0.3'::double precision)
|
||||
Rows Removed by Filter: 19993
|
||||
|
||||
== 6. Trap: ordering by a similarity expression instead of the distance: select c.id from Chunk c order by 1 - cosine_distance(c.embedding, :q) desc limit 10
|
||||
Query Text: select c1_0.id from chunk c1_0 order by (1-(c1_0.embedding<=>cast($1 as vector))) desc fetch first 10 rows only
|
||||
Limit (cost=1502.19..1502.22 rows=10 width=16) (actual time=7.318..7.322 rows=10 loops=1)
|
||||
-> Sort (cost=1502.19..1552.19 rows=20000 width=16) (actual time=7.315..7.317 rows=10 loops=1)
|
||||
Sort Key: (('1'::double precision - (embedding <=> '[<query vector>]'::vector))) DESC
|
||||
Sort Method: top-N heapsort Memory: 25kB
|
||||
-> Seq Scan on chunk c1_0 (cost=0.00..1070.00 rows=20000 width=16) (actual time=0.012..4.995 rows=20000 loops=1)
|
||||
|
||||
== 7. The Criteria shape from T03 (distance in the select list, so Hibernate renders 'order by 2'): does the index still work?
|
||||
Query Text: select c1_0.id,(c1_0.embedding<=>cast($1 as vector)) from chunk c1_0 order by 2 fetch first $2 rows only
|
||||
Limit (cost=167.05..171.49 rows=10 width=16) (actual time=0.886..0.923 rows=10 loops=1)
|
||||
-> Index Scan using chunk_hnsw_cos on chunk c1_0 (cost=167.05..9048.00 rows=20000 width=16) (actual time=0.884..0.920 rows=10 loops=1)
|
||||
Order By: (embedding <=> '[<query vector>]'::vector)
|
||||
|
||||
== 8. A category filter next to the ORDER BY: select c.id from Chunk c where c.category = 3 order by cosine_distance(c.embedding, :q) limit 10
|
||||
Query Text: select c1_0.id from chunk c1_0 where c1_0.category=3 order by (c1_0.embedding<=>cast($1 as vector)) fetch first 10 rows only
|
||||
Limit (cost=167.05..211.48 rows=10 width=16) (actual time=0.947..0.986 rows=6 loops=1)
|
||||
-> Index Scan using chunk_hnsw_cos on chunk c1_0 (cost=167.05..9053.00 rows=2000 width=16) (actual time=0.946..0.983 rows=6 loops=1)
|
||||
Order By: (embedding <=> '[<query vector>]'::vector)
|
||||
Filter: (category = 3)
|
||||
Rows Removed by Filter: 34
|
||||
Reference in New Issue
Block a user