Repository navigation
perf(visibility): check brand data existence with LIMIT 1 instead of an exact COUNT - #789
Conversation
… 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
left a comment
There was a problem hiding this comment.
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 > 0can now beunfilteredHasData ?? (summary.totalResults > 0). The explicit null check was load-bearing while the value was a number, because0is falsy; narrowing it to a boolean retired that.- The same shape lives in
traffic.ts, wherecountVisits()is called with no arguments and read only as> 0. Out of scope here, andai_traffic_logsis 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!
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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsChecklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)