pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: olad32 (@olad32) <noreply+olad32@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #744: Response time rise when retrieving raw text associated with embedding (without index)
Date: Mon, 30 Dec 2024 14:34:19 +0000
Message-ID: <gh-pgvector-pgvector-744@github.com> (raw)

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) ?

view thread (4+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-744@github.com>
Permalink:  ../gh-pgvector-pgvector-744@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