Skip to content

perf(visibility): check brand data existence with LIMIT 1 instead of an exact COUNT - #789

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
shahzainshafique:fix/visibility-existence-check
Aug 27, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
shahzainshafique:fix/visibility-existence-check

Conversation

@shahzainshafique

Copy link
Copy Markdown
Contributor

Summary

The Visibility page answered "does this brand have any data at all?" with an exact COUNT(*) over prompt_results (a ~128 MB bitmap heap scan on the largest brand) only to compare the total against zero. Since the one consumer reads it as a boolean, getBrandResultsTotal becomes brandHasResults, selecting id with .limit(1) so the planner stops at the first match (~16,500 buffers → 2). hasAnyData stays a boolean and the "no data for this filter" vs "no data at all" distinction is unchanged.

Related issue

Closes #729

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

… COUNT

The Visibility page answered "does this brand have any data at all?" with
an exact COUNT(*) over prompt_results — a bitmap heap scan of ~16,500
buffers (~128 MB) on the largest brand — only to compare the total against
zero. The result feeds a single boolean, hasAnyData.

Ask for one row instead. getBrandResultsTotal becomes brandHasResults,
selecting `id` with `.limit(1)`, so the planner stops at the first match:
an index scan on a selective brand, an early-exit sequential scan on a
large one. On the same brand this drops ~16,524 buffers / 63.8 ms to
2 buffers / 3.8 ms. It is the largest of the page's eight parallel queries
and the only one that can be deleted outright rather than optimized.

hasAnyData stays a boolean and the "no data for this filter" vs "no data
at all" distinction is unchanged, so there are no surface changes. A brand
whose rows are all chatgpt-shopping still correctly reports no results.

Closes ansvisor#729

@gkhngyk gkhngyk left a comment

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.

Verified against production rather than taken on trust, and it holds: on the largest brand the count was 19,993 buffers / 81ms, the new check is 2 buffers / 0.085ms.

The case I went looking for is the one that isn't in the issue. LIMIT 1 has a planner trap on the miss — with no matching row and a seq scan, it reads to the end of the table. That is exactly the state this flag exists to detect: a brand with no data at all. So the change could have been slower precisely where it matters most.

It isn't. On a brand with zero rows the planner takes idx_prompt_results_brand_created and answers in 0.85ms. Checked a 48-row brand and a 21k-row brand too — index scans, all sub-millisecond. The seq scan only appears on the brand dense enough that the first heap page already contains a match.

Behaviour is unchanged: getBrandResultsTotal had exactly one consumer and it read the number as a boolean, the throw-don't-swallow guard is preserved, and the "no data for this filter" vs "no data at all" distinction still resolves the same way.

Two optional things, neither blocking — merge as is if you'd rather not touch it again:

  • unfilteredHasData !== null ? unfilteredHasData : summary.totalResults > 0 can now be unfilteredHasData ?? (summary.totalResults > 0). The explicit null check was load-bearing while the value was a number, because 0 is falsy; narrowing it to a boolean retired that.
  • The same shape lives in traffic.ts, where countVisits() is called with no arguments and read only as > 0. Out of scope here, and ai_traffic_logs is 6,108 rows today so there is nothing to gain yet — noting it so it isn't rediscovered from scratch later.

Nice, tightly scoped change. Thanks!

@gkhngyk
gkhngyk merged commit b524a59 into ansvisor:main Aug 27, 2026
5 checks passed
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.

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

2 participants