pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
@ 2024-12-30 14:34 "olad32 (@olad32)" <noreply+olad32@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: olad32 (@olad32) @ 2024-12-30 14:34 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi, i'm not a postgresql expert but i want to know if the following behavior is expected from the query planner/optimizer : 

I have a simple table containing vector and raw text chunk (no HNSW index)
`CREATE TABLE IF NOT EXISTS embeddings (
                    id SERIAL PRIMARY KEY,
                    chunk TEXT NOT NULL,
                    embedding vector(1536) NOT NULL
                )`
I inserted around 600k rows in it. 

In order to retrieve relevant shunk semantically, i first naively submit this query : 
`SELECT 
		id,
                chunk,
		RANK () OVER (ORDER BY embedding <=> %s::vector) AS rank,
	FROM embeddings
	ORDER BY rank
	LIMIT 10`
this gives me 7s response time, but before going through HNSW index stuff, i tried this instead (**without chunk being selected)** : 
`SELECT 
		id,
                -- chunk, 
		RANK () OVER (ORDER BY embedding <=> %s::vector) AS rank,
	FROM embeddings
	ORDER BY rank
	LIMIT 10`
and i get below 1.5s response time

so i ended up with this solution :
`WITH vector_search AS (
	SELECT 
		id,
		RANK () OVER (ORDER BY embedding <=> %s::vector) AS rank,
	FROM embeddings
	ORDER BY rank
	LIMIT 10
)
SELECT vector_search.id, embeddings.chunk
FROM vector_search LEFT JOIN embeddings ON vector_search.id = embeddings.id;`
which thanksfully gives the same response time 1.5s

Is this behavior expected from the query planner ? isn't the query planner able to optimize the original query (without WITH) ?

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
@ 2024-12-30 14:39 ` "olad32 (@olad32)" <noreply+olad32@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: olad32 (@olad32) @ 2024-12-30 14:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

**Original query plan :** 
"Limit  (cost=280819.39..280820.75 rows=10 width=1132) (actual time=6371.982..7093.264 rows=10 loops=1)"
"  ->  WindowAgg  (cost=280819.39..350050.26 rows=509776 width=1132) (actual time=6371.980..7093.261 rows=10 loops=1)"
"        ->  Gather Merge  (cost=280819.39..341129.18 rows=509776 width=1124) (actual time=6371.968..7093.238 rows=10 loops=1)"
"              Workers Planned: 3"
"              Workers Launched: 3"
"              ->  Sort  (cost=279819.35..280230.46 rows=164444 width=1124) (actual time=6274.350..6274.360 rows=42 loops=4)"
"                    Sort Key: ((embedding <=> 'OMITTED'::vector))"
"                    Sort Method: external merge  Disk: 139872kB"
"                    Worker 0:  Sort Method: external merge  Disk: 136896kB"
"                    Worker 1:  Sort Method: external merge  Disk: 141056kB"
"                    Worker 2:  Sort Method: external merge  Disk: 148240kB"
"                    ->  Parallel Seq Scan on embeddings  (cost=0.00..103697.55 rows=164444 width=1124) (actual time=0.097..1535.580 rows=127464 loops=4)"
"Planning Time: 0.085 ms"
"Execution Time: 7115.736 ms"

**Final query plan :** 
"Nested Loop Left Join  (cost=122882.31..122967.80 rows=10 width=1519) (actual time=1493.491..1498.082 rows=10 loops=1)"
"  ->  Limit  (cost=122881.89..122883.30 rows=10 width=28) (actual time=1493.458..1497.995 rows=10 loops=1)"
"        ->  WindowAgg  (cost=122881.89..194661.64 rows=509776 width=28) (actual time=1493.457..1497.992 rows=10 loops=1)"
"              ->  Gather Merge  (cost=122881.89..183191.68 rows=509776 width=30) (actual time=1493.448..1497.974 rows=10 loops=1)"
"                    Workers Planned: 3"
"                    Workers Launched: 3"
"                    ->  Sort  (cost=121881.85..122292.96 rows=164444 width=30) (actual time=1479.872..1479.965 rows=813 loops=4)"
"                          Sort Key: ((embeddings_1.embedding <=> 'OMITED'::vector))"
"                          Sort Method: external merge  Disk: 5240kB"
"                          Worker 0:  Sort Method: external merge  Disk: 5088kB"
"                          Worker 1:  Sort Method: external merge  Disk: 4832kB"
"                          Worker 2:  Sort Method: external merge  Disk: 4840kB"
"                          ->  Parallel Seq Scan on embeddings embeddings_1  (cost=0.00..103697.55 rows=164444 width=30) (actual time=0.090..1416.149 rows=127464 loops=4)"
"  ->  Index Scan using embeddings_pkey on embeddings  (cost=0.42..8.44 rows=1 width=1503) (actual time=0.007..0.007 rows=1 loops=10)"
"        Index Cond: (id = embeddings_1.id)"
"Planning Time: 0.327 ms"
"Execution Time: 1499.332 ms"


^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
@ 2024-12-30 16:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-12-30 16:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @olad32, as far as I know, Postgres isn't able to internally execute the first query like the second (delay retrieving `chunk` until after the sort + limit when doing a seq scan). This behavior won't be specific to pgvector.

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
@ 2024-12-30 16:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-12-30 16:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2024-12-30 16:25 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-12-30 14:34 [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index) "olad32 (@olad32)" <noreply+olad32@github.com>
2024-12-30 14:39 ` "olad32 (@olad32)" <noreply+olad32@github.com>
2024-12-30 16:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-12-30 16:25 ` "ankane (@ankane)" <noreply+ankane@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