Skip to content

refactor: narrow implicit from_many subquery selections - #281

Open
jechol wants to merge 2 commits into
ash-project:mainfrom
jechol:perf/prune-relationship-projections
Open

jechol wants to merge 2 commits into
ash-project:mainfrom
jechol:perf/prune-relationship-projections

Conversation

@jechol

@jechol jechol commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Prune unused columns from implicit from_many? subquery selections. Preserve explicit selections and whole-row references.

For example, an invoice query reads the latest payment's status and paid_at, but the lateral subquery selects all 20 payment columns. This change narrows the selection while preserving references from enclosing scopes. Joins, nesting, filtering and ordering are unchanged.

PostgreSQL already optimizes away these unused projections. The main motivation is to make generated SQL easier to inspect while debugging. Other SQL backends may also benefit, though that has not been measured.

ash-project/ash_postgres#919 applies pruning immediately before query execution.

I've put together a runnable example with formatted before/after SQL.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T06:55:39.232692Z 7f7baef New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c76ce808e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/query/projection.ex Resolved
Comment thread lib/query/projection.ex
]

# Run at the execution boundary: later calculations can reuse an existing join.
def prune(query) do

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.

have we benchmarked this? I hesitate to do something that makes the SQL smaller and take a non-trivial performance hit on all queries. I'm wondering if this could be "built up" while we build the query instead of done as a pass at the end to avoid the cost of re-traversal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried collecting required columns during query construction and opened #282 with that implementation.

I've also updated the benchmark to compare upstream, this PR, and the new implementation, including query construction costs. For the latest Payment query, the new implementation uses about 1.6% more caller-process reductions than upstream and 4.6% fewer than this version. Wall times varied between runs, so I'm not claiming a runtime speedup.

This branch has not been deployed

No deployments
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