spring-projects / spring-projects/spring-ai

Optimize SQL query for pgvector similarity search

Open
#4,465 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.