spring-projects / spring-projects/spring-ai
Optimize SQL query for pgvector similarity search
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>
<version>1.1.0-M1</version>
</dependency>
Currently, Spring AI uses the following SQL for pgvector similarity search:
SELECT *, embedding <=> ? AS distance FROM %s WHERE embedding <=> ? < ? %s ORDER BY distance LIMIT ?
This query calculates the <=> operator twice (in SELECT and WHERE), which may cause redundant distance calculations and unnecessary overhead. It can be optimized to compute the distance only once.
Proposed Improvement:
Use a subquery to avoid recomputing <=> and improve readability and performance:
SELECT * FROM ( SELECT *, embedding <=> ? AS distance FROM %s ) sub WHERE distance < ? %s ORDER BY distance LIMIT ?;
Do you think it'll work?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides the current and proposed pgvector similarity-search SQL but names no source file or test. Locate the Spring AI pgvector similarity-search implementation, compare the generated query and parameter handling with the proposal, and verify that results remain equivalent while avoiding redundant distance computation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, postgresql, sql
- Domain
- backend, database, performance
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100