Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres-pgvector doesn't use the vector index #984

Open
martinloretzzz opened this issue Nov 21, 2024 · 1 comment
Open

postgres-pgvector doesn't use the vector index #984

martinloretzzz opened this issue Nov 21, 2024 · 1 comment

Comments

@martinloretzzz
Copy link

I built a project on top of the postgres-pgvector template, but the database queries were really slow. I had 2.8 million rows in the database with 512 dimensional embedding vectors. Queries took like 12 seconds. (After using an ivfflat index it took 100ms)

Reproduction repo is here: https://github.com/martinloretzzz/nextjs-drizzle-pgvector

After investigating the issue, it turns out the queries don't use the vector index, because we're looking for the 1 - cosineDistance, while the index is built for the cosine distance only.

The fix is quite simple, need to look for the smallest cosineDistance, instead of the largest 1-cosineDistance:

const similarity = sql<number>`${cosineDistance(pokemons.embedding, vectorQuery)}`
const pokemon = await db
    .select({ id: pokemons.id, name: pokemons.name, similarity })
    .from(pokemons)
    .where(lt(similarity, 0.5))
    .orderBy((t) => asc(t.similarity))
    .limit(8)

Note:
Use postgres EXPLAIN to see the query plan.
For this small dataset of 150 pokemons, for both queries the index aren't used, to test if index would be used on big datasets, so set SET SESSION enable_seqscan=false;
Exported sql queries: before, after, use with SET SESSION enable_seqscan=false; EXPLAIN {query} is psql/pgadmin

I'll do a PR for this repo later.

@DevJuliox
Copy link

The response we get in pokemon is an array, but what I expect is natural language. How could I achieve this? I'm using Vercel SDK AI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants