Repository navigation
perf(insights): drop the full-table scan on load + use SQL limit/count (closes #313) - #352
Merged
Merged
Conversation
The Insights page fired five server actions through one client-side Promise.all; Next.js runs server actions sequentially (each is its own POST), so the wait was ~their sum. One of them scanned the brand's entire prompt_results history, and getPromptResults over-fetched every row's heavy columns just to slice/count in JS. (#313) - getPromptResults now issues one query: SQL `.range(offset, offset+limit)` for the page + `count: 'exact'` for an accurate total, instead of materializing every matching row and slicing/counting in Node. This also fixes a latent cap: the old fetch-all was bounded by Supabase's default ~1000 rows, so both the returned list and `total` were silently capped; the range + count return the true page (up to `limit`) and true N. - New getInsightsData(brandId, opts): runs summary + results + competitor + SoV in a real server-side Promise.all (one round trip) and derives "has any data" from a cheap `count: 'exact', head: true` instead of the old unbounded `limit: 1` scan. - Insights page calls the one consolidated action. Behavior-preserving: same rows (newest first, same cap), same numbers, same filters/empty states. buildResultsQuery's count is opt-in so exportPromptResults is untouched. Also speeds up the topic detail page (#312), which shares getPromptResults.
There was a problem hiding this comment.
Pull request overview
This PR improves first-load performance for /dashboard/insights by eliminating client-side server action serialization and removing an unbounded prompt_results scan from the load path. It also makes getPromptResults return an accurate total by using SQL pagination + count instead of fetching all matching rows and slicing/counting in JS.
Changes:
- Updated
getPromptResultsto use.range(offset, offset + limit - 1)andcount: 'exact'to page in SQL and return an accurate total (fixing the prior ~1000-row silent cap). - Added
getInsightsData(brandId, opts)to consolidate the Insights page’s data fetch into a single server action with a real server-sidePromise.all. - Updated the Insights page to call
getInsightsDatainstead of firing multiple server actions from the client.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| web/src/lib/actions/tracking.ts | Adds opt-in count support to the results query, implements SQL paging + count in getPromptResults, and introduces consolidated getInsightsData with a cheap existence count. |
| web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx | Replaces multiple client-initiated server actions with a single consolidated getInsightsData call to avoid serialized server action latency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+322
to
+327
| const { count } = await supabase | ||
| .from('prompt_results') | ||
| .select('*', { count: 'exact', head: true }) | ||
| .eq('brand_id', brandId) | ||
| .neq('platform', 'chatgpt-shopping'); | ||
| return count ?? 0; |
…review) getBrandResultsTotal swallowed a Supabase error and returned 0, which would flip hasAnyData to false and show the empty state (and hide the failure). Throw on error like the other queries in this file, and select `id` for the head-only count instead of `*`. Addresses the Copilot review on #352.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Speeds up the first load of
/dashboard/insights. The page fired five server actions through one client-sidePromise.all; Next.js runs server actions sequentially (each is its own queued POST), so latency was ~their sum. One of them scanned the brand's entireprompt_resultshistory, andgetPromptResultsover-fetched every matching row's heavy columns (full response text + JSON) just to slice/count in JS. (#313)Changes
getPromptResults(web/src/lib/actions/tracking.ts) — now issues one query: SQL.range(offset, offset + limit - 1)for the page pluscount: 'exact'for an accurate total, instead of materializing every matching row and slicing/counting in Node.totalwere silently capped at ~1000. The range + count now return the true page (up tolimit) and the trueN— which is exactly what "Showing 1500 of N" should show.getInsightsData(brandId, opts)(new) — runs summary + results + competitor + SoV in a real server-sidePromise.all(genuinely parallel, one round trip) and derives "has any data" from a cheapcount: 'exact', head: trueinstead of the old unboundedgetPromptResults(..., { limit: 1 })scan.getInsightsDatacall.Behavior-preserving
This is a performance refactor — the displayed rows, grouped table, and every number stay identical:
PROMPT_RESULTS_ROW_LIMITcap), same KPI/competitor/SoV/trend numbers, same filters + empty states.buildResultsQuery's count is opt-in, soexportPromptResults(which paginates and doesn't need a per-page count) is untouched.getPromptResults.Related issue
Closes #313. (The optional "narrower select" from the issue — dropping heavy columns not shown in the row list — is intentionally left as a safe follow-up so this PR stays strictly behavior-preserving; the count +
.range()already remove the unbounded scan and the over-fetch of all rows.)Type of change
refactor— performanceHow to test
/dashboard/insightson a brand with a large history → first paint is noticeably faster; there is noselect('*')over the whole history in the load path (the has-data check is acount … head: true).yarn typecheck,yarn lint,yarn format:check,yarn testpass fromweb/.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (fromweb/)yarn typecheckpasses (fromweb/)yarn format:checkpasses (fromweb/)