Skip to content

Visibility: replace the full-table count behind "does this brand have data" with an existence check #729

Closed

Description

@gkhngyk

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

  • getBrandResultsTotal and its single call site in getInsightsData
  • The hasAnyData type stays a boolean, so no surface changes

How to test

  1. 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.
  2. 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.
  3. 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

  1. added
    enhancement
    ai visibility
    on Aug 17, 2026
  2. shahzainshafique commented on Aug 22, 2026

    @shahzainshafique
    Contributor

    Hi @gkhngyk,
    I'd like to work on this issue. Please assign it to me.

  3. gkhngyk commented on Aug 25, 2026

    @gkhngyk
    ContributorAuthor

    Assigned to you, @shahzainshafique — thanks for picking it up. getBrandResultsTotal in web/src/lib/actions/tracking.ts is 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.

  4. added a commit that references this issue on Aug 28, 2026
    b524a59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai visibilityenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions