Repository navigation
fix(performance): Optimize automations list collection query - #2081
sentry[bot] wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Closing this one. JUNIOR-AJ isn't a real N+1: the latest event shows the identity lookup plus five select spans, which are the four aggregates (total, scope counts, creator options, destination options) running in parallel and then one page query. That's a fixed number of queries, not one per row, and the slowest takes about 45ms of a ~494ms request.
The change also undoes what #1978 set out to do. It loads the whole access-scoped collection into Node on every request and reimplements search, sort, and paging in JS. Memory and transfer would then grow with the number of accessible automations instead of the page size. It also quietly changes ordering: compareText sorts by code point, but Postgres ORDER BY uses the database collation. And no tests cover the rewritten paths.
If this endpoint ever gets slow, a better fix is to keep paging in SQL and compute the aggregates in a single CTE/count(*) filter pass over the collection, with the existing integration tests in tests/integration/api/automations.test.ts as the guard.
There was a problem hiding this comment.
Closing this one — I don't think the change is worth shipping.
JUNIOR-AJ isn't really an N+1. The detector grouped five select spans that are separate fixed queries (filtered total, scope counts, creator options, destination options, page rows). Their count doesn't grow with the number of rows. In the latest flagged trace (ddf0ef74cc2b4f7cb22066794bc90b63), those queries took about 6–45ms each, and four of them run in parallel. The ~490ms request is mostly the four junior_task_executions reads at ~330ms each, and this PR doesn't touch them.
The rewrite also goes back on #1978, which deliberately moved search, filtering, sorting, and paging into SQL. Now every request would load the viewer's whole accessible collection, instruction text included, and filter/sort/page it in JS. So the cost grows with collection size instead of page size. It also swaps Postgres collation, lower(), and min() semantics for code-point and toLowerCase() approximations, which can quietly change sort order and option labels. And there's no test covering the new path.
If this endpoint's latency becomes a problem, the execution-chart queries are the place to look.
There was a problem hiding this comment.
Closing this one. The trade-off doesn't hold up.
The Sentry issue flags a "N+1", but the trace for JUNIOR-AJ shows four aggregate queries running in parallel (14–45ms) and then a 6ms page query. That's five queries no matter how many automations there are, not N+1. The request takes ~494ms overall, and most of that time isn't spent in these queries.
The change swaps that out for one unbounded SELECT of the viewer's entire accessible collection: every owned automation plus every public one in their Slack workspaces, with instruction text and the correlated last-execution lookup for each row. Node then counts, filters, sorts, and pages those rows. Before, the page read was LIMIT pageSize and the aggregates stayed in Postgres. So the cost now grows with the size of the workspace instead of the page size, which is a worse scaling profile for the list endpoint.
It also re-implements database semantics in JS. compareText compares UTF-16 code units, toLowerCase handles casing, and there's a hand-rolled trim. Those match Postgres ORDER BY/min()/lower() only under C collation. Under the default collation, the order of creator/destination options and title sort can change. There's no test that pins the old ordering or the counts, and the PR says integration tests weren't run.
If the repeated union evaluation turns out to matter, a smaller fix would keep it in SQL. One option is a WITH accessible AS MATERIALIZED (...) CTE that feeds the counts, the option groupings, and the limited page in one statement. That way the existing ordering semantics and LIMIT stay as they are.
There was a problem hiding this comment.
Closing this one. I don't think the underlying problem is real, and the change makes the endpoint scale worse.
- JUNIOR-AJ is a false positive from the N+1 detector. The five flagged spans are the four parallel aggregates (total, scope counts, creator options, destination options) plus the page query in
readAutomationCollection. That's a fixed five queries per request no matter how many automations there are. They all get flagged because each span is just labeledselect. In the latest event they add up to about 45ms of a ~494ms request, so the DB isn't where the time goes. - The rewrite pulls every accessible automation row into memory on every list request, full instruction text included, and does the filtering, search, sorting and paging in JS. That undoes #1978, which moved this work into SQL on purpose so the list could cover the whole accessible collection without a cutoff. The current cost is bounded by page size. With this change it grows with the size of the collection.
- It also changes behavior without any tests: title and label ordering switch from the database collation to code-point order, and case folding for search switches from Postgres
lower()to JStoLowerCase(). Integration tests weren't run either.
If the repeated union shows up as a real cost later, a better fix is to keep paging in SQL and fold the aggregates into fewer queries (for example a single CTE or a window count). Resolving or tuning the detector for JUNIOR-AJ is probably enough for now.
|
7 screenshot changes — 7 changed · 0 added · 0 removed Review screenshots in Frameshift
|
The
GET /api/automationsendpoint was identified as having an N+1-like query pattern. ThereadAutomationCollectionfunction inpackages/junior/src/chat/automations/collection.tswas executing the same expensiveviewerAutomationCollectionunion query multiple times (for total count, scope counts, creator options, destination options, and the paged rows).This change modifies
readAutomationCollectionto execute theviewerAutomationCollectionquery only once, fetching all necessary columns. All subsequent calculations for total count, scope counts, creator/destination filter options, filtering, sorting, and paging are now performed in-memory on this single result set. This significantly reduces database load and improves performance for the automations list endpoint.Linting, typechecking, and formatting checks passed. Integration tests were not run in this environment but are essential for full verification.
Fixes JUNIOR-AJ
@sentry <feedback>: Autofix iterates on these changes@sentry stop iterating: Autofix stops iterating on this runThis PR was automatically generated by Sentry. You can adjust this setting at any time.