Repository navigation
Description
Summary
Opening the Visibility page fires an exact COUNT(*) over the brand's prompt_results rows to answer a yes/no question: does this brand have any data at all? The answer is used as a boolean and nothing else, but the query scans every matching row to produce it.
On the largest brand in one production database that is a bitmap heap scan of 16,524 buffers — about 128 MB — to learn that the count is greater than zero. Warm it takes ~64 ms; cold it is 128 MB of disk reads, and it runs alongside seven other aggregate queries that all start in the same tick.
The count is the eighth of those eight parallel queries and the only one that reads the table directly rather than through an aggregate function. It is also the only one whose result is thrown away down to a single bit.
Where it is
getBrandResultsTotal in web/src/lib/actions/tracking.ts:
const { count, error } = await supabase
.from('prompt_results')
.select('id', { count: 'exact', head: true })
.eq('brand_id', brandId)
.neq('platform', 'chatgpt-shopping');Its only consumer, in getInsightsData:
const hasAnyData = unfilteredTotal !== null ? unfilteredTotal > 0 : summary.totalResults > 0;The doc comment calls it a "cheap existence check ... counts, no rows". Counting no rows is not the same as reading no rows — count: 'exact' still visits every one of them.
It runs on the default page load, not only on an explicitly filtered view: the default date preset is 24h, so hasFilters is true from the first render and checkUnfiltered is always set.
Why it matters now
Statement timeouts on this workload arrive in clusters — seven to nine of them within the same second, one per query in the page's Promise.all. That is the whole page load failing together when the buffer cache is cold, which the user sees as a generic server-action error toast rather than as a slow page.
Removing this query does not fix the cold-cache problem on its own; the other seven still scan. But it is the largest single read of the eight, and the only one that can be deleted outright rather than optimized.
Proposed change
Ask for one row instead of all of them:
const { data, error } = await supabase
.from('prompt_results')
.select('id')
.eq('brand_id', brandId)
.neq('platform', 'chatgpt-shopping')
.limit(1);and return a boolean. Measured on the same two brands:
| Brand | Rows | Current (exact count) | With limit(1) |
|---|---|---|---|
| Largest | ~53,000 | 16,524 buffers, 63.8 ms | 2 buffers, 3.8 ms |
| Smallest | 48 | — | 4 buffers, 1.7 ms |
The planner picks a plain index scan on idx_prompt_results_brand_id for a selective brand and a sequential scan with an early exit for a large one. Both stop at the first matching row.
One case is worth checking while implementing: a brand whose rows are all chatgpt-shopping has no early exit to reach, so the filter runs to the end of whatever plan is chosen. Confirm the shape there before merging.
Renaming the function to say what it now returns (brandHasResults) keeps the call site honest, and unfilteredTotal !== null becomes a straightforward boolean check.
Scope
getBrandResultsTotaland its single call site ingetInsightsData- The
hasAnyDatatype stays a boolean, so no surface changes
How to test
- Open the Visibility page for a brand with a large result history and confirm the KPI cards, competitor table and Share of Voice render unchanged.
- Apply a filter combination that returns nothing and confirm the page still distinguishes "no data for this filter" from "no data at all" — that distinction is the only thing this query exists for.
- Open the page for a brand that has never completed a tracking run and confirm the empty state is the onboarding one, not the filtered one.
Activity
shahzainshafique commentedon Aug 22, 2026
Hi @gkhngyk,
I'd like to work on this issue. Please assign it to me.
gkhngyk commentedon Aug 25, 2026
Assigned to you, @shahzainshafique — thanks for picking it up.
getBrandResultsTotalinweb/src/lib/actions/tracking.tsis the whole surface; note its only consumer reads the result as a boolean, so the return type can narrow with it. Ping here if anything is unclear.perf(insights): check brand data existence with LIMIT 1, not an exact…