Skip to content

refactor: optimize first_added CTE by leveraging SQLite bare columns - #252

Merged
andrew merged 1 commit into
git-pkgs:mainfrom
abhinavgautam01:refactor/sqlite-min-optimization
Jun 28, 2026
Merged

andrew merged 1 commit into
git-pkgs:mainfrom
abhinavgautam01:refactor/sqlite-min-optimization

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Description

This PR is a follow-up to #250 mentioned #250 (review)_ here. It optimizes the first_added CTE in internal/database/queries.go by leveraging SQLite's "bare columns in an aggregate query" guarantee.

Instead of using a correlated subquery to fetch the added_in commit SHA, we can directly select c.sha alongside the MIN(c.committed_at) aggregate. Since we use modernc.org/sqlite, it is safe to rely on SQLite's guarantee that bare columns will come from the exact row that produced the minimum aggregate value.

This simplification:

  • Removes the need for a correlated subquery.
  • Reduces overall query complexity.
  • Removes an extraneous parameter binding (branchID).
  • Maintains the correct behavior (verified by our existing regression guards).

Testing

  • Verified TestSearchDependenciesCrossEcosystem and TestSearchDependenciesAddedInSHA continue to pass.
  • Verified all other tests pass successfully with the updated query format.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the follow-up. Verified the regression tests still pass locally.

Might be worth a one-line comment above first_added noting that c.sha next to MIN(c.committed_at) relies on SQLite's bare-column-in-aggregate guarantee, since it reads like a bug to anyone coming from Postgres. Not blocking.

@andrew
andrew merged commit 3b0e527 into git-pkgs:main Jun 28, 2026
4 checks passed
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

Successfully merging this pull request may close these issues.

2 participants